Search Unity

Slow rotate for up axis not working (image attached).

Discussion in 'Scripting' started by rjdfhorn2006, Jun 5, 2016.

  1. rjdfhorn2006

    rjdfhorn2006

    Joined:
    Jun 14, 2010
    Posts:
    141
    I want my capsule's up axis to point towards an object. Here's my code:

    Code (CSharp):
    1.     void Update () {
    2.         Vector3 direction = cube.transform.position - transform.position;
    3.         Debug.DrawRay(transform.position, direction * 1000, Color.green);
    4.         Debug.DrawRay(transform.position, transform.up * 1000, Color.blue);
    5.  
    6.  
    7.         Quaternion toRotation = Quaternion.FromToRotation(transform.up, direction);
    8.         transform.rotation = Quaternion.Slerp(transform.rotation, toRotation, 5f * Time.deltaTime);
    9.     }
    'cube' is an object in the 3d world (see attached image). The green ray hows the direction the character's up axis should become aligned with while the blue ray shows the character's up axis direction. I cannot figure out why this code is not making the character's up axis point towards the cube. My capsule starts off lying down so its x rotation value is 90 starting out. Any ideas?
     

    Attached Files:

  2. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    I didn't test this in Unity myself, but you should propably normalize the vector first

    Code (CSharp):
    1. Vector3 direction = (cube.transform.position - transform.position).Normalized;
     
    rjdfhorn2006 likes this.
  3. rjdfhorn2006

    rjdfhorn2006

    Joined:
    Jun 14, 2010
    Posts:
    141
    @Zaflis Thanks - that fixed it.