I know that I would find it's hypotenuse and use something like asin(x / h) and asin(z / h) but I need to get these right.

This is what I have tried. I have no idea how it works it just does.

Game scene:

				    var movvec: Vector3 =  ($Player/Camera/RayCast.get_collision_point() - $Player.global_transform.origin)
					
					movvec.y = -movvec.y
					movvec = movvec.normalized()
					var basz = movvec.z * $Player.global_transform.basis.z					
					var basx = movvec.x * $Player.global_transform.basis.x
					var basy = movvec.y * $Player.global_transform.basis.y
					var tot: Vector3 = basy - basx - basz
					#var ptran = false
					if viewdir == 1 or viewdir == 3:
						bulinst.altpos()
						tot.x = -tot.x
						tot.z = -tot.z
					bulinst.set_rotation(tot)
					
						
					buldir.append(tot)
					$Player.add_child(bulinst)

Bullet scene:

var trans = false
func altpos():
	trans = true

func set_rotation(var coord: Vector3):
	var x = coord.x
	var z = coord.z
	var shy = pow((x * x) + (z * z), .5)
	var flgs = 0
	if x < 0:
		flgs = 1
	if z < 0:
		flgs += 2
	if trans:
		flgs += 4
	var sang = 0
	match flgs:
		0:
			sang = asin(x / shy)
		1:
			sang = asin(x / shy)
		2:
			sang = acos(z / shy)
		3:
			sang = asin(z / shy) + deg2rad(270)
		4:
			sang = asin(x / shy) + deg2rad(90)
		5:
			sang = asin(z / shy) + deg2rad(180)
		6:
			sang = acos(x / shy) 
		7:
			sang = asin(z / shy) + deg2rad(180)
		
		
	self.scale = Vector3(.5, .5, .5)
	self.rotate_y(sang)
	myVector = coord

It looks like you answered your own question then. =)

You can replace most of that with:

rotation.y = atan2(coord.x, coord.z)

atan2 takes handles turning a 2d vector's components into a 360 degree angle. (Well, -pi to pi radians technically)

  • Lis likes this.
6 months later