Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Control Car on Air

Discussion in 'Scripting' started by Darokar, Sep 7, 2015.

  1. Darokar

    Darokar

    Joined:
    Aug 22, 2015
    Posts:
    5
    Im desperate. Iooking for solution for 3 days on internet and dont found any who working. I try create simple car game 2d


    Im done all and all working but now i want add possibility to rotate car in air witch arrows keys.

    I look for internet for solution but any script dont work or get errors. So i PLEASE some smart person who can give me solution for A to B. What to do, to this script working on this car. Where i must add this script and how i can manual change force of rotation car in air. I need really smart person who can help me. I try learn Unity from 3 weeks but is complete nightmare because i see always only errors and nothing works. PLEASE HELP.
     
  2. gcoope

    gcoope

    Joined:
    Dec 20, 2012
    Posts:
    11
    Sounds like you'll want to Lerp the rotation of your cars transform
     
  3. Darokar

    Darokar

    Joined:
    Aug 22, 2015
    Posts:
    5
    I complete fresh in unity just can me someone paste some ready code and explain where i must him paste and how it works ? My code now for car is WheelJointCarMovement. Mayby there is some bug ?:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class WheelJointCarMovement : MonoBehaviour {
    6.     //reference to the wheel joints
    7.     WheelJoint2D[] wheelJoints;
    8.     //center of mass of the car
    9.     public Transform centerOfMass;
    10.     //reference tot he motor joint
    11.     JointMotor2D motorBack;
    12.     //horizontal movement keyboard input
    13.     float dir = 0f;
    14.     //input for rotation of the car
    15.     float torqueDir = 0f;
    16.     //max fwd speed which the car can move at
    17.     float maxFwdSpeed = -7000;
    18.     //max bwd speed
    19.     float maxBwdSpeed = 7000f;
    20.     //the rate at which the car accelerates
    21.     float accelerationRate = 800;
    22.     //the rate at which car decelerates
    23.     float decelerationRate = -100;
    24.     //how soon the car stops on braking
    25.     float brakeSpeed = 2500f;
    26.     //acceleration due to gravity
    27.     float gravity = 9.81f;
    28.     //angle in which the car is at wrt the ground
    29.     float slope = 0;
    30.     //reference to the wheels
    31.     public Transform rearWheel;
    32.     public Transform frontWheel;
    33.  
    34.     // Use this for initialization
    35.     void Start () {
    36.         //set the center of mass of the car
    37.         GetComponent<Rigidbody2D>().centerOfMass = centerOfMass.transform.localPosition;
    38.         //get the wheeljoint components
    39.         wheelJoints = gameObject.GetComponents<WheelJoint2D>();
    40.         //get the reference to the motor of rear wheels joint
    41.         motorBack = wheelJoints[0].motor;
    42.     }
    43.     //all physics based assignment done here
    44.     void FixedUpdate(){
    45.         //add ability to rotate the car around its axis
    46.         torqueDir = Input.GetAxis("Horizontal");
    47.         if(torqueDir!=0){
    48.             GetComponent<Rigidbody2D>().AddTorque(3*Mathf.PI*torqueDir, ForceMode2D.Force);
    49.         }
    50.         else{
    51.             GetComponent<Rigidbody2D>().AddTorque(0);
    52.         }
    53.         //determine the cars angle wrt the horizontal ground
    54.         slope = transform.localEulerAngles.z;
    55.      
    56.         //convert the slope values greater than 180 to a negative value so as to add motor speed
    57.         //based on the slope angle
    58.         if(slope>=180)
    59.             slope = slope - 360;
    60.         //horizontal movement input. same as torqueDir. Could have avoided it, but decided to
    61.         //use it since some of you might want to use the Vertical axis for the torqueDir
    62.         dir = Input.GetAxis("Horizontal");
    63.      
    64.         //explained in the post in detail
    65.         //check if there is any input from the user
    66.         if(dir!=0)
    67.             //add speed accordingly
    68.             motorBack.motorSpeed = Mathf.Clamp(motorBack.motorSpeed -(dir*accelerationRate - gravity*Mathf.Sin((slope * Mathf.PI)/180)*80 )*Time.deltaTime, maxFwdSpeed, maxBwdSpeed);
    69.         //if no input and car is moving forward or no input and car is stagnant and is on an inclined plane with negative slope
    70.         if((dir==0 && motorBack.motorSpeed < 0 ) ||(dir==0 && motorBack.motorSpeed==0 && slope < 0)){
    71.             //decelerate the car while adding the speed if the car is on an inclined plane
    72.             motorBack.motorSpeed = Mathf.Clamp(motorBack.motorSpeed - (decelerationRate - gravity*Mathf.Sin((slope * Mathf.PI)/180)*80)*Time.deltaTime, maxFwdSpeed, 0);
    73.         }
    74.         //if no input and car is moving backward or no input and car is stagnant and is on an inclined plane with positive slope
    75.         else if((dir==0 && motorBack.motorSpeed > 0 )||(dir==0 && motorBack.motorSpeed==0 && slope > 0)){
    76.             //decelerate the car while adding the speed if the car is on an inclined plane
    77.             motorBack.motorSpeed = Mathf.Clamp(motorBack.motorSpeed -(-decelerationRate - gravity*Mathf.Sin((slope * Mathf.PI)/180)*80)*Time.deltaTime, 0, maxBwdSpeed);
    78.         }
    79.      
    80.      
    81.      
    82.         //apply brakes to the car
    83.         if (Input.GetKey(KeyCode.Space) && motorBack.motorSpeed > 0){
    84.             motorBack.motorSpeed = Mathf.Clamp(motorBack.motorSpeed - brakeSpeed*Time.deltaTime, 0, maxBwdSpeed);
    85.         }
    86.         else if(Input.GetKey(KeyCode.Space) && motorBack.motorSpeed < 0){
    87.             motorBack.motorSpeed = Mathf.Clamp(motorBack.motorSpeed + brakeSpeed*Time.deltaTime, maxFwdSpeed, 0);
    88.         }
    89.         //connect the motor to the joint
    90.         wheelJoints[0].motor = motorBack;
    91.      
    92.     }
    93.  
    94. }
     
  4. Darokar

    Darokar

    Joined:
    Aug 22, 2015
    Posts:
    5