- Home /
Camera points towards object but doesn't go to it
Here is my player script:
public class PlayerControler : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
public static Vector3 lastPosition;
private Rigidbody rb;
private int count;
void Start ()
{
rb = GetComponent<Rigidbody> ();
count = 0;
SetCountText ();
winText.text = "";
}
void Update ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
Vector3 lastPosition = transform.position;
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Count: " + count.ToString ();
if (count >= 12) {
winText.text = "You Win!!";
}
}
}
//Destroy(other.gameObject);
And here is my camera script:
public class Accessor : MonoBehaviour {
void Start()
{
GameObject thePlayer = GameObject.Find("Player");
PlayerControler playerScript = thePlayer.GetComponent<PlayerControler>();
}
}
public class CameraController : MonoBehaviour {
public GameObject player;
public float moveSpeed = 5.0f;
private Vector3 offset;
public Transform target;
// Use this for initialization
// Update is called once per frame
void LateUpdate ()
{
transform.position = PlayerControler.lastPosition;
transform.LookAt (target);
}
}
I'm pretty sure it was originally from a tutorial, but still. The objective is for the camera to go to the position that the player was in the previous frame, and then point at the player now. It successfully points at the player, but it just stays in place. Help please
Answer by Harinezumi · Feb 09, 2018 at 10:02 AM
You have member variable lastPosition
in PlayerControler
, but you set the position to a local variable lastPosition
in Update(). To fix it just delete Vector3 from before it (because that signifies you are declaring a new variable).
The same thing with playerScript
in Accessor.Start(): you store it in a local variable. Move the declaration of the variable outside of Start(), and then you don't have to make lastPosition
static (be careful with using static variables, unless you know what you are doing, avoid them).
Now it says "a field initializer cannot reference the non-static field, method, or property 'Accessor.thePlayer.'"
Without showing the code, I cannot be sure what causes the error message, but I'm guessing you made the new variable Accessor.thePlayer
static (for no reason) and then tried to assign it to something (this is what the error message is saying). Removing the static keyword from it should fix (this) error.
In general, don't use static variables unless you really know what you are doing. Static variables are "bound" to the class, not the object instances, so multiple objects can access them at the same time, which - due to unpredictable execution order - can cause bugs.
Answer by Honorsoft · Feb 09, 2018 at 08:46 AM
Not sure, it's after 3am, but is it your LateUpdate function? Try this and see if it works:
void LateUpdate ()
{
transform.position = playerScript.lastPosition;
transform.LookAt (target);
}
If that was the problem, it was because you made the proper reference to the Component in the Start function:
PlayerControler playerScript = thePlayer.GetComponent<PlayerControler>();
..but in the LateUpdate you still used PlayerControler:
transform.position = PlayerControler.lastPosition;
(PlayerController is missing an L by the way)
Your answer
![](https://koobas.hobune.stream/wayback/20220612152248im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How To Desync A Player's Camera? 1 Answer
Distribute terrain in zones 3 Answers
Camera follow player while it doesnt block animations 1 Answer
Making camera follow upwards 3 Answers
Camera Follow works partially, need help to finish the script 3 Answers