0 votes

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))
in Engine by (22 points)

1 Answer

0 votes

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.

by (617 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.