Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CarMovement : MonoBehaviour
- {
- public static CarMovement Instance;
- //making public varaible for image
- public UnityEngine.UI.Image image;
- //fuel
- public float fuel = 1;
- //engine burning the fuel
- public float fuelconsumption = 0.1f;
- //Speed for the car will move
- public float speed = 1500f;
- public float rotationSpeed = 15f;
- //ref to back wheel
- public WheelJoint2D backWheel;
- //ref to front wheel (also this is the Torque)
- public WheelJoint2D frontWheel;
- public Rigidbody2D rb;
- private float movement = 0f;
- private float rotation = 0f;
- void Awake()
- {
- Instance = this;
- }
- //This is for user input
- void Update()
- {
- movement = -Input.GetAxisRaw("Vertical") * speed;
- rotation = Input.GetAxisRaw("Horizontal");
- //it counted in frames
- image.fillAmount = fuel;
- }
- //This is where we DO the movemnet
- void FixedUpdate()
- {
- // check if the car will be moving
- //fuel checker
- if (movement == 0f )
- {
- backWheel.useMotor = false;
- frontWheel.useMotor = false;
- }
- else if(fuel > 0 )
- {
- Debug.Log("We have fuel");
- backWheel.useMotor = true;
- frontWheel.useMotor = true;
- // you can also make it "1000" rather than = "backWheel.motor.maxMotorTorque"
- JointMotor2D motor = new JointMotor2D { motorSpeed = movement, maxMotorTorque = backWheel.motor.maxMotorTorque };
- backWheel.motor = motor;
- frontWheel.motor = motor;
- }
- rb.AddTorque(-rotation * rotationSpeed * Time.deltaTime);
- fuel -= fuelconsumption * Mathf.Abs(movement) * Time.fixedDeltaTime;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement