I have managed to go backwards and forwards with my kart in Unity. But when it comes to turning, it becomes a real challenge and I am unable to turn my vehicle smoothly with my a & d keys.
Please help!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KartController : MonoBehaviour
{
private Rigidbody rb;
// adding a speed variable
public float speed = 3.0f;
// adding a brake variable
public float BrakeSpeed = 1.0f;
// adding a rotation position variable
// Start is called before
void Start()
{
// refering to the game object component/class
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
// adding movement for forward
if (Input.GetKey(KeyCode.W))
{
rb.velocity = new Vector3(speed * -2, 0, 0);
}
// adding movement for backward
if (Input.GetKeyDown(KeyCode.S))
{
rb.velocity = new Vector3(speed * 2, 0, 0);
}
// adding movement for rotation
float rotation_speed = 10f;
if (Input.GetButtonDown("Horizontal"))
{
transform.Rotate(0.0f, -Input.GetAxis("Horizontal") * rotation_speed, 0.0f);
}
Debug.Log(rotation_speed);
}
}
CodePudding user response:
You've got a few different options and you are mixing a few things up.
Your Kart is a Rigidbody with a physics simulation. To move it forward or backwards you are setting the Karts velocity. If an object has a velocity in the physics simulation it will try to move because in the simulation the standard physics formula are solved. I.e. distance moved = velocity * time
For rotation the same concepts apply. There is the angular velocity of a rigidbody. If an object has angular velocity it tries to rotate accordingly.
With transform Rotate you are setting the rotation of the object directly. You are basically skipping the physics simulation. This is good if you want to for example reset your kart orientation and position at the start of the race but is less suited to control your Kart in the simulation.
You could directly set rb.angularVelocity similar how you set the Karts directional velocity.
Depending on the type of game and the realism you might want to look into controlling the acceleration instead of the velocity. Or even wheelcollider... (https://docs.unity3d.com/Manual/WheelColliderTutorial.html)
CodePudding user response:
You can try the following code to select the car, I hope it can help you, thank you
private float y;
//vehicle control speed parameters
private float speedOne = 0f; //Vehicle real-time speed
private float speedMax = 120f; //The maximum speed of the vehicle
private float speedMin = -20f; //The minimum speed of the vehicle (the maximum speed of reversing)
private float speedUpA = 2f; //Vehicle acceleration acceleration (A key control)
private float speedDownS = 4f; //Vehicle deceleration acceleration (S key control)
private float speedTend = 0.5f; //Acceleration when no operation real-time speed tends to 0
private float speedBack = 1f; //Vehicle reversing acceleration
// Update is called once per frame
void Update()
{
//mouse hide
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
//Press the W key and the speed does not reach the maximum, then the speed increases
if (Input.GetKey(KeyCode.W) && speedOne < speedMax)
{
speedOne = speedOne Time.deltaTime * speedUpA;
}
//Press the S key and the speed does not reach zero, then the speed is reduced
if (Input.GetKey(KeyCode.S) && speedOne > 0f)
{
speedOne = speedOne - Time.deltaTime * speedDownS;
}
// No speed operation is performed and the speed is greater than the minimum speed, then the slow operation
if (!Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S) && speedOne > 0f)
{
speedOne = speedOne - Time.deltaTime * speedTend;
}
if (!Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S) && speedOne < 0f)
{
speedOne = speedOne Time.deltaTime * speedTend;
}
//When the S key is pressed and the speed does not reach the maximum reversing speed, and the vehicle is in a state where the vehicle can be reversed, the vehicle reverses
if (Input.GetKey(KeyCode.S) && speedOne > speedMin && speedOne<=0)
{
speedOne = speedOne - Time.deltaTime * speedBack;
}
/// press space, the car stops
if (Input.GetKey(KeyCode.Space) && speedOne != 0)
{
speedOne = Mathf.Lerp(speedOne, 0, 0.4f);
if (speedOne < 5) speedOne = 0;
}
transform.Translate(Vector3.forward * speedOne * Time.deltaTime);
//Use A and D to control the left and right rotation of the object
if(speedOne>1f||speedOne<-1f)
{
y = Input.GetAxis("Horizontal") * 60f * Time.deltaTime;
transform.Rotate(0, y, 0);
}
/* if (transform.eulerAngles.z != 0)
{
transform.eulerAngles = new Vector3(transform.eulerAngles.x, t
ransform.eulerAngles.y, 0);
}*/
}