# Car style turn movement

I have been attempting to do a car style turn movement for when my I press either left or right, but I am not sure how I should have it be done. What I was previously doing what when the player pressed up/down it would cause the player to go forward/backwards depending on the forward vector of the player. However, for turning I would like the player to turn like how a car would, rather than just rotating directly on the Y. How would something like this be possible?

Iâ€™m sure someone with more experience could provide more detail or specifics on this, but I would approach this by modifying a turning angle based on the left/right input. Not just hardwiring the Y rotation to it, but adjusting a turn angle (turning speed, max turning speed could then be controlled by a couple public variables so you can adjust in the inspector, etc). As left or right are input, make the change to the turning variable. Then have the car adjust its Y rotation based on that turning angle, and then do movement based on its speed. That way the car would be moving in the direction its then pointing. By controlling a turning angle, you would have control over how sensitive the steering is then.

Just my thoughts on this from a top down look.

~Scorp

That sounds like a great idea. I have been trying to look up how to do turning angles, but I canâ€™t see anything for it. Do you happen to know how to calculate it?

If youre not making a car, that method is overly complicated.

a simple transform.Rotate(0,1,0) will suffice

Not exactly.

For example, if I had my up arrow bound to move along the forward axis and my right arrow to just simply rotate on the Y, if I held both down at the same time then I would not get a nice fluid turn.

``````float forward = 0;
float left = 0;

if(Input.GetKey(KeyCode.W))
forward = 1;
else if(Input.GetKey(KeyCode.S))
forward = -1;

if(Input.GetKey(KeyCode.A))
left = -1;
else if(Input.GetKey(KeyCode.D))
left = 1;

transform.Translate(forward,0,0);
transform.Rotate(0, left * forward, 0);
``````

also, if you dont like the way it rotates when in reverseâ€¦

transform.Rotate(0, left * Mathf.abs(forward), 0);

I donâ€™t feel that it would be overly complicated at all actually. Consider the following code:

``````public float TurnSpeed = 2f;
public float MoveSpeed = 10f;

private float leftRight = 0f;
private float forward = 0f;

// Use this for initialization
void Awake()
{

}

void ProcessInput()
{
leftRight = Input.GetAxis("Horizontal");
forward = Input.GetAxis("Vertical");
}

// Update is called once per frame
void Update()
{
ProcessInput();

transform.RotateAroundLocal(Vector3.up, leftRight * TurnSpeed * Time.deltaTime);

transform.Translate(0, 0, forward * MoveSpeed * Time.deltaTime);
}
``````

As simple as this is, it accomplishes several goals, imo. Firstly, you have a couple variables you can tweak as necessary for moving and turning speed. Utilize the built in axis names and configure you input options within Unity as needed so that you have the flexibility for control options in the future. You could hardwire the input to the specific keys, but I highly recommend that you donâ€™t. The other code posted doesnâ€™t actually give the desired result I donâ€™t believe because not only is it turning based on a fixed hardcoded amount, but it is also rotating on the wrong axis and not accounting for system performance. By using this technique, youâ€™re addressing all of those things. In fact, it would be a very simple addition to add in a secondary rotational angle around the Y axis for the object, which could be applied after the main transformations, to simulate swing from forces when turning (sliding or drifting in the case of a car as an example). Itâ€™s important to keep things so that these other functionalities are able to be implemented, imo. While Unity does offer some functions to make something very simple to do, sometimes with only one line of code, youâ€™re never going to find a solid game or simulation using something as simple as that. The extra pays off in the end, and is almost always required for what could be considered a finished product. Just my thoughts.

I just added this code as a script to a simple cube object and â€śdroveâ€ť it around on top of another simple cube made into a plane and it worked quite well for me. In fact it inspired me to possibly make a camera system for a driving game, so that the cameraâ€™s position to lag behind the car smoothly with some swing, because typically you would see the object/car turning in front of the camera for a moment until the camera has a good chance to â€śget back into positionâ€ť straight behind the object/car. Anyhoo, Iâ€™m rambling with thoughts.

Hope this helps,

~Scorp

Youâ€™ve offered nothing more than an incorrect solution, with some added smoothing.

The rotation will still occur regardless of whether you are moving forward.

Iâ€™m aware of that (rotation will occur). The difference is that itâ€™s more controllable and on the correct axis.

Oh wait, you mean the fact that you can rotate when not moving forward or reverse? Ohâ€¦ my badâ€¦

and yeah this is a very crude way which needs more to be realistic, but still better to work with imo than a hardcoded fixed amount of rotation.

``````if(mathf.Abs(forward) > 0)
transform.RotateAroundLocal(Vector3.up, leftRight * TurnSpeed * Time.deltaTime);
``````

I would still prefer to maintain a higher level of control. Many ways to skin a cat.

1 Like

how to stop rotation???