Search Unity

Good wheels keep on turnin' (problem...help!)

Discussion in 'Scripting' started by Jeepster, Apr 18, 2019.

  1. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    Hi,

    I've asked for help about this before but to no avail unfortunately. I've got a child object that rotates with AddTorque on the Y-axis and the parent has a mouselook script on it using horisontal and vertical rotation input. The childs rotation is affected by the parents in such a way that it starts to rotate in all kinds of axis even though it's rigidbody is frozen except for y axis.

    Here'smy code for the rotation of the child:
    Code (CSharp):
    1.   void Awake()
    2.     {
    3.         gO = GetComponent<Rigidbody>();
    4.         gO.angularDrag = Random.Range(0f, 1.5f);
    5.         Vector3 torque;
    6.         torque.y = Random.Range(-200, 200);
    7.         //gO.AddRelativeTorque(0f, 2000f, 0f, ForceMode.Impulse);
    8.         gO.AddTorque(Vector3.left * torque.y, ForceMode.Impulse);
    9.      
    10.     }
    And it works great unparented. And here's the parents mouselook code:
    Code (CSharp):
    1.        rotationX += Input.GetAxis("Mouse X") * lookSpeed;
    2.             rotationY += Input.GetAxis("Mouse Y") * lookSpeed;
    3.  
    4.             transform.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
    5.             transform.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);
    I've been told to compensate for the parents rotation through script and the example was this code:
    Code (CSharp):
    1. private void LateUpdate(){
    2.     wheel.transform.rotation = Quaternion.euler(Mathf.Sin(Mathf.Deg2Rad * Time.time), 0, 0);
    3. }
    But it also eliminates the child's "AddTorque" rotation. So it does compensate for the parents movements, but is there any way to counter the parents mouselook and still have the child object rotate with AddTorque? and if not, how do I make this work ?? I've been stuck on this for a while and I'd reaaally love som help. Thank you in advance
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    Forget about that for a moment.

    I assume that you want the spinning object parented to the camera object because you want to have that spinning object to stay in the same location and orientation relative to the view. Is that right? If so, rather than adding torque to Vector3.left, have you tried simply adding torque to the object's transform.left instead?
     
    Jeepster likes this.
  3. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    You're absolutely right and no I haven't tried transform.left, but now that I've tried to write the code including the random range for the impulse, I can't get it to work. gO.AddTorque(transform.up* torque.y, ForceMode.Impulse); doesn't work. It only works if I don't move the mouse at all, and even then it's off-kilter. Do I have to change something in the lines before i.e.:
    1. Vector3 torque;
    2. torque.y = Random.Range(-200, 200);
     
  4. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    I have now tried simplifying it to just gO.AddTorque(transform.up * torque, ForceMode.Force ); and it doesn't work. As soon as I look sideways, the wheel spins in the wrong directions. Do you have any Idea how I can make this work?