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. Dismiss Notice

I want the wheel to spin still whilst using this script

Discussion in 'Scripting' started by JDVDeisgn, Jan 18, 2016.

  1. JDVDeisgn

    JDVDeisgn

    Joined:
    Oct 17, 2015
    Posts:
    148
    all my other wheels except the front two with this script on them

    Code (CSharp):
    1. ublic class rotate : MonoBehaviour {
    2.  
    3.     public float yRotation = 5f;
    4.  
    5.     // Use this for initialization
    6.     void Start () {
    7.  
    8.  
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update () {
    13.  
    14.        
    15.  
    16.         yRotation += Input.GetAxis("Horizontal");
    17.         transform.eulerAngles = new Vector3(0, 0, yRotation);
    18.  
    19.  
    20.     }
    21. }
    22.  
    I basically don't want it to affect the y axis mainly. I tried changing the y value to null but it won't let me do that
     
  2. HelpMe_Please

    HelpMe_Please

    Joined:
    Feb 15, 2013
    Posts:
    33
    I dont really understand what you want?
    Maybe this can help you
    transform.eulerAngles = new Vector3(0, 0, transform.rotation.y);
    or
    transform.eulerAngles = new Vector3(0, 0, transform.rotation.y+RotationSpeed)
     
  3. JDVDeisgn

    JDVDeisgn

    Joined:
    Oct 17, 2015
    Posts:
    148
    basically when I apply this script, the wheel physics from the rigidbody don't seem to apply anymore to it so it doesn't naturally spin. I basically wanna not specifiy a value for the y axis as it puts a lock on any spinning

    note: I am talking about natural spin on the y axis, I am ONLY changing the rotation to change the direction, which is a different axis to the wheel's natural roll
     
  4. HelpMe_Please

    HelpMe_Please

    Joined:
    Feb 15, 2013
    Posts:
    33
    Ah yes i got it:
    Try add rotation torque. I use this for planes rotation, But you can use this for wheels too. You might have to limit the max rotation tho.
    Video tutorial :https://unity3d.com/learn/tutorials/modules/beginner/physics/addtorque
    or
    something like
    Code (CSharp):
    1. public class AddTorqueExample : MonoBehaviour
    2. {
    3.     public float amount = 50f;
    4.  
    5.  
    6.     void FixedUpdate ()
    7.     {
    8.         float h = Input.GetAxis("Horizontal") * amount * Time.deltaTime;    
    9.         rigidbody.AddTorque(transform.up * h);
    10.  
    11.     }
    12. }