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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Quarternion.LookRotation problem

Discussion in 'Scripting' started by pretender, Dec 7, 2013.

  1. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    862
    Hi, I am having trouble with, what I seemed, is the simple thing.

    I want my object to look straight at my target obviously :), so for a test i made a new scene, created cube and a cylinder, set them apart in the scene and attached this script to the cylinder (cylinder is a what is supposed to look at the target)

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RotationTest : MonoBehaviour
    5. {
    6.     public Transform target;
    7.    
    8.     void Update()
    9.     {
    10.         transform.rotation = Quaternion.LookRotation(target.position - transform.position);
    11.     }
    12. }
    I set the target in the inspector and run the player. By moving the cube cylinder rotates but its longer side (it seems that it is perpendicular to the cube). So i figured that i need somehow to tell unity what side i need to point at the target. i tried this

    Code (csharp):
    1. transform.rotation = Quaternion.LookRotation(target.position - transform.position,Vector3.up);
    and this
    Code (csharp):
    1. transform.rotation = Quaternion.LookRotation(target.position - transform.position,Vector3.left);
    but it does not work, nothing is changed.

    Check out the image about more details. And thanks! $forum_screenshot.png
     
  2. generalmcmutton

    generalmcmutton

    Joined:
    May 20, 2010
    Posts:
    42
    I imagine you've already figured a solution at this point, but I thought I'd offer a solution since looking at your code helped me fix an issue with my own.

    If you haven't tried setting the Up axis in LookRotation to Forward, as well, then the easiest thing to to would be to parent the cylinder to an empty gameobject, and then put the code onto that instead.Then you can rotate the cylinder to the correct orientation from within that gameobject.