Search Unity

Camera Code simple Issue... javascript please help!

Discussion in 'Scripting' started by tallieke, Jul 2, 2015.

  1. tallieke

    tallieke

    Joined:
    Aug 17, 2014
    Posts:
    37
    Unity,Javascript!

    so heres the problem... I made a camera script for my racing game so the camera will follow the car normally in 3rd person view, the script also involves when the car speeds up it zooms out a little but i think that works well. also when the car reverses it is supposed to go infront of the car and look at it from reverse (like in gta5). this all works how its supposed to except for one problem. when i first run the scene the camera always starts out infront of the car instead of behind it! when i start moving forward it fixes but why is this problem happening and can I fix it. any help is appreciated thanks! :)

    Code (JavaScript):
    1. #pragma strict
    2. var car : Transform;
    3. var distance : float = 6.4;
    4. var height : float = 1.4;
    5. var rotationDamping : float = 3.0;
    6. var heightDamping : float = 2.0;
    7. var zoomRacio : float = 0.5;
    8. var DefaultFOV : float = 60;
    9. private var rotationVector : Vector3;
    10. function Start () {
    11.  
    12. }
    13.  
    14. function LateUpdate () {
    15. var wantedAngle = rotationVector.y;
    16. var wantedHeight = car.position.y + height;
    17. var myAngle = transform.eulerAngles.y;
    18. var myHeight = transform.position.y;
    19. myAngle = Mathf.LerpAngle(myAngle, wantedAngle,rotationDamping*Time.deltaTime);
    20. myHeight = Mathf.Lerp(myHeight,wantedHeight, heightDamping*Time.deltaTime);
    21. var currentRotation = Quaternion.Euler(0,myAngle,0);
    22. transform.position = car.position;
    23. transform.position -= currentRotation*Vector3.forward*distance;
    24. transform.position.y = myHeight;
    25. transform.LookAt(car);
    26. }
    27. function FixedUpdate () {
    28. var localVelocity = car.InverseTransformDirection(car.rigidbody.velocity);
    29. if (localVelocity.z<-0.5) {
    30. rotationVector.y = car.eulerAngles.y + 180;
    31. }
    32. else {
    33. rotationVector.y = car.eulerAngles.y;
    34. }
    35. var acc = car.rigidbody.velocity.magnitude;
    36. camera.fieldOfView = DefaultFOV + acc*zoomRacio;
    37.  
    38. }
     
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    First, sorry if I was kind of harsh, but I hope, you know, what I meant with staying in a thread for a script if the problem is in there. :) For this problem, could you make a screenshot or explain, where the camera is at the beginning of the scene before playing it? Either, your camera is already in front of the car before starting the game, or some variable is missing in your script above and gets its value first when you move your car.
     
  3. tallieke

    tallieke

    Joined:
    Aug 17, 2014
    Posts:
    37
    thansk for the help, there ill post a screenshot along with this but before the scene starts the camera is behind the car not infront and it instantly switches, so that rules that out. heres the

    screenshot

    im not sure if your able to see the picture or not please tell me
     
  4. tallieke

    tallieke

    Joined:
    Aug 17, 2014
    Posts:
    37
    there we go sorry
     

    Attached Files:

  5. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Could you comment out line 30 and see what happens?
     
  6. tallieke

    tallieke

    Joined:
    Aug 17, 2014
    Posts:
    37
    I tried that and it just made it worse, instead of the issue fixing when i start driving it just stayed in that view... any other ideas? :)
     
  7. tallieke

    tallieke

    Joined:
    Aug 17, 2014
    Posts:
    37
    found a solution!
    on line 29 i had a value of -0.5, i figured that probably had something to do with it so i changed it to positive 0.5 and it fixed it perfectly. i think i had it so my car was always moving backwards really slowly and that why when i stopped it also when back to reverse view. thanks for all the help anyways and I still need help with a few other problems! ;)
     
  8. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Thought of something like that being a wrong velocity or something on your car as your cam reacted right when driving forward, glad you made it :)