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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Local rotation along local Y-axis towards point in world space [Editor Viewport]

Discussion in 'Scripting' started by Gustav_Bok, May 14, 2018.

  1. Gustav_Bok

    Gustav_Bok

    Joined:
    Dec 6, 2012
    Posts:
    35
    Hi people!

    I've tried to solve this problem all week and I think I need the help of the hivemind.

    This is inside a custom editor script and the OnScene() Method is used for regular updates.

    I have a gameobject with it's up-vector set to the normal of the ground below it.

    I have another point in world space that I would like for this gameobject to LookAt, but at the same time conform with the original up-vector, the surface normal.

    I've tried to create a plane at the location with the normal of the gameobject's up-vector to see if I can create a point and for the LookAt. For some reason I get really wrong

    Code (CSharp):
    1.  
    2.                  Plane m_Plane = new Plane(spawnedObject.transform.up, spawnedObject.transform.position);
    3.                  DrawPlane(spawnedObject.transform.position, spawnedObject.transform.up);
    4.  
    5.                  Ray ray = Camera.current.ScreenPointToRay(Input.mousePosition);
    6.  
    7.                  float enter = 0.0f;
    8.                  if (m_Plane.Raycast(ray, out enter))
    9.                  {
    10.                      Vector3 hitPoint = ray.GetPoint(enter);
    11.                      Debug.Log("Hit plane! " + hitPoint);
    12.                      Handles.color = Color.red;
    13.                      Handles.DrawLine(spawnedObject.transform.position, hitPoint);
    14.                      spawnedObject.transform.LookAt(spawnedObject.transform.up, hitPoint);
    15.                  }
    16.                 }
    If you have any ideas of how to solve this I would be incredible happy for your time!

    Sincerely,
    Gustav
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
    It looks like you're using 'LookAt' wrong - you should be passing the position of the object you want to look at (i.e. hitPoint) followed by the up vector (spawnedObject.transform.up). You've got them the wrong way around I think?
     
  3. Gustav_Bok

    Gustav_Bok

    Joined:
    Dec 6, 2012
    Posts:
    35
    Hi tonemcbride! Thanks for lending your brain.
    Switching the values didn't help, the problem seems to be that wherever I do the plane.Raycast from it always returns the same value in hitPoint, even though i move the mouse to different locations. Something might be wrong with the ray intersection.
     
  4. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
    ScreenPointToRay will always give the same start position (pretty much) as the actual screen is tiny (think of it like an eye in the scene representing the camera). For example, in this image http://www.falloutsoftware.com/tutorials/gl/perspective-transform-visual-diagram-opengl-3d-to-2d.png you can see how moving the mouse won't affect the start point.

    All that's really changing is the direction of the ray (i.e. if you click the top-left of the screen the ray will start from the camera centre but move outwards to the top-left)