4

(For Unity 5.3.5f1) Right now I am working on a 3D camera that is orbiting horizontally around the player. The player is a RigidBody rolling sphere. The player can freely rotate the axis horizontally but when there is no input I want the rotation to reset back to the direction of the velocity.

Right now all I need to know is how to situate the camera behind the player's direction of velocity by rotating the camera around the player from its previous rotation.

Here is a crude drawing of what I am looking for: enter image description here

Currently to update the camera to orbit around the player I use this script on the camera (with comments):

 using UnityEngine;
 using System.Collections;

 public class example : MonoBehaviour {

     public GameObject Player;
     //I assume you would use this PlayerRB to find data regarding the movement or velocity of the player.
     //public Rigidbody PlayerRB;

     private float moveHorizontalCamera;
     private Vector3 offset;

     // Use this for initialization
     void Start ()
     {   //the offset of the camera to the player
         offset = new Vector3(Player.transform.position.x - transform.position.x,  transform.position.y - Player.transform.position.y, transform.position.z - Player.transform.position.z);
     }

     // Update is called once per frame
     void Update ()
     {
         moveHorizontalCamera = Input.GetAxis("Joy X"); //"Joy X" is my right joystick moving left, none, or right resulting in -1, 0, or 1 respectively

         //Copied this part from the web, so I half understand what it does.
         offset = Quaternion.AngleAxis(moveHorizontalCamera, Vector3.up) * offset;
         transform.position = Player.transform.position + offset;
         transform.LookAt(Player.transform.position);
     }
 }

Any help at all would be great!

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
  • https://www.youtube.com/watch?v=Q1xZGt41N80 –  Jun 08 '16 at 16:59
  • to explain the last 3 lines of you code (as you say you only understand it partly): the line with `Quaternion.AngleAxis * offset` rotates the vector offset by moveHorizontalCamera degree around the y-axis. the next line translates the transforms position to playerposition + offset (the rotated offset vector) and the last line rotates the transform such that transform.forward is pointing toward the player – nyro_0 Jun 08 '16 at 17:01

1 Answers1

0

I would suggest you use Vector3.RotateTowards (https://docs.unity3d.com/ScriptReference/Vector3.RotateTowards.html)

You take the Vector that is currently pointing from the Player to the camera (transform.position - Player.transform.position) and rotate it towards -Player.GetComponent<Rigidbody>().velocity.normalized * desiredDistance (the vector pointing in the opposite direction of the players velocity with magnitude corresponding the desired distance of the camera to the player). You can then use Vector3.RotateTowards to rotate the camera (smoothly or immediately) around the Player by setting the cameras position accordingly.

Something like:

transform.position = Player.transform.position + Vector3.RotateTowards(transform.position - Player.transform.position, -Player.GetComponent<Rigidbody>().velocity.normalized * desiredDistance, step, .0f);

where step if the angle update in radians. (Note: you should avoid calling GetComponent<Rigidbody>() every Update. You are better off storing it somewhere)

I hope I understood your question correctly and this helps.

nyro_0
  • 1,135
  • 1
  • 7
  • 9
  • This is great! But is there anyway I could set the desiredDistance to my original offset and my original rotation? I also need it locked on the y-axis, not rotating above or below the player, only around it as like a 2d circular motion. Update of my code so far, I think I am using it right (@ line 45): (http://pastebin.com/b30HXdXB). I also desire acceleration but I think that is already enough for one comment. – will rabbermann Jun 08 '16 at 20:29
  • to set the desiredDistance to your original offsets distance just set its value to `desiredDistance = offset.magnitude`. you can lock the cameras y-position to the players y-position just by setting it equal after rotating: `transform.position.y = Player.transform.position.y` (although this should not be necessary as long as the players velocity is parallel to the y-axis) – nyro_0 Jun 09 '16 at 07:02