Search Unity

help. I wrote a unity player car script there is a problem. Line 78

Discussion in 'Scripting' started by MrJohnWeez, Nov 26, 2013.

  1. MrJohnWeez

    MrJohnWeez

    Joined:
    Aug 31, 2013
    Posts:
    11
    //car physics calculations stuff//
    private var accel : Vector3;
    public var throttle : float;
    private var deadZone : float = .001;
    private var myRight : Vector3;
    private var velo : Vector3;
    private var flatVelo : Vector3;
    private var relativeVelocity : Vector3;
    private var dir : Vector3;
    private var flatDir : Vector3;
    private var carUp : Vector3;
    private var carTransform : Transform;
    private var carRigidbody : Rigidbody;
    private var enginForce : Vector3;

    private var turnVec : Vector3;
    private var imp : Vector3;
    private var rev : float;
    private var actualTurn : float;
    private var carMass : float;
    private var wheelTransform : Transform[] = new Transform[4]; //the Transforms for the 4 wheels//
    public var actualGrip : float;
    public var horizontal : float; //key control mobile or keys//
    private var maxSpeedToTurn : float = .2; //keeps car from turning until reached this value//

    //the physical car transforms//
    public var frontLeftWheel : Transform;
    public var frontRightWheel : Transform;
    public var rearLeftWheel : Transform;
    public var rearRightWheel : Transform;

    //this controls the turning of the wheels//
    public var LFWheelTransform : Transform;
    public var FRWheelTransform : Transform;

    //car physics adjustments//
    public var power : float = 300;
    public var maxSpeed : float = 50;
    public var carGrip : float = 70;
    public var turnSpeed : float = 3.0;

    private var slideSpeed : float;
    public var mySpeed : float;

    private var carRight : Vector3;
    private var carFwd : Vector3;
    private var tempVEC : Vector3;

    function Start()
    {
    Initialize();
    }

    function Initialize()
    {

    carTransform = transform;

    carRigidbody = rigidbody;

    carUp = carTransform.up;

    carMass = rigidbody.mass;

    carFwd = Vector3.forward;

    carRight = Vector3.right;

    setUpWheels();

    //this turns the car center of gravity to negative so it does not flip over//
    carRigidbody.centerOfMass = Vector3(0,-0.7,.35);
    }

    function Update()
    {

    carPhysicsUpdate();


    checkInput();
    }

    function LateUpdate()
    {

    rotateVisualWheels();


    engineSound();
    }

    function setUpWheels()
    {
    if((null == frontLeftWheel || null == frontRightWheel || null == rearLeftWheel || null == rearRightWheel ))
    {
    Debug.LogError("one or more of the wheel transforms have not been pluged in on the car");
    Debug.Break();
    }
    else
    {
    //set up the transforms//
    wheelsTransform[0] = frontLeftWheel;
    wheelsTransform[2] = rearLeftWheel;
    wheelsTransform[1] = frontRightWheel;
    wheelsTransform[3] = rearRightWheel;
    }
    }

    private var rotationAmount : Vector3;

    function rotateVisualWheels()
    {

    LFWheelTransform.localEulerAngles.y = horizontal * 30;
    FRWheelTransform.localEulerAngles.y = horizontal * 30;

    rotationAmount = carRight * (relativeVelocity.z * 1.6 * Time.deltaTime * Mathf.Rad2Deg);

    wheelTransform[0].Rotate(rotationAmount);
    wheelTransform[1].Rotate(rotationAmount);
    wheelTransform[2].Rotate(rotationAmount);
    wheelTransform[3].Rotate(rotationAmount);
    }

    private var deviceAccelerometerSensitivity : float = 2;

    function checkImput()
    {

    if (Application.platform == RuntimePlatform.Android)
    {

    accel = Imput.acceleration * deviceAccelerometerSensitivity;

    if(accel.x > deadZone || accel.x < -deadZone) {
    horizontal = accel.x;
    }
    else
    {
    horizontal = 0;
    }
    throttle = 0;

    for (var touch : Touch in Input.touches) {
    if(touch.position.x < Screen.width -Screen.width/3 touch.position.y < Screen.height/3)
    {
    throttle = 1;
    }
    else if(touch.position.x < Screen.width/3 touch.position.y < Screen.height/3)
    {
    throttle = -1;
    }
    }
    }
    else if (Application.platform == RuntimePlatform.WindowsEditor || RuntimePlatform.WindowsWebPlayer) //unity editor//
    {
    //use the key board for all input//
    horizontal = Input.GetAxis("Horizontal");
    throttle = Input.GetAxis("Vertical");
    }
    }

    function CarPhysicsUpdate()
    {

    myRight = carTransform.right;


    velo = carRigidbody.velocity;

    tempVEC = Vector3(velo.x,0,velo.z);


    flatVelo = tempVEC;


    dir = transform.TransformDirection(carFwd);

    tempVEC = Vector3(dir.x,0,dir.z);


    flatDir = Vector3.Normalize(tempVEC);


    relativeVelocity = carTransform.InverseTransformDirection(flatVelo);


    slidSpeed = Vector3.Dot(myRight,flatVelo);


    mySpeed = flatVelo.magnitude;


    rev = Mathf.Sign(Vector3.Dot(flatVelo,flatDir));


    engineForce = ( flatDir * ( power * throttle ) * carMass);


    actualTurn = horizontal;


    if(rev < 0.1f)
    {
    actualTurn =- actualTurn;
    }


    turnVec = ((( carUp * turnSpeed ) * actualTurn ) * carMass )* 800;







    actualGrip = Mathf.Lerp(100, carGrip, mySpeed * 0.02);
    imp = myRight * ( -slideSpeed * carMass * actualGrip);

    }

    function slowVelocity ()
    {
    carRididbody.AddForce(-flatVelo * 0.025);
    }


    function engineSound ()
    {
    audio.pitch = 0.30 + mySpeed * 0.025;

    if (mySpeed > 30)
    {
    audio.pitch = 0.25 + mySpeed * 0.015;
    }
    if (mySpeed > 40)
    {
    audio.pitch = 0.20 + mySpeed * 0.013;
    }
    if (mySpeed > 49)
    {
    audio.pitch = 0.15 + mySpeed * 0.011;
    }

    if ( audio.pitch > 2.0 ) {
    audio.pitch = 2.0;
    }
    }

    function FixedUpdate()
    {
    if(mySpeed < maxSpeed)
    {

    carRididbody.AddForce( engineForce * Time.deltaTime );
    }

    if(mySpeed > maxSpeedToTurn)
    {

    carRididbody.AddTorque ( turnVec * Time.deltaTime );
    }
    else if(mySpeed < maxSpeedToTurn)
    {
    return;
    }

    carRididbody.AddForce( imp * Time.deltaTime );
    }
     

    Attached Files:

    • $Car.js
      File size:
      5.5 KB
      Views:
      786
  2. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Please use code tags properly and tell us what error you are getting. Just saying "there is a problem" and pasting code doesn't give anyone any kind of idea about what your problem is.
     
  3. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Post code tags and I'll give it a shot.
     
  4. TheRealMacklebee

    TheRealMacklebee

    Joined:
    Nov 28, 2012
    Posts:
    16
    if I had to guess without digging into the code and with no indication what the "problem" is, i would say its because you are calling for this function in the Update loop:
    but the actual function is named:
     
    Last edited: Nov 26, 2013
  5. MrJohnWeez

    MrJohnWeez

    Joined:
    Aug 31, 2013
    Posts:
    11
    sorry I am new to unity and I did not know what to do. Thanks for your help. :)
     
  6. MrJohnWeez

    MrJohnWeez

    Joined:
    Aug 31, 2013
    Posts:
    11
    Thanks a bunch. but I did the:

    function CarPhysicsUpdate()

    but when I hit jun it gave me this:

    Assets/535345.js(78,18): BCE0044: expecting (, found 'CarPhysicsUpdate'.

    I did research to try to find what that meant and found nothing. Could someone help me.
     
  7. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    378
    Thats not the only error :LOL: there is a ton of grammatic errors such as : Input - Imput, Rigidbody - Rididbody and so on, dont know where did he get this script but it just amaze me :D
    Script is ok but it needs some grammar check.
     
    Last edited: Nov 29, 2013
  8. Deleted User

    Deleted User

    Guest

    please edit your first post and put your code inside code tags ( see here for further instructions http://forum.unity3d.com/threads/143875-Using-code-tags-properly ) and also please reformat after doing so. unless you dont do that it is unlikely that someone will help you.

    Also without reading your code this error message is genarally caused by syntax error eg. missing ; or foretting a ( or { etc