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

Rotation isn't working right.

Discussion in 'Scripting' started by Epic-Username, Jun 14, 2015.

  1. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    I cannot rotate a object towards another i have tried:
    transform.LookAt (target.transform.position); this makes the object move extremely slow and sometimes moves backwards.
    transform.Rotate(target.transform.position); which just spins it like crazy and if i set x and z to 0 it just spins around
    and
    transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.Euler(0, target.transform.position.y, 0),Time.deltaTime * 5); and yet again doesn't work
    can you please help me with this problem.

    Here is my code.
    Code (CSharp):
    1.  
    2. void Move(){
    3.         transform.Translate ((Vector3.forward * (1 * size) / 2) * Time.deltaTime);
    4.  
    5.         if (freemove == true) {
    6.             counter++;
    7.             if (counter >= 100) {
    8.                 direction = Random.Range (-1, 2);
    9.                 counter = 0;
    10.             }
    11.             if (direction == 0)
    12.                 transform.Rotate (Vector3.up);
    13.             if (direction == -1)
    14.                 transform.Rotate (Vector3.down);
    15.         } else if (freemove == false) {
    16.             //transform.LookAt (target.transform.position);
    17.             transform.Rotate(target.transform.position);
    18.             //transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.Euler(0, target.transform.position.y, 0),Time.deltaTime * 5);
    19.             Debug.Log(target.transform.position);
    20.         }
    21.     }
    22.  
     
  2. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    You're trying to rotate the object by a coordinate, which will not work. You're thinking of how to do this completely wrong.

    Let's say you have Object A, and Object B. You're rotating Object A on the X-axis by Object B's Y position. You're thinking it will make Object A look at Object B's height, but, in reality, you will just add the Y position of Object B to Object A's X-axis. So, if Object B is 1 unit up from the ground, then the X axis rotation will increase by 1 every time you rotate the object, which will cause it to spin.

    As for transform.LookAt, that should not make your object more slowly/backwards. Try using transform.LookAt(traget.transform) instead, that may work better.

    If you're confused on how to use it, read up on the documentation here.
     
  3. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    I figured out why its moving slowly or backwards, because it follows an enemy that's smaller then it, It tries to go down slightly which is causing it do drag slower. How do i make LookAt(); only apply to turning not looking down or up.
     
    Last edited: Jun 14, 2015
  4. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Just use the x and z (or y - depending on your use of axis) values of your target and the y value of the current gameobject itself. Like
    Code (CSharp):
    1. new Vector3(targetobject.transform.position.x, this.transform.position.y, targetobject.transform.position.z)
    This obv. only works if your terrain is flat, though.