Search Unity

Question Problem with rotation

Discussion in 'Scripting' started by claudiok, Sep 26, 2022.

  1. claudiok

    claudiok

    Joined:
    Aug 16, 2022
    Posts:
    18
    I'm trying to make rotation in one object , using one point as reference. I would that the forward from my object is always the point of reference . Here is the code that i'm trying to make it work.

    compañero.transform.rotation = Quaternion.Slerp(compañero.transform.rotation, Quaternion.LookRotation(Luces.transform.position), 1f);

    Compañero is object that i'm trying to make rotate
    luces is reference point that i would the object look at
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    The first parameter of LookRotation needs to be a direction, not a position. You need to calculate the forward direction that you want. In this case, I'm guessing it should be:
    Code (csharp):
    1.  
    2. (Luces.transform.position -  compañero.transform.position).normalized
    3.  
    Basically, to get the direction, you just need to subtract the object's position from the target's position to get a vector. Then normalize that vector.

    Edit: For the purpose of LookRotation, I don't know whether it needs to be normalized to work.
     
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,008
    No, it does not need to be normalized. Though it doesn't really hurt on modern hardware ^^. In many cases you need the normalized direction for other things anyways.
     
  4. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    I believe it needs normalising if you want to compare the desired forward vector with the current forward vector. However if you are merely setting the forward vector to = -(positionA - positionB) it will result the correct direction.
     
  5. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    I stand Incorrected.

    Code (CSharp):
    1. if (Vector3.Angle(OBJECT.forward, DESIREDFORWARD) < BUFFER)
    At less than buffer you can go ahead and set the forward to the desired forward, Greater than buffer you can

    Code (CSharp):
    1. OBJECT.transform.forward += (OBJECT.transform.right * RATEOFTURN);
    Eventually you are within the buffer.

    here is example of results
     
    Last edited: Sep 26, 2022