0

I'm mehmet. I'm new to Unity and trying to learn. I'm building a car controller for the Hyper Casual game using Wheel Collider. I want to clamp the rotation of the car in the y-axis. How can I do that?

private void Start()
{
    rb = GetComponent<Rigidbody>();
    rb.centerOfMass = centerOfmass;
}

private void Update()
{
    Move();
    AnimateWheels();
    transform.position = new Vector3(Mathf.Clamp(transform.position.x, -3f, 3f), transform.position.y,
        transform.position.z);
    
    if (Input.touchCount > 0)
    {
        _touch = Input.GetTouch(0);
        
        rotation = _touch.deltaPosition.x;

        if (_touch.phase == TouchPhase.Moved)
        {
            Turn();
            
        }

        if (_touch.phase == TouchPhase.Ended)
        {
            //rotationSpeed = 0;
        }
    }
    
}

private void Move()
{
    foreach (var wheel in wheels)
    {
        wheel.collider.motorTorque = 5f * 20 * 300 * Time.deltaTime;
    }
}

private void Turn()
{
    foreach (var wheel in wheels)
    {
        if (wheel.axel == Axel.Front)
        {
            var steerAngle = touchInput * rotationSpeed * 2;
            wheel.collider.steerAngle = Mathf.Clamp(wheel.collider.steerAngle, -25, 25);
            wheel.collider.steerAngle = Mathf.Lerp(wheel.collider.steerAngle, steerAngle, .3f * Time.deltaTime);
            
        }
    }
}

private void AnimateWheels()
{
    foreach (var wheel in wheels)
    {
        Quaternion _rot;
        Vector3 _pos;
        wheel.collider.GetWorldPose(out _pos, out _rot);
        
        wheel.model.transform.position = _pos;
        wheel.model.transform.rotation = _rot;
    }
}

}

These are the lines of code. I couldn't figure it out no matter what I did. Can you help?

0 Answers0