Rotation with kinematicBody

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ij3rry_

hai , i’m new to game development and i consider it as my hobby

var velocity = -transform.basis.z * speed * delta     # this is my velocity
move_and_slide(velocity)

and i added 5 rayCat in front , right , left , up , bottom and i want to rotate it like in this code .

if frontRayCast.is_colliding():
		#print("Got")
		bottomRayCast.enabled = true
		topRayCast.enabled = true
		rightRayCast.enabled = true
		leftRayCast.enabled = true
		if not bottomRayCast.is_colliding():
			rotate_x(deg2rad(-45))
		elif not topRayCast.is_colliding():
			rotate_x(deg2rad(45))
		elif not rightRayCast.is_colliding():
			rotate_z(deg2rad(45))
		elif not leftRayCast.is_colliding():
			rotate_z(deg2rad(-45))

but it’s rotating weirdly in both left and right sides

:bust_in_silhouette: Reply From: DaddyMonster

It’s a fun hobby, isn’t it? :slight_smile:

Trivial point first: if you’re getting into game dev then I’d encourage you to leave degrees behind and move to radians. Degrees are cost for no gain and radians come naturally from the geometry. PI radians is 180 so rather than 45 degrees, try to get into the habit of using PI/4. No big deal though.

I assume all the code is in your _physics_process method. Yeah, that’s going to produce some very erratic behaviour. It’ll only rotate when the front one is colliding. PI/4 radians at 60fps is 7.5 rotations per second. Plus the rotations stack. You might get dizzy! :slight_smile:

Also, you’re rotating the object on multiple axes. You have to be really careful about Euler rotations, they’re a bit like Rubik’s Cubes, once you do them they can be very hard to undo and your result is going to be chaotic. Here’s a great piece about the pitfalls that I’d encourage you to read:

Can I ask what you’re trying to achieve here? Are you looking for the object to turn towards / away from the collision? If so then the dot product is likely what you need.