Search Unity

Smooth Follow 2D simple tweak

Discussion in 'Scripting' started by Rphysx, Apr 20, 2014.

  1. Rphysx

    Rphysx

    Joined:
    Mar 26, 2014
    Posts:
    54
    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):
    1.  
    2.     var target : Transform;
    3.     var smoothTime = 0.3;
    4.     private var thisTransform : Transform;
    5.     private var velocity : Vector2;
    6.      
    7.     function Start()
    8.     {
    9.     thisTransform = transform;
    10.     }
    11.      
    12.     function Update()
    13.     {
    14.          // *********   Added tweak *************
    15.     if(velocity.x > 5f) //in C# I'd do it this way, but apparently
    16.     velocity.x = 5f; //if velocity gets past 5, it doesn't remain 5
    17.         //********** tweak end*************
    18.      
    19.     thisTransform.position.x = Mathf.SmoothDamp( thisTransform.position.x,
    20.     target.position.x, velocity.x, smoothTime);
    21.     thisTransform.position.y = Mathf.SmoothDamp( thisTransform.position.y,
    22.     target.position.y, velocity.y, smoothTime);
    23.     }
    24.  
     
  2. wondyr

    wondyr

    Joined:
    Nov 30, 2013
    Posts:
    36
    Toss this on your main camera and see what happens.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GenericCamFollow : MonoBehaviour
    5. {
    6.     public Transform player;
    7.  
    8.     void Update ()
    9.     {
    10.         transform.position = new Vector3(player.position.x + 6, 0, -10);
    11.         //  -10 is the Main Camera's Z position in the Unity Inspector
    12.         //  6 makes the Main Camera stay 6 steps ahead of player
    13.     }
    14. }
     
  3. Rphysx

    Rphysx

    Joined:
    Mar 26, 2014
    Posts:
    54
    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
     
  4. Bivrost

    Bivrost

    Joined:
    Mar 26, 2014
    Posts:
    80
    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.
     
  5. Rphysx

    Rphysx

    Joined:
    Mar 26, 2014
    Posts:
    54
    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