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

Vehicle wont turn

Discussion in 'Scripting' started by darugylsweater, May 13, 2015.

  1. darugylsweater

    darugylsweater

    Joined:
    Oct 26, 2013
    Posts:
    17
    I am creating a hover racer game and we have almost everything working but there is one major problem, the player cant turn left and right. I know the problem has something to do with the script that aligns the player to the angle of the track but I don't know what part is causing it. Here is the script
    Code (JavaScript):
    1. var smooth = 10;
    2.  
    3. function FixedUpdate()
    4. {
    5. //make platform adjust terrain rotation
    6. var rcHit : RaycastHit;
    7. //Make raycast direction down
    8. var theRay : Vector3 = transform.TransformDirection(Vector3.down);
    9. if (Physics.Raycast(transform.position, theRay, rcHit))
    10. {
    11.      //this is for getting distance from object to the ground
    12.      var GroundDis = rcHit.distance;
    13.      //get rotation
    14.      targetRotation = Quaternion.FromToRotation(Vector3.up, rcHit.normal);
    15.    
    16.      //Smooth rotation
    17.      transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime * smooth);
    18. }
    19. }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    there's no player input in that script... ?

    It's a fixedupdate so there shouldn't be really... but how is the player input handled?
     
  3. darugylsweater

    darugylsweater

    Joined:
    Oct 26, 2013
    Posts:
    17
    Player input is in another script, turning is done with AddTorque. All the other movement is done with AddForce.
     
  4. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    968
    If turning is done with AddTorque, then you shouldn't be messing with the transform.rotation
     
  5. darugylsweater

    darugylsweater

    Joined:
    Oct 26, 2013
    Posts:
    17
    The transform.rotation is to align it to the track, anything else doesn't work well
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    if you update the rotation you are overriding the updates done by the physics engine with the torque...
     
  7. darugylsweater

    darugylsweater

    Joined:
    Oct 26, 2013
    Posts:
    17
    so how would i fix this
     
  8. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    968
    You need to use a torque to align it to the track as well