- Home /
How to make camera position relative to a specific target.
i'm in the process of making a camera controller component for the player, the idea is that it stays fixed on the player and can be turned left and right with the Q and E keys, and zoomed in with the mouse scroll wheel, however, the script i have set up works in every way but one, the left and right part, see, the camera rotates perfectly fine, but for whatever reason, the camera flies out a massive distance from the ball unless the ball is at 0 on both the X and Z axis:
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
public Transform target;
public float distance;
public float lift;
public float XPosition;
public bool cameraTurnIsFixed = false;
void Update ()
{
transform.position = target.position + new Vector3(XPosition, lift, distance);
transform.LookAt(target);
if (Input.GetAxis("Mouse ScrollWheel") < 0 && distance >= -5)//Zoom out
{
distance -= 0.2f;
}
if (Input.GetAxis("Mouse ScrollWheel") > 0 && distance <= -2.5)//Zoom in
{
distance += 0.2f;
}
if (cameraTurnIsFixed == false)
{
if (Input.GetKey (KeyCode.Q))
{
transform.RotateAround(target.position, Vector3.up, 40 * Time.deltaTime);
distance = transform.position.z;
XPosition = transform.position.x;
}
if (Input.GetKey (KeyCode.E))
{
transform.RotateAround(target.position, -Vector3.up, 40 * Time.deltaTime);
distance = transform.position.z;
XPosition = transform.position.x;
}
}
}
}
to fix this, i need to make the xPosition and distance variables relative to the target (the player ball), however, i don't know how to do this to be honest, therefore i am asking here what i should do.
and please, don't mistake this as me asking to "make my script for me", i just need a nudge in the right direction, and in fact it would be more helpful for me because i would actually know how it works, just being handed a script with no explanation or informative comments doesn't exactly teach me anything.
cameraTurnIsFixed is a thing i have in place for when i don't want the player to run the camera in certain places.
the player is a rolling sphere, the camera is meant to be situated about 1.5 units above the sphere, to get a view from above to make the game feel more 3D, the distance is a short distance the camera is from the player, all the while, the player should be able to rotate the camera on the horizontal plane, almost like if it were a planet, and there was a sun around 1.5 units above the player.
monster hunter has a camera that functions like this too, though you can set the height of the camera, and the player isn't a sphere.
so can someone assist me?, perhaps point out some documentation and where to apply it to or something.
i still need help, i tried modifying the distance (the Y axis) along with the cameraTurning (X) but that didn't work either, i'm trying to get the camera to be turnable in a perfect circular orbit around the player on the horizontal plane.
am i looking at this the wrong way?.
i'm still in need of assistance, tried messing with rotations, no joy.
I haven't made a 3rd person cam from scratch (kudos for trying) but I do have, I$$anonymous$$HO, the best 3rd person mmo-style zoom/rotate/orbit camera script I've found to date posted here:
http://www.getyour411.com/unoob/player.html
Scroll down to the mmo-cam style camera- maybe looking at how someone else did it will help you adopt what you are wanting.
thanks to both of you, personally DajBuzis answer seems more helpful as the other page is a bit hard to follow, i'm trying it out right now, it isn't working, the distance and lift variables are resetting the camera position constantly.
this line right here does it:
transform.position = target.position + new Vector3(cameraTurning, lift, distance);
the whole script is like this:
using UnityEngine;
using System.Collections;
public class CameraController : $$anonymous$$onoBehaviour
{
public Transform target;
public float distance;
public float lift;
public bool cameraTurnIsFixed = false;
void Update ()
{
transform.position = target.position + new Vector3(0, lift, distance);
transform.LookAt (target);
if (Input.GetAxis("$$anonymous$$ouse ScrollWheel") < 0)
{
distance -= 0.2f;
}
if (Input.GetAxis("$$anonymous$$ouse ScrollWheel") > 0)
{
distance += 0.2f;
}
if (cameraTurnIsFixed == false)
{
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Q))
{
transform.RotateAround(target.position, Vector3.up, 40 * Time.deltaTime);
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.E))
{
transform.RotateAround(target.position, -Vector3.up, 40 * Time.deltaTime);
}
}
}
}
i'm not entirely sure how to fix it, as i need the camera to follow the player and stay a set distance above them, parenting wont work because the player is a sphere.
Answer by sumeetkhobare · Feb 10, 2014 at 08:13 PM
Hey, i figured the flaw in your code, i guess. The update function continuously asks your camera to point at the target, whereas it wants to look someplace else while staying at (0,lift,distance), right? Do something like this.
if(cameraTurnIsFixed == true)
transform.LookAt (target);
else
{
if (Input.GetKey (KeyCode.Q))
{
transform.RotateAround(target.position, Vector3.up, 40 * Time.deltaTime);
}
if (Input.GetKey (KeyCode.E))
{
transform.RotateAround(target.position, -Vector3.up, 40 * Time.deltaTime);
}
}
or if you want to like orbit your camera around your player while looking at it only, not anywhere else, you could do like this:
transform.position = target.position + target.up *lift - target.forward * distance;
transform.LookAt(target);
the distance vector is minus, because the camera will be behind the player, if you add them it will be in front of it looking at its face. Correct me if i am wrong, if not and this works, mark the question solved. :)
that code's a bit messed up, like a lot, even after clearing up the errors with the general placement of the {} things, the first set makes the camera turn rather than rotate around the player, and the second one just does crazy spinning shtiz.
i think there may have been some miscommunication here on my part, i didn't explain my code clearly enough, see, cameraTurnIsFixed is a thing i have in place for when i don't want the player to run the camera in certain places.
the player is a rolling sphere, the camera is meant to be situated about 1.5 units above the sphere, to get a view from above to make the game feel more 3D, the distance is a short distance the camera is from the player, all the while, the player should be able to rotate the camera on the horizontal plane, almost like if it were a planet, and there was a sun around 1.5 units above the player.
monster hunter has a camera that functions like this too, though you can set the height of the camera, and the player isn't a sphere.
however, i think i can make your thing work, i'll try setting up an empty gameobject above the player and make the camera rotate around THAT ins$$anonymous$$d, i think this will work, i'll come back and confirm this if it works, for now though, it's getting late on my side of the planet.
Yes, exactly as you said, Parent the player with some empty gameObject and then rotate the camera around it.. this way, the player can still roll and your camera wont do mad-eye rolls.. :)
PS: And I don't know why my code is co$$anonymous$$g in a single line. I write it in formatted fashion but it still comes in a single line, sometimes out of the code block.
hrm, i've attempted it, and i failed, i also made an attempt to use the empty object as a parent for the camera and rotate it in the hopes that the camera would move and think it was still in the same spot, but no such luck.
i honestly have no idea what i'm supposed to do to overcome this line:
transform.position = target.position + new Vector3(0, lift, distance);
this one line is the cause of my problems, turning the camera sort of works, it turns very slightly before being snapped back into position by this line.
i desperately need this, it's the very last thing i need for the game mechanics, and i can't get it to work.
Okay.. Follow this, sorry i $$anonymous$$Now JS, don't know c# much.
RollScript.js
function Update () {
var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
transform.Rotate(directionVector*5,Space.World);
}
CameraRotateScript.js
function Update () {
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Q))
transform.RotateAround(transform.position,Vector3.up,40*Time.deltaTime);
else if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.E))
transform.RotateAround(transform.position,Vector3.up,40*-Time.deltaTime);
}
I used two planes just to create sloping, nothing else. see if this works good. Works good for me.
this rotates the ball as well as the camera.
wait, i just thought of something, maybe i can have something like "cameraIsTurning == false" to make the camera set the position, i'm not confident about it, but it might just work.
Your answer
Follow this Question
Related Questions
Third Person Camera Control 2 Answers
How to spawn a player and control him in uScript (node scripting) 0 Answers
CCTV control 1 Answer
Changing the Camera Control 2 Answers