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:
IEnumerator BringPaperInFrontOfPlayer()
{
if (Camera.main == null)
{
Debug.LogError("No main camera selected for player!");
yield return null;
}
var modelTransform = currentDrawingModel.transform;
var targetPos = playerController.DrawingStopPoint.position;
while (modelTransform.position != targetPos)
{
targetPos = playerController.DrawingStopPoint.position;
modelTransform.position = Vector3.MoveTowards(modelTransform.position, targetPos, Time.deltaTime * 2f);
yield return null;
}
modelTransform.rotation = Quaternion.LookRotation(Camera.main.transform.forward, currentDrawingModel.transform.up);
paperInFrontOfPlayer = true;
}
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:
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.
9745786–1394593–2024-04-02 13-20-22.zip (4.71 MB)