Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Need to keep an object in front of the player facing toward the camera during movement

Discussion in 'Scripting' started by AxeyMcAxeStuff, Apr 2, 2024.

  1. AxeyMcAxeStuff


    May 27, 2017
    I'm trying to place a 3d object in front of the player (this is in a first person camera mode) and have that object remain facing the player when they move around. Ultimately the goal is for them to be able to write on the object in realtime when moving around but that's for later. Currently I'm trying to get the rotation to work properly but it's not quite right.

    To get the object in front of the player initially, I am using a Coroutine:

    Code (CSharp):
    1. IEnumerator BringPaperInFrontOfPlayer()
    2. {
    3.     if (Camera.main == null)
    4.     {
    5.         Debug.LogError("No main camera selected for player!");
    6.         yield return null;
    7.     }
    9.     var modelTransform = currentDrawingModel.transform;
    10.     var targetPos = playerController.DrawingStopPoint.position;
    13.     while (modelTransform.position != targetPos)
    14.     {
    15.         targetPos = playerController.DrawingStopPoint.position;
    17.         modelTransform.position = Vector3.MoveTowards(modelTransform.position, targetPos, Time.deltaTime * 2f);
    19.         yield return null;
    20.     }
    22.     modelTransform.rotation = Quaternion.LookRotation(Camera.main.transform.forward, currentDrawingModel.transform.up);
    26.     paperInFrontOfPlayer = true;
    27. }

    This brings the object in front of the player, although the rotation doesn't seem to be correct. I did check the clipboard model that I'm using to make sure the axes are correct and they seem to be (i.e. moving the object forward and backward from input moves it correctly along the blue forward axis). I thought that using the LookRotation with the camera forward and the model up would align them.

    Secondly I need to keep the object facing the player when they look around. In the LateUpdate function I'm also using LookRotation:

    Code (CSharp):
    1. currentDrawingModel.transform.rotation = Quaternion.LookRotation(Camera.main.transform.forward, currentDrawingModel.transform.up);
    I have a zipped video that I uploaded showing what is happening. It's definitely rotating with the player, but not correctly and I think the position needs to change to maintain the proper distance as the player looks around.

    I thought about Slerping (it looks like Slerping would match the desired rotation and position change?) but my understanding is that it's meant to be used when there is a need for change over time instead of immediately? Any help is appreciated, even if it's just resources that might be valuable.

    Attached Files:

  2. zulo3d


    Feb 18, 2023
    Try setting the rotation with this:
    Code (CSharp):
    1. modelTransform.rotation = Quaternion.LookRotation(Camera.main.transform.forward, Camera.main.transform.up);
  3. AxeyMcAxeStuff


    May 27, 2017

    That didn't work. I feel like it's so close though. I tried using RotateTowards and I think mechanically it's working, but the rotation of the object seems to have the .forward of the vector pointing toward the camera, and attempts to force the .up Vector point at the camera haven't worked so far.
  4. Nad_B


    Aug 1, 2021
    As a quick around just add a rotation on top of that to make it face the way you want... hey as long as it works!
  5. AxeyMcAxeStuff


    May 27, 2017
    Ok, I did get it working! I had to change the axes of the original object by making an empty object, setting it into the center of the object, then setting the new empty object as the parent of the old. I guess that means it WAS my axes in the first place? I have much to learn. Thanks folks.