I've applied this standard assets script to my camera and it's actually doing a good job but since my background texture is placed inside a quad that "follows" the player and not the camera at higher movement speed the camera gets too far or too much behind the player and gets out of view. Since I never programmed in JS I'd like to ask you how should I tweak this code to stop the script from moving the camera if the velocity is over (for example) 5f. I tried to change it this way : Code (csharp): var target : Transform; var smoothTime = 0.3; private var thisTransform : Transform; private var velocity : Vector2; function Start() { thisTransform = transform; } function Update() { // ********* Added tweak ************* if(velocity.x > 5f) //in C# I'd do it this way, but apparently velocity.x = 5f; //if velocity gets past 5, it doesn't remain 5 //********** tweak end************* thisTransform.position.x = Mathf.SmoothDamp( thisTransform.position.x, target.position.x, velocity.x, smoothTime); thisTransform.position.y = Mathf.SmoothDamp( thisTransform.position.y, target.position.y, velocity.y, smoothTime); }
Toss this on your main camera and see what happens. Code (csharp): using UnityEngine; using System.Collections; public class GenericCamFollow : MonoBehaviour { public Transform player; void Update () { transform.position = new Vector3(player.position.x + 6, 0, -10); // -10 is the Main Camera's Z position in the Unity Inspector // 6 makes the Main Camera stay 6 steps ahead of player } }
This fixes the camera on one single position at each update call, what I want to do is stopping the SmoothFollow2D from moving the camera at a certain speed, since this make the player go out of view after a certain speed
If you just want to limit the camera's speed, the SmoothDamp function comes with an option maxSpeed to clamp the maximum speed. See Mathf.SmoothDamp and Vector3.SmoothDamp. Also, it will probably look better if you use Vector3.SmoothDamp to smooth your 2D position instead of separately smoothing x and y with Mathf.SmoothDamp.
Actually the maxSpeed parameter tells the compiler what's the maximum speed before it stops following the objects thus increasing the out of sight distance between the player and the camera.. But I solved it though, few bools and a separate method and it works great now