HillClimb/Assets/Scripts/carController.cs
2023-01-20 02:22:00 +05:30

78 lines
1.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class carController : MonoBehaviour
{
public WheelJoint2D frontWheel;
public WheelJoint2D rearWheel;
JointMotor2D motorFront;
JointMotor2D motorBack;
public float speedF;
public float speedB;
public float torqueF;
public float torqueB;
public bool TractionFront = true;
public bool TractionBack = true;
public float carRotationSpeed;
void Start()
{
CameraFollower.Target = transform;
}
// Update is called once per frame
void Update()
{
if (Input.GetAxisRaw("Vertical") > 0)
{
if (TractionFront)
{
motorFront.motorSpeed = speedF * -1;
motorFront.maxMotorTorque = torqueF;
frontWheel.motor = motorFront;
}
if (TractionBack)
{
motorBack.motorSpeed = speedF * -1;
motorBack.maxMotorTorque = torqueF;
rearWheel.motor = motorBack;
}
}
else if (Input.GetAxisRaw("Vertical") < 0)
{
if (TractionFront)
{
motorFront.motorSpeed = speedB * -1;
motorFront.maxMotorTorque = torqueB;
frontWheel.motor = motorFront;
}
if (TractionBack)
{
motorBack.motorSpeed = speedB * -1;
motorBack.maxMotorTorque = torqueB;
rearWheel.motor = motorBack;
}
}
else
{
rearWheel.useMotor = false;
frontWheel.useMotor = false;
}
if (Input.GetAxisRaw("Vertical") != 0)
{
GetComponent<Rigidbody2D>().AddTorque(carRotationSpeed * Input.GetAxisRaw("Vertical") * 1);
}
CameraFollower.UpdateFrame();
}
}