Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Custom transform

Discussion in 'Getting Started' started by Attix, Mar 14, 2019.

  1. Attix

    Attix

    Joined:
    Sep 28, 2018
    Posts:
    4
    Hey, would it be possible to either:
    A: define a custom forward, up, and right vector
    or
    B: having a feature like "Apply position and rotation" allowing you in that way to change the forward, up, and right vector

    That way Transform.LookAt() would be much easier to use, knowing that I had already defined the vector that is pointing forward.

    The way I'm doing it now is with an empty game object and the object I want to use is rotated and offset inside that object to point the way I'd like. This feels hacky and unorganized and keeps my object visuals and my scripts on two objects instead of one.

    On the same note I'd like a Transform.LookDirection(Vector3 _direction).
    I've solved that by using Transform.LookAt(transform.position + direction), but again, this feels unclear.
     
  2. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    974
    This is something I use:

    Code (CSharp):
    1. public static Quaternion LookRotation(Vector3 worldForward, Vector3 worldUp, Vector3 localForwardAxis, Vector3 localUpAxis)
    2. {
    3.     //Set the rotation of the destination
    4.     Quaternion rotationA = Quaternion.LookRotation(worldForward, worldUp);
    5.  
    6.     //Set the rotation of the custom normal and up vectors.
    7.     //When using the default LookRotation function, this would be hard coded to the forward and up vector.
    8.     Quaternion rotationB = Quaternion.LookRotation(localForwardAxis, localUpAxis);
    9.  
    10.     //Calculate the rotation
    11.     return rotationA * Quaternion.Inverse(rotationB);
    12. }
     
  3. Attix

    Attix

    Joined:
    Sep 28, 2018
    Posts:
    4
    I still think it should just be a part of the engine. I'd really love that. It's a minor feature that would clear up a lot of my code and objects
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,612
    One simple thing that people do sometimes is create an empty parent object. Then rotate your original object so that it's front is pointing toward the parent object's positive Z direction.

    Then Transform.Forward, and such, will be accurate as long as you use your code is working with the empty parent's transform.
     
    Kiwasi and Deleted User like this.
  5. Attix

    Attix

    Joined:
    Sep 28, 2018
    Posts:
    4
    That is what I said I've done, I just think it's stupid and it adds completely useless objects and makes things unorganized. I don't want "hacks" to make things work. It should be very simple to just edit the forward, up, and right vectors and also an offset would be very useful, but no, it's not. I don't know how the Unity team has written their unerlying procedures, but it really shouldn't be hard to implement the features I requested. I'd do it myself if I could edit the Transform class