Hi, I’m new to scripting and all, I got a camera that I want to stay at the distance away I have it set at in the editor to the player, but, I want to be able to use the left and right arrow keys to rotate it around the player, but, I’m using WASD to move and rotate the player. It’s really just experimenting and getting my hands dirty, but, this has stumped me. Here are my codes
PLAYER SCRIPT
var MoveAmount : int;
var InitialMoveAmount = MoveAmount;
var RotateAmount : int;
public static var PlayerLocation : Vector3;
function Update () {
PlayerLocation = gameObject.transform.position;
if (Input.GetKeyDown(KeyCode.LeftShift))
{
MoveAmount = MoveAmount*2;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
MoveAmount = InitialMoveAmount;
}
if (Input.GetKey(KeyCode.W)){
transform.Translate(Vector3.forward * MoveAmount * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S)){
transform.Translate(-Vector3.forward * MoveAmount * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A)){
transform.Rotate(Vector3.up * -RotateAmount * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D)){
transform.Rotate(Vector3.up * RotateAmount * Time.deltaTime);
}
}
Camera Script
var myTarget : Transform;
var MyPosition : Vector3;
function Start()
{
MyPosition = gameObject.transform.position;
}
function Update () {
transform.LookAt(myTarget);
//transform.position = MyPosition + PlayerMoveScript.PlayerLocation;
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.RotateAround(PlayerMoveScript.PlayerLocation,Vector3.up, 100 * Time.deltaTime);
}
if (Input.GetKey(KeyCode.RightArrow))
{
transform.RotateAround(PlayerMoveScript.PlayerLocation,Vector3.up, -100 * Time.deltaTime);
}
}
It works and rotates around the player, if I uncomment the line in the camera script, it’ll follow the player, but I cannot rotate it (because it’s obviously always making it’s position that of it’s relative position plus the player position. So… how could I work around this? Please don’t just add code and say “Here, use these scripts instead” I’d appreciate it if you went into a little detail on what the new things you added or changed did exactly! Thank you!