- Home /
How do I follow a gameobject with the camera
I need to follow a gameobject, only on the y axis. I have tried multiple other soloutions, none of which worked for my purpose. Here is the outline of what I want to to do: Rotate Left/Right with Q/E Respectively; Stay at a set offset distance away from the player. Smoothly rotate in a circle around the player.
Here is my current code:`using UnityEngine; using System.Collections;
public class CameraBehaviourScript : MonoBehaviour { public GameObject Player; private Vector3 offset;
void Start ()
{
offset = transform.position;
}
void Update()
{
}
void LateUpdate ()
{
transform.position = Player.transform.position + offset;
transform.LookAt(Player.transform);
//transform.rotation.Set(0, Player.transform.rotation.y, 0, 0);
}
} `
My way of doing this before, would be to set the camera position based on the rotation keys, but I cant seem to find a way to make it work.
PS. Is there a way for me to do something like this?: transform.rotation.y = Player.transform.rotation.y;
Let me see if I understand the question correctly.
You want the camera to be behind the player, looking at the player, but when you want to rotate the camera, the camera itself is just moving in an arc behind the player whilst it is looking at the player, which is independant of the player's rotation?
Answer by Hoeloe · Sep 10, 2013 at 09:52 PM
Your method is fine, you're just using the wrong values. Rotations are not stored as (x rotation, y rotation, z rotation). They are stored as Quaternions, so trying to take just the y component to keep the y axis rotation will not give the expected result. Instead, Quaternions have a EulerAngles
method, which is used to get the Euler rotation (that is, (x,y,z) rotation), rather than the Quaternion values. By taking the y component of the Euler angles only, you can ensure that it remains fixed on the other two axes.
As to your other question, no, you can't set only one component of the rotation. Not only would you again be setting the Quaternion value directly (which as I explained is a bad idea), but Unity uses properties to reference its fields, in order to return a copy of the data. Modifying that is nonsensical, as it isn't really stored anywhere (except temporarily), and as it's a copy of the original data, wouldn't change anything anyway.
Thank you a ton :D I'm assu$$anonymous$$g you are referencing my commented out code?
I am indeed. Whenever you have a problem with something not doing what you expect in future, take a look at the Unity documentation first, because most of the time the problem will simply be that something isn't quite what you thought it was.
This overall does not solve my problem :| Is there a way, using the method you described above, to rotate like I originally outlined?
Ah, you want it to rotate around the player, staying a set distance away? That's a slightly different problem, but one you can solve fairly easily. You can just use the "LookAt" code for that, the trick is in the position of the camera, rather than the rotation. You can rotate a Vector3 by a Quaternion, by multiplying the Quaternion by the Vector. So, what you actually want to do is change your offset value! When you "rotate" the camera, you don't actually change the rotation, you rotate the offset vector (using Quaternion.AngleAxis
). Then you just set the camera position to the player position plus the new offset, and make it look at the player. That should do what you need.
I don't fully understand what you are saying. Do you mean I would multiple the offset v3 by another quaternion variable using ToAngleAxis() ? Is there a code snippit you could show me for an example
Answer by coward · Sep 10, 2013 at 10:14 PM
Use the smooth look at script from Unity, or even the third person controller has a script for the camera, allowing you to adjust height and distance from the character, as well as other various properties.
There are also a number of scripts here that may be useful (including a smoothfollow c# version)
This works very well, the only problem is that my "Player" is a sphere, stopping movement on the 2 axis seems not to work as well as I want, what do you recommend? Thank you :D
Great - I also realised I said smooth look at, but smooth follow is probably the one you want. Time for caffeine...
I was using SmoothFollow, and I had the problem above. Alternatively, what could work, as this rotates on all axis?
I just tried the smoothlook script, it has the same issue.
Did you look at smoothfollowadvanced in the link? I says it takes input and extends the bump one which stops rotation when it hits walls
Answer by Developer1212 · Aug 04, 2015 at 04:12 AM
You really don't need all of this , To make the camera follow a player you make the camera the gameobject's child . This should also work for the rotation
This doesn't work well, I've tried this before and it apart from the fact that the camera object will copy the GameObjects movement (In this case it's a ball object) So the camera will be rotating forwards along with the GameObject. I've been trying to do something similar myself for a weeks to no avail so far.
Your answer
![](https://koobas.hobune.stream/wayback/20220613113929im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
fixed rotation around object 1 Answer
rotate to swip direction 0 Answers
C# Rotate GameObject at Other Point other than Center 3 Answers