- Home /
Rotatearaound a moving object
Hi!
I am trying to create a simple camera that would follow the player and only allow orbital movement sideways around the player. I have two pieces of code. Both work flawlessly on their own.
One for orbiting around the player:
void FixedUpdate()
{
Transform PC = GameObject.FindWithTag("Player").transform;
float p88re = Input.GetAxis("Kylgsuund"); // identify button press
float kiiruskonstant = 2f; //fix orbiting speed
transform.RotateAround(PC.position, Vector3.up, kiiruskonstant * p88re); //turn camera
}
And the other for following the player:
{
public Transform target; // The position that that camera will be following.
public float smoothing = 5f; // The speed with which the camera will be following.
Vector3 offset; // The initial offset from the target.
void Start ()
{
// Calculate the initial offset.
offset = transform.position - target.position;
}
void FixedUpdate ()
{
// Create a postion the camera is aiming for based on the offset from the target.
Vector3 targetCamPos = target.position + offset;
// Smoothly interpolate between the camera's current position and it's target position.
transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime);
}
}
Which ever way I try to combine the two, the camera goes bananas, losing focus on the player and original rotation axis as soon as I move the character and try to rotate the camera. I just want the camera to follow the player, keeping the original distance and angle and moving around the player when axis movement is applied.
I've been stuck with this for two days now. Any and all pointers appreciated.
Answer by Glurth · Jun 14, 2017 at 03:54 PM
This issue with the first item is that when the player moves THEN you perform the rotate around. Consider: player is at 0,0 and camera is at 0,1 aiming at the player.
Now, the player moves to 3,0. THEN you rotate the camera around this point: note that the radius of this rotation is greater than 1 (the original distance of the camera from the player)
I would suggest you adjust (translational-offset) the position of the camera at the same time (and by the same amount) that you adjust the player position. This will keep the distance and direction between the two objects the same. THEN do your rotate around.
Thanks for the input, Glurth!
As the second script works to make the camera follow the player I did try to combine them together. I am only getting to know C# and Unity so there's probably some simple conflict here, but my attempts, such as the following do not give the desired result and ins$$anonymous$$d make the camera wander off completely.
{
public Transform target; // Playercharacter will have to be marked as target
public float smoothing = 5f; //To smooth camera following
Vector3 offset; // Creating a storage point for the original offset between player character and camera
void Start ()
{
offset = transform.position - target.position; //Deter$$anonymous$$e the actual offset between player character and camera
}
void FixedUpdate ()
{
Vector3 targetCamPos = target.position + offset; // This is where the camera is supposed to go to when the character has changed position
transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime); // Smoothly interpolate between the camera's current position and it's target position.
float p88re = Input.GetAxis("$$anonymous$$ylgsuund"); //and if A or D button is pressed
float kiiruskonstant = 2f; //fix orbiting speed
transform.RotateAround(PC.position, Vector3.up, kiiruskonstant * p88re); //in addition to following the player also turn the camera
}
}
I am guessing that RotateAround is messing up the offset I use for following the player, but I'm not sure how to prevent that.
Edited to make the code better to read
correct: the offset that you compute in start will no longer be valid after you rota$$anonymous$$roud.
I like to store a "lastCyle" variable for stuff like this:
Vector3 lastCyleTargetPosition;
void Start() {lastCyleTargetPosition=target.position;}
void Update()
{
Vector3 target$$anonymous$$ovement= target.position - lastCyleTargetPosition;
lastCyleTargetPosition=target.position;
transform.position += target$$anonymous$$ovement;
....
}
Thank you, Glurth! I got it!
Great to see a working community here at Unity Answers. Double thanks for explaining things ins$$anonymous$$d of just blurting out a correction.
Your answer
Follow this Question
Related Questions
"Follow Target" script with a prefab (C#) Help me please! 1 Answer
One of the functions on my Camera script doesn't execute correctly. 1 Answer
UNITY 3D: How to make the camera follow the player? Smoothly 2 Answers
delay "after mouseclick" 0 Answers
Problem with a Unity Scripting example from the scripting manual! 0 Answers