Method not able to see Vector3 axis
I am successfully (for the most part) trying to make my camera follow a player. I am using the Z axis like a Y axis. I have set a movement script to check the players position and then follow accordingly.. the script all works fine in the update function but once i put it in its own method the camera gos wonky.
void Update () {
Vector3 CharPos = GameObject.Find("Character").transform.position;
Vector3 RelevantPos = new Vector3 (this.transform.position.x, this.transform.position.y, (CharPos.z -8));
Vector3 RelevantRotation = new Vector3((-CharPos.z + 25f), this.transform.rotation.y, this.transform.rotation.z);
if (CharPos.z >= (-8.5f) && CharPos.z <= (4f)) {
FollowPlayer();
}
}
void FollowPlayer () {
this.transform.position = RelevantPos;
this.transform.eulerAngles = new Vector3 (RelevantRotation.x, transform.eulerAngles.y, transform.eulerAngles.z);
}
Answer by Darkomen43 · Dec 23, 2017 at 11:18 PM
I have fixed my issue, not sure if I have done so the best way and would still like input on the culprit but here it is working to pitch, pan, and zoom around the character. However this requires Vector3 CharPos = GameObject.Find in each method, Update, FollowingPlayer, and PanPlayer . Why it has to be in each method separately im not quite sure?
void Update () {
Vector3 CharPos = GameObject.Find("Character").transform.position; // Camera World Bounds
if (CharPos.z >= (-8.5f) && CharPos.z <= (4f)) { //These are just confining the player to the area
FollowPlayer();
}
if (CharPos.x >= (9.3f) && CharPos.x <= (22.5f)) { //These are just confining the player to the area
PanPlayer();
}
}
void FollowPlayer () {
Vector3 CharPos = GameObject.Find("Character").transform.position;
Vector3 RelevantPos = new Vector3 (this.transform.position.x, this.transform.position.y, (CharPos.z -8));
Vector3 RelevantRotation = new Vector3((-CharPos.z + 25f), this.transform.rotation.y, this.transform.rotation.z); //Pitch
this.transform.position = RelevantPos;
this.transform.eulerAngles = new Vector3 (RelevantRotation.x, transform.eulerAngles.y, transform.eulerAngles.z);
}
void PanPlayer() {
Vector3 CharPos = GameObject.Find("Character").transform.position;
Vector3 RelevantPos = new Vector3 (CharPos.x, this.transform.position.y, this.transform.position.z);
this.transform.position = RelevantPos;
}
Your answer
Follow this Question
Related Questions
I cant figure out what is wrong with my code 1 Answer
X-Axis rotation quirk 0 Answers
Problem with aiming Script(Only Some Code Works) 0 Answers
Detecting Rays from controller input and cast from Object 0 Answers
Vector3 is not receiving enough arguments from a another script that gives out transform.position. 1 Answer