Search Unity

Quaternion Rotation and me. I love you.

Discussion in 'Scripting' started by jimmyhall2711, Jan 24, 2015.

  1. jimmyhall2711

    jimmyhall2711

    Joined:
    May 17, 2014
    Posts:
    1
    I wish to rotate an object locally 90 degrees from its original direction, and all future rotation
    take this into account when changing direction. I want it to act like the left side of the object is the front. Thanks in advance, I'm not the brightest light bulb. I will make my game though!! :D
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LookAtTarget : MonoBehaviour
    5. {
    6.     private GameObject target;
    7.     private Vector3 targetPoint;
    8.     private Quaternion targetRotation;
    9.  
    10.     void Start ()
    11.     {
    12.         target = GameObject.FindWithTag("Player");
    13.         //transform.forward = (new Vector3(90,90, 90));
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         targetPoint = new Vector3(target.transform.position.x, transform.position.y, target.transform.position.z) - transform.position;
    19.         targetRotation = Quaternion.LookRotation (-targetPoint, Vector3.up);
    20.         transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, Time.deltaTime * 10.0f/*2.0f*/) * (Vector3.left);
    21.         /*
    22.         * The above code turn the object this script is attachted to towards the player.
    23.         * It works, but the object is not aligned foward properly(Blender imported object)
    24.         * I'm trying to rotate the object an addition 90 degrees left(Local) below.
    25.         * It doesn't work at all can't convert vector3 to Quaternion
    26.         */
    27.  
    28.         //transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y + 90, transform.rotation.z);
    29.  
    30.        
    31.     }
    32. }
     
    Last edited: Jan 25, 2015
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Use code tags.
     
    cmcpasserby likes this.
  3. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I think you can just multiply the transform rotation:
    You have to change your target rotation:
    Quaternion tRotation = target.transform.rotation*Quaternion.Euler(0,90,0);
    Then replace target rotation with that in the slerp.