Hi guys, im rather new to unity so maybe there is something to the camera behaviour that I just dont understand yet. However, I have written this smoothFollow2D script in C# to make my Camera follow a square player object when its moving:
public Transform target;
public float smoothTime = 0.3f;
private Transform thisTransform;
private Vector2 velocity;
// Use this for initialization
void Start ()
{
thisTransform = GetComponent<Transform>();
}
// Update is called once per frame
void Update ()
{
Vector2 smoothMove = new Vector2(Mathf.SmoothDamp(thisTransform.position.x,
target.position.x, ref velocity.x, smoothTime),
Mathf.SmoothDamp(thisTransform.position.y, target.position.y, ref
velocity.y, smoothTime));
thisTransform.position = smoothMove;
}
This is working very well so far, but when i apply this script to my camera and start the game it simply stops rendering any objects but the background(Skybox). I can see the camera following the player object in the scene view but i see nothing in the game view. I also had this problem before without any sripts and just with 3 objects in my scene, but I cant tell what I did to solve it.
EDIT:
I figured out that the problem has something to do with the initialisation of the targeting Transform, if I leave it empty or just clear the start section there is no rendering issue.
Why would you create a variable just for your transform component? Just use "transform" instead.
– RioneerI thought it just works that way because you have to acces transform by GetComponent(); So you say I could use it like this.transform?
– Skip.8Storing transform in a reference variable saves processing power. Your script is correct. By assigning vector2 to your camera's position. You set position.z = 0. Does this cause problem?
– PriyanshuThat was my tought too. Thanks, and yes my problem was caused by the z positioning. I've already found that out by myself, but thanks anyway.
– Skip.8