Home > Software design >  Rotate Prefab based on camera direction
Rotate Prefab based on camera direction

Time:01-13

I'm making a Rollercoaster-Tycoon clone in Unity, and have run into the issue of how to position tracks. I want the track to have an offset from the last placed track based on the direction of the camera. I tried using a Vector3 to offset the track, but that vector would only work for that specific direction. I need it to offset the track based on the direction the player is building in. (The camera's direction) What is a quick and simple fix for this issue?

CodePudding user response:

You can try to use the Quaternion.LookRotation method to determine the rotation of the camera, and apply that to the new track piece.

Vector3 offset = camera.transform.forward * offsetDistance;
Vector3 newTrackPosition = lastTrackPosition   offset;
Quaternion rotation = Quaternion.LookRotation(camera.transform.forward);
newTrack.transform.position = newTrackPosition;
newTrack.transform.rotation = rotation;
  • Related