Help - 3D Vehicle using a sphere

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

Hi,

After seeing an example of a vehicle in Unity using a sphere to control it, I wanted to make something similar in godot. https://twitter.com/KenneyNL/status/1107783904784715788

What I basically have is a RigidBody with a sphere collision shape that controls the forward/backward momentum and a sprite inside the sphere that dictates the direction everything is supposed to be heading torwards to.

It’s hard to explain, but the problem is that when I stop turning, the ball goes diagonally instead of straight forward, depending of the orientation.

Here’s the code for my RigidBody

extends RigidBody

var movement = Vector3.ZERO
var velocity = Vector3.ZERO
var speed = 15
onready var sprite

func _integrate_forces(state):
	#collision_layer = 1
	#collision_mask = 1
	# Reset movement
	movement = Vector3.ZERO
	# Get sprite's vector
	sprite = get_node("../sprite")
	velocity =  sprite.transform.basis.z
	
	# Inputs
	if Input.is_action_pressed("up"):
		movement.z -= 1
		movement.x -= 1
	if Input.is_action_pressed("down"):
		movement.z += 1
		movement.x += 1
	if Input.is_key_pressed(KEY_SPACE):
		 collision_layer = 0
		 collision_mask = 0

	# Apply force
	add_force(Vector3(movement * velocity).normalized() * speed, Vector3.ZERO)

Here’s the code for the Sprite:

extends Sprite3D

onready var TargetNode = get_node("../player_rb")
onready var StartOffset = self.transform.origin - TargetNode.transform.origin

func _process(delta):
	# Follow the RigidBody's position
	self.transform.origin = TargetNode.transform.origin + StartOffset
	# Rotate left or right
	if Input.is_action_pressed("left"):
		rotate_y(deg2rad(2))
	if Input.is_action_pressed("right"):
		rotate_y(deg2rad(-2))
:bust_in_silhouette: Reply From: MrEliptik

It’s a bit late to reply and you probably already solved the problem, but for it might be interesting for some.

If I understand your problem correctly I think the car is still moving diagonally when you release the gas because you’re only applying force to your rigid body when pressing a key. You would need to apply a force constantly to keep the right direction. You could decrease that force with time when not accelerating that way, your body will come to a stop.