Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kenton84 · May 18, 2017 at 11:07 AM · rotationmovementplayerturning

How do I make a vehicle turn properly?

When I tried using a movement script my car turns 90 degrees instantly instead of slowly turning like a real car would.

Here is the code Im using for it

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
 [SerializeField] private Transform target; //created to attached object to
 
 public float rotSpeed = 1.0f;
 public float moveSpeed = 250.0f;
 public float jumpSpeed = 275.0f;
 public float gravity = -9.8f;
 public float terminalVel = -300.0f;
 public float minFallSpeed = -1.0f;
 
 private float verticalSpeed;
 private CharacterController _controller;
 private ControllerColliderHit contactCollider; //used to store raycast information
 
 private PlayerManager playerInfo; //creates a player profile to edit and add info
 private Controller playerController; //recieve inputs from a controller
 
 void Start()
 {
     verticalSpeed = minFallSpeed;
     _controller = GetComponent<CharacterController> ();
     playerInfo =  GameObject.FindGameObjectWithTag("GameManager").GetComponent<PlayerManager> (); //find tag to access scripts from other gameobjects
 }
 
 // Update is called once per frame
 void Update ()
 {
     //start @ 0,0,0 and add movement components progressively
     Vector3 movement = Vector3.zero;
 
     float horInput = Input.GetAxisRaw ("Horizontal");
     float verInput = Input.GetAxisRaw ("Vertical");
 
     if(horInput != 0 || verInput != 0)
     {
         //move while arrow keys are pressed
         movement.x = horInput; //change movespeed to playerInfo.currentSpeed
         movement.z = verInput; //how fast will the player move in that direction while holding the keys
         movement = Vector3.ClampMagnitude (movement, playerInfo.topSpeed); //limit diagonal movement   
 
         //create new Quat to temporarily store the new angle
         //print(transform.localEulerAngles.y); //angle that moves when player is turning
 
         Quaternion temp = target.rotation;
         target.eulerAngles = new Vector3 (0, target.eulerAngles.y, 0);
         movement = target.TransformDirection (movement);
         target.rotation = temp;  //save to replace original rotation
 
 
         //calculates a quat facing in that direction
         //transform.rotation = Quaternion.LookRotation (movement);
         Quaternion direction = Quaternion.LookRotation(movement);
         transform.rotation = Quaternion.Lerp (transform.rotation, direction, rotSpeed * Time.deltaTime);
     }
 
     //if I hold down space
     if(Input.GetKey(KeyCode.Space) || Input.GetKey(KeyCode.Joystick1Button0) )
     {
         brake ();
     }
 
     //if I let go of the brake button
     if(!Input.GetKey(KeyCode.Space) || !Input.GetKey(KeyCode.Joystick1Button0))
     {
         //movement speed goes first to prevent speed from stopping at 1 due to update function
         movementSpeed (); //increases speed when not braking
         playerInfo.hasStopped = false;
     }
 
     movement *= Time.deltaTime;
     _controller.Move (movement);
 
     //move forward
     movement.z = playerInfo.currentSpeed;
     movement *= Time.deltaTime;
     transform.Translate(movement);
 
 }
 
 void OnControllerCollidorHit(ControllerColliderHit hit)
 {
     contactCollider = hit;
 }
 
 
 
 void movementSpeed()
 {
 
     //player hasnt reached max speed
     if(playerInfo.reachedMax != true && playerInfo.hasStopped != true)
     {
         playerInfo.currentSpeed += 1; //if not accelerate
     }
 
     //if at maxSpeed stop increasing
     if(playerInfo.currentSpeed == playerInfo.topSpeed)
     {
         playerInfo.reachedMax = true;
     }
 
     //checks to change reachedMax incase of item pickup
     else
     {
         playerInfo.reachedMax = false; 
         playerInfo.hasStopped = true;
     }
 }
 
 //lower speed until 0
 void brake()
 {
     //while slowing down to 0
     if(playerInfo.currentSpeed >= 0)
     {
         //lower by 10 every frame?
         playerInfo.currentSpeed -= 10;
         playerInfo.hasStopped = false;
     }
 
     //keep current speed at 0 to prevent negative speed
     if(playerInfo.currentSpeed <= 0)
     {
         playerInfo.currentSpeed = 0;
         playerInfo.hasStopped = true;
     }
 
 }
 
 
 //doesnt revert speed back to current
 void boost() //boost boost boost boost
 {
     //create temp to revert boost back
     float temp = playerInfo.currentSpeed;
 
     if(Input.GetKeyDown(KeyCode.LeftShift))
     {
         playerInfo.currentSpeed *= 1.5f; //since its private it cant change values - going to public set should fix
     }
 
     if(Input.GetKeyUp(KeyCode.LeftShift))
     {
         playerInfo.currentSpeed = temp;
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Sloth1998 · Apr 12, 2018 at 10:23 AM

You could try using Quaternion.Slerp function to smooth out the process of turning your car. https://docs.unity3d.com/ScriptReference/Quaternion.Slerp.html It basically requires two rotation values which are stored in the form of Quaternion and one float value, One is your current rotation state and the other is the rotation state you want to acquire. The float value is used to define the amount of smoothing required when transitioning from one state to another.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rotation for movement cannot be set in script? 1 Answer

Checking GameObject Position on Andriod 1 Answer

How to create a player movement with Rotation? 1 Answer

How to move as well as rotate the object to face the direction it is moving. 0 Answers

Player Move-Rotation 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges