- Home /
How to make a GTA camera orbit
I am researching about TPS cameras . If i use LookAt(target), player stands in the middle of view and crosshair on it. GTA5, Just Cause 2 have nice cameras. Player model stands on bit left side. You can rotate around them with camera and crosshair is in the middle. How can i put player on left side in any amount with a variable?
Vector3 cameraOrientationVector= new Vector3(1,0,-2);
public float crosshairSize;
public Transform target;
public Texture2D crosshairImage;
public float turnSpeed=5f;
private Vector3 offsetX;
private Vector3 offsetY;
void Start ()
{
offsetX = new Vector3 (10, 0, -2);
offsetY = new Vector3 (10, 0, -2);
}
void OnGUI()
{
float xMin = (Screen.width / 2) - (crosshairImage.width / 2);
float yMin = (Screen.height / 2) - (crosshairImage.height / 2);
Rect position = new Rect((Screen.width - crosshairSize) * 0.5f, (Screen.height - crosshairSize) * 0.5f, crosshairSize, crosshairSize);
GUI.DrawTexture(new Rect(xMin, yMin, crosshairImage.width, crosshairImage.height), crosshairImage);
}
void LateUpdate ()
{
offsetX = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offsetX;
offsetY = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * turnSpeed, Vector3.right) * offsetY;
transform.position = target.position + offsetX;
transform.LookAt(target); // so crosshair on player
//transform.position = target.position + cameraOrientationVector;
}
For now i just moved crosshair to right. Still wondering how do they move lookAt(target) to right. If i find something, i will post here.
It depends on whether you are using an actual separate object or just storing things in variables. Either case involves vector math. If I have an actual object, I start by making that object face back to the camera to make its forward vector be pointing from it to the camera. Then you can make the object move some distance to the left via the left vector. I think Unity can get these vectors for you automatically via the transform component if you are using an object. If you are using variables it can be a little more complicated, but the same idea applies. You would just have to get your own version of the "left" vector.
Yup, understanding Vectors, how they can relate to each other, especially special things like cross/dot product, and then quaternions(not needed for this camera most likely except what Unity does internally) will get you far. Understanding the actual math behind the things isn't usually as important, but understanding for example that the dot product gives a value saying how "parallel" a pair of vectors are can help, for example if you want to use it to see if something sees something else, and you don't have understand how to get the dot product to learn how to use it.
Answer by kburkhart84 · Oct 16, 2014 at 10:42 PM
I'm no expert on cameras, but let me get you started.
Think logically what you want to do. If you look at the player directly, it will do exactly that, which isn't what you want. Instead, you want to look at something else, which can vary. So, create another object(or you can just store it in variables). This object could be either where the player is pointing, it could hang out to the right ear of the player avatar, or different things. Then, make your camera look at that instead. This concept is great as well for example if you want to change things, like near/far, or maybe have the view either get ahead of where the player is moving, or lag behind, you can adjust only the separate object and keep the camera looking at the same thing.
The trick to this is not so much the separate object, but the position of the camera. You would likely want to have some method of detecting what direction the player is looking, and modify that vector to give a sort of angle and position where you think the camera should be. Draw it out, write some simple vectors, see how they are related and then convert them to get what you want in code.
The last thing I should mention is that you should almost always lerp/tween your camera, both the position and the lookAt spot, in order to keep things smooth. The main exception to this is if you want an instant "look" at something somewhere else, and even then sometimes game designers want to make it smooth instead of instant.
Thanks for your guide. It will be very helpful on understanding other tps cam scripts.
How was this an answer at all? Anyway, look at the Camera.ViewportPointToRay function, use this and fire a raycast, then have your player rotate towards the rays direction. You could just use a regular camera with smooth-follow to achieve this effect if you toy with the settings.
Honestly, I didn't expect it to get accepted as the answer. $$anonymous$$y intention, considering the Davud appears to have some knowledge was to say how I would go about it logically, not write a script for him.
$$anonymous$$aybe I'm crazy, but understanding the logic of the situation will help more in the long run than just copypasta scripts....just sayin.
I found the code a week ago. It's just the way kburkhart84 has explained. Teddy W shared his unity project. You can find necessary codes from there.