2D game - Unable to rotate sprite as per direction

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

I want the car the move forward and reverse only. It will turn only if it hits a curve either in front or at back. The below code turns the car forward but cant figure out to turn it backwards. Also when it is moving forward and I hit the down button it moves backward but the sprite also changes direction 180 degrees. Backward becomes forward and forward becomes backwards.

Someone please help me to get things right.

My code as below:

extends KinematicBody2D

export (int) var speed = 200

var velocity = Vector2()
var moving = false

# Called when the node enters the scene tree for the first time.
func _ready():
	set_physics_process(true)


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
	get_input()
	if moving == true:
		velocity = move_and_slide(velocity)

func get_input():
	if Input.is_action_pressed("ui_down"):
		velocity = Vector2(-speed, 0).rotated(rotation)
		moving = true
	elif Input.is_action_pressed("ui_up"):
		rotation = velocity.angle()
		velocity = Vector2(speed, 0).rotated(rotation)
		moving = true
	else:
		moving = false
:bust_in_silhouette: Reply From: njamster

When pressing the “ui_down”-key you’re setting the velocity to (-speed, 0). If you then press “ui_up” it will calculate the angle of this velocity-vector and rotate accordingly by 180°. In addition to that it will also set the velocity-vector, but not to (speed, 0), as you also rotate that vector by 180°. So the new velocity is identical to the old velocity of (-speed, 0). Meaning the player will rotate by 180° but still continue it’s movement.

This should work:

if Input.is_action_pressed("ui_down"):
    rotation = velocity.angle()
    velocity = Vector2(-speed, 0)
    moving = true
elif Input.is_action_pressed("ui_up"):
    rotation = velocity.angle()
    velocity = Vector2(speed, 0)
    moving = true

I have already tried this. Have you tried this code at your end. It didn’t work for me.

With this code when you press forward, the car rotates as per direction but keeps getting back to zero rotation. Secondly when you click back, it changes direction 180 degrees which is what I don’t want. If it is reversing, it should go back in reversing style and not change direction suddenly.

aadyainfotainment | 2020-04-01 20:52

Have you tried this code at your end. It didn’t work for me.

I did. And it works. It might not be what you want, but it works.

I find your description of what you want quite confusing, mostly because you keep talking about “forward” and “backward” when your code is using “ui_up” and “ui_down” to move your car to the left or right respectively… However, I guess you’re trying to implement a “realistic” car steering, i.e. one where the car sprite does not turn around immediately but needs to first turn around instead. In that case, check out this excellent tutorial on the topic. It should cover all you need.

njamster | 2020-04-01 22:13

Sorry but I forgot to mention that my game is top-down view. I am not using right left controls to turn the car. It should turn only if it hits a slanting obstacle.

aadyainfotainment | 2020-04-02 07:20

:bust_in_silhouette: Reply From: aadyainfotainment

Can someone else who understands this requirement reply?

It’s very simple requirement. Car has controls to go either forward or reverse.

So I used ui_up to move forward and ui_down to move backward.

The car will turn only if it hits a slanting obstacle (instead of sliding it will rotate in the direction of movement.) current updated code does not achieve this. The car is sliding and also the car is changing direction like a platformer. It is a top-down view so it should not change that way.