- Home /
Where to put camera orbit around player script?
I have been searching around the forums and found a few codes to do what I'm asking for, but I don't have any idea on where to put the code. I've tried making a new script file and copying and pasting the code in there. But that doesn't work. Also I have tried going into the camera move scripts and using those but they don't orbit around the player. So I tried deleting everything in that script and pasting that script into there. That didn't work ether. And after all of this I checked to make sure the programming language was all the same of what type of script document I made. If anyone knows why this is could you tell me what I'm doing wrong?
I am trying to use this code:
using UnityEngine;
using System.Collections;
public class Orbit : $$anonymous$$onoBehaviour {
public float turnSpeed = 4.0f;
public Transform player;
public float height = 1f;
public float distance = 2f;
private Vector3 offsetX;
private Vector3 offsetY;
void Start () {
offsetX = new Vector3 (0, height, distance);
offsetY = new Vector3 (0, 0, distance);
}
void LateUpdate()
{
offsetX = Quaternion.AngleAxis (Input.GetAxis("$$anonymous$$ouse Y") * turnSpeed, Vector3.up) * offsetX;
offsetY = Quaternion.AngleAxis (Input.GetAxis("$$anonymous$$ouse Y") * turnSpeed, Vector3.right) * offsetY;
transform.position = player.position + offsetX;
transform.LookAt(player.position);
}
}
So, is this script attached to your camera object? Is the player Transform variable assigned correctly? Does it throw any errors during runtime? What happens when you run the game and move the mouse? Pleaase provide more info.
The error it gives me is this:
UnassignedReferenceException: The variable player of Camera_Orbit has not been assigned. You probably need to assign the player variable of the Camera_Orbit script in the inspector.
The script is attached to the camera, I don't know what the player transform variable is and nothing happens when I move the mouse.
Thank you for your help so far :)
You should have 2 objects the camera and player objects. The script is attached to the camera object. The script has a player variable that you have to set to the player object. Select the camera object so you can see where you have to drag the player object in the inspector. Then drag the player object from the hierarchy to the player variable (in the inspector).
THAN$$anonymous$$ YOU SO $$anonymous$$UCH! :D IT WOR$$anonymous$$ED!
Answer by Cherno · Jun 10, 2015 at 08:39 PM
Ok.
This line:
public Transform player;
means that a variable of Type "Transform" with the name "player" is declared. This variable is meant to reference the Transform component of your player object. The variable will be used by the line
transform.LookAt(player.position);
. However, you never assigned the Transform component of the player object to the variable, so you get the error when the sxcript tries to access it. What you need to do is drag and drop the player object in your hierarchy over to the inspector window when you have the camera object selected. You will see a small field "player" where the orbiting script is. This will automatically assign the Transform component of the player object to the player variable.
Your answer
Follow this Question
Related Questions
Rotating camera around object up/down problem 0 Answers
3rd person player 0 Answers
Orbit Camera Zoom limit 1 Answer
Animation automatique personnage 0 Answers
Locking world rotation of child 1 Answer