Car rotate

Hi,
I copied code from class, but the z position from the “Car” object didn’t change when the “Horizontal” Input changes. How can I move objects freely?

This is my code.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class driver : MonoBehaviour

{

[SerializeField] float steerSpeed = 1f;

[SerializeField] float moveSpeed = 0.01f;



void Start()

{

   

}

void Update()

{

    float steerAmount = Input.GetAxis("Horizontal") * steerSpeed * Time.deltaTime;

    float moveAmount = Input.GetAxis("vertical") * moveSpeed * Time.deltaTime;

    transform.Rotate(0, 0, -steerAmount);

    transform.Translate(0, moveSpeed, 0);

}  

}

The Z-Position is up and down. You don’t want the car to move up and down. What you are changing is the rotation on the Z-Axis. That’s not the position.

Your code has a problem, though. You are calculating the Y-position, but you are not using it. You are using the moveSpeed instead of the moveAmount. You are also looking at “vertical” which is wrong. It needs an uppercase “V”. You will probably get errors saying that “vertical” does not exist

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms