![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Millard |
I have a ball rolling script, but the ball rolls faster if it is moving diagonally. I realize that I have to normalize the movement to fix it, but I am not sure how to do this in this case. here’s my code:
extends KinematicBody
var speed = 500
var rot_speed = 9
var acceleration = .2
var velocity = Vector3(0,0,0)
func _ready():
pass
func _physics_process(delta):
#cam_target.rotation = Vector3(0,0,0)
if Input.is_action_pressed("ui_right") and Input.is_action_pressed("ui_left"):
velocity.x = 0
elif Input.is_action_pressed("ui_right"):
velocity.x = speed
$MeshInstance.rotate_z(deg2rad(-rot_speed))
elif Input.is_action_pressed("ui_left"):
velocity.x = -speed
$MeshInstance.rotate_z(deg2rad(rot_speed))
else:
velocity.x = lerp(velocity.x, 0, acceleration)
if Input.is_action_pressed("ui_up") and Input.is_action_pressed("ui_down"):
velocity.z = 0
elif Input.is_action_pressed("ui_up"):
velocity.z = -speed
$MeshInstance.rotate_x(deg2rad(-rot_speed))
elif Input.is_action_pressed("ui_down"):
velocity.z = speed
$MeshInstance.rotate_x(deg2rad(rot_speed))
else:
velocity.z = lerp(velocity.z, 0, acceleration)
move_and_slide(velocity * delta, Vector3(0,-1,0))
Thanks in advance.