Search Unity

Smooth Follow script to C# with some changes

Discussion in 'Scripting' started by RingOfStorms, Apr 22, 2012.

  1. RingOfStorms

    RingOfStorms

    Joined:
    Oct 23, 2010
    Posts:
    584
    I'm trying to convert the smooth follow script to C#, and make it work with a rigidbody so that it doesn't ignore physics. This is what I have so far
    Code (csharp):
    1. public class SmoothFollow : MonoBehaviour {
    2.     public Transform target;    // The target we are following
    3.     public float distance;      // The distance from the target along its Z axis
    4.     public float height;        // the height we want the camera to be above the target
    5.     public float positionDamping;   // how quickly we should get to the target position
    6.     public float rotationDamping;   // how quickly we should get to the target rotation
    7.    
    8.     // Use this for public variable initialization
    9.     public void Reset() {
    10.         distance = 3;
    11.         height = 1;
    12.         positionDamping = 6;
    13.         rotationDamping = 60;
    14.     }
    15.    
    16.     // LateUpdate is called once per frame
    17.     public void LateUpdate () {
    18.         ensureReferencesAreIntact();
    19.        
    20.         #region Get Transform Manipulation
    21.         // The desired position
    22.         Vector3 targetPosition = target.position + target.up * height - target.forward * distance;
    23.         // The desired rotation
    24.         Quaternion targetRotation = Quaternion.LookRotation(target.position-transform.position, target.up);
    25.         #endregion
    26.        
    27.         #region Manipulate Transform
    28.         transform.position = Vector3.MoveTowards(transform.position, targetPosition, positionDamping * Time.deltaTime);
    29.         transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationDamping * Time.deltaTime);
    30.         #endregion
    31.     }
    32.    
    33.     // Checks to make sure all required references still exist and disables the script if not
    34.     private void ensureReferencesAreIntact() {
    35.         if (target == null) {
    36.             Debug.LogError("No target is set in the SmoothFollow Script attached to " + name);
    37.             this.enabled = false;
    38.         }
    39.     }
    The error is on line 36, you can't use myRigidbody.position.y, and height shouldn't matter for physics until later
    Any ideas?
     
    Last edited: Apr 22, 2012
  2. Deleted User

    Deleted User

    Guest

    Unless I'm going crazy, line 36 in your example does not contain "myRigidbody.position.y". Did you perhaps post the wrong code?

    Anyway, you can't assign individual values to positions like this:

    myRigidbody.position.y = 1;

    You must do it like this:

    myRigidbody.position = new Vector3(myRigidbody.position.x, 1, myRigidbody.position.z);

    Does that help, or did I completely miss what is going on?
     
  3. RingOfStorms

    RingOfStorms

    Joined:
    Oct 23, 2010
    Posts:
    584
    I want to take the smooth follow that is included in the camera scripts of standard assets, and make it a C# script, htat uses rigid body instead of transforms.
     
  4. RingOfStorms

    RingOfStorms

    Joined:
    Oct 23, 2010
    Posts:
    584
    If anyone has any more ideas that would be nice.

    If you can't help because your lost here is some info behind it:
    If you have watched the TornadoTwins tutorial, they use the smooth follow script to make spheres follow each other for a a worm. With this method the script uses transforms, and therefor ignores physics. I would like it to be smoother, and respond to physics, so that the colliders will be responsive to walls and such.

    So I just need a Smooth Follow script in C# preferably (but not necessary - I can convert) that is the smooth follow script with physics for colliders.
     
  5. Deleted User

    Deleted User

    Guest

    Your first post is confusing because you say you have an error in line 36, but the code you posted does not correspond. It looks like the error you got was actually in line 36 of a "wormFollow.cs".

    I have gotten the error:

    several times and it was always because of the reason I stated above. I've quickly revised the code you posted to make it work with rigidbodies. I haven't tested it, but it should work. If this doesn't work I will bug off ;)

    Code (csharp):
    1. public class SmoothFollow : MonoBehaviour {
    2.     public Transform target;    // The target we are following
    3.     public float distance;      // The distance from the target along its Z axis
    4.     public float height;        // the height we want the camera to be above the target
    5.     public float positionDamping;   // how quickly we should get to the target position
    6.     public float rotationDamping;   // how quickly we should get to the target rotation
    7.     Rigidbody rBody;
    8.    
    9.     void Awake() {
    10.         rBody = GetComponent<Rigidbody>();
    11.     }
    12.    
    13.     // Use this for public variable initialization
    14.     public void Reset() {
    15.         distance = 3;
    16.         height = 1;
    17.         positionDamping = 6;
    18.         rotationDamping = 60;
    19.     }
    20.  
    21.     // LateUpdate is called once per frame
    22.     public void FixedUpdate () {
    23.         ensureReferencesAreIntact();
    24.         #region Get Transform Manipulation
    25.         // The desired position
    26.         Vector3 targetPosition = target.position + target.up * height - target.forward * distance;
    27.         // The desired rotation
    28.         Quaternion targetRotation = Quaternion.LookRotation(target.position-transform.position, target.up);
    29.         #endregion
    30.  
    31.         #region Manipulate Transform
    32.         rBody.position = Vector3.MoveTowards(rBody.position, targetPosition, positionDamping * Time.deltaTime);
    33.         rBody.rotation = Quaternion.RotateTowards(rBody.rotation, targetRotation, rotationDamping * Time.deltaTime);
    34.         #endregion
    35.     }
    36.  
    37.     // Checks to make sure all required references still exist and disables the script if not
    38.     private void ensureReferencesAreIntact() {
    39.         if (target == null) {
    40.             Debug.LogError("No target is set in the SmoothFollow Script attached to " + name);
    41.             this.enabled = false;
    42.         }
    43.     }
    44. }
     
  6. RingOfStorms

    RingOfStorms

    Joined:
    Oct 23, 2010
    Posts:
    584
    I saw this script befor and used it, but it was much to jittery. The objects don;t move smoothly and htey jump around allot. Is there no way to just convert the exact thing from the old Smooth Follow into C# or Java but have colliders respond
     
    Urosq likes this.
  7. Timbecile76

    Timbecile76

    Joined:
    Jul 8, 2009
    Posts:
    248
    I made a variation of the SmoothFollow2D script that works in C#. (still uses transforms instead of rigidbodies). this might help you along:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SmoothFollow2D : MonoBehaviour {
    6.  
    7.    
    8.     public Transform target ;
    9.     float  smoothTime = 0.3f;
    10.     private Transform thisTransform ;
    11.     private Vector2 velocity;
    12.     float yOffset = 0.7f;
    13.    
    14.     public bool useSmoothing = false;
    15.     void Start()
    16.     {
    17.         thisTransform = transform;
    18.         velocity = new Vector2(0.5f, 0.5f);
    19.     }
    20.    
    21.     void Update()
    22.     {
    23.         Vector2 newPos2D = Vector2.zero;
    24.         if (useSmoothing){
    25.             newPos2D.x =  Mathf.SmoothDamp( thisTransform.position.x, target.position.x, ref velocity.x, smoothTime);
    26.             newPos2D.y= Mathf.SmoothDamp( thisTransform.position.y, target.position.y + yOffset, ref velocity.y, smoothTime) ;
    27.         }else{
    28.             newPos2D.x = target.position.x;
    29.             newPos2D.y = target.position.y + yOffset;
    30.            
    31.         }
    32.            
    33.  
    34.         Vector3 newPos = new Vector3(newPos2D.x, newPos2D.y , transform.position.z);
    35.        
    36.         transform.position = Vector3.Slerp(transform.position, newPos, Time.time);
    37.        
    38.     }
    39. }
    40.  
     
  8. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,242
    Just a couple notes:
    - converted or not, SmoothFollow doesn't work well at all with physics :p
    - whatever changes you make to SmoothFollow, always use LateUpdate to update the camera transform
     
  9. Lesh_M

    Lesh_M

    Joined:
    Apr 28, 2012
    Posts:
    1
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class SmoothFollow : MonoBehaviour
    4. {
    5.     #region Consts
    6.     private const float SMOOTH_TIME = 0.3f;
    7.     #endregion
    8.  
    9.     #region Public Properties
    10.     public bool LockX;
    11.     public bool LockY;
    12.     public bool LockZ;
    13.     public bool useSmoothing;
    14.     public Transform target;
    15.     #endregion
    16.  
    17.     #region Private Properties
    18.     private Transform thisTransform;
    19.     private Vector3 velocity;
    20.     #endregion
    21.  
    22.     private void Awake()
    23.     {
    24.         thisTransform = transform;
    25.  
    26.         velocity = new Vector3(0.5f, 0.5f, 0.5f);
    27.     }
    28.  
    29. // ReSharper disable UnusedMember.Local
    30.     private void LateUpdate()
    31. // ReSharper restore UnusedMember.Local
    32.     {
    33.         var newPos = Vector3.zero;
    34.  
    35.         if (useSmoothing)
    36.         {
    37.             newPos.x = Mathf.SmoothDamp(thisTransform.position.x, target.position.x, ref velocity.x, SMOOTH_TIME);
    38.             newPos.y = Mathf.SmoothDamp(thisTransform.position.y, target.position.y, ref velocity.y, SMOOTH_TIME);
    39.             newPos.z = Mathf.SmoothDamp(thisTransform.position.z, target.position.z, ref velocity.z, SMOOTH_TIME);
    40.         }
    41.         else
    42.         {
    43.             newPos.x = target.position.x;
    44.             newPos.y = target.position.y;
    45.             newPos.z = target.position.z;
    46.         }
    47.  
    48.         #region Locks
    49.         if (LockX)
    50.         {
    51.             newPos.x = thisTransform.position.x;
    52.         }
    53.  
    54.         if (LockY)
    55.         {
    56.             newPos.y = thisTransform.position.y;
    57.         }
    58.  
    59.         if (LockZ)
    60.         {
    61.             newPos.z = thisTransform.position.z;
    62.         }
    63.         #endregion
    64.  
    65.         transform.position = Vector3.Slerp(transform.position, newPos, Time.time);
    66.     }
    67. }