Search Unity

cursor-following object jitters when camera moves

Discussion in 'Scripting' started by teatime, Aug 23, 2009.

  1. teatime

    teatime

    Joined:
    Jun 16, 2008
    Posts:
    129
    in my scene i have an object that is projected onto the nearest surface behind the cursor with this script:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CursorHit : MonoBehaviour {
    5.    
    6.     public HeadLookController headLook;
    7.    
    8.     // Update is called once per frame
    9.     void LateUpdate () {   
    10.         Ray cursorRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    11.         RaycastHit hit;
    12.         if (Physics.Raycast(cursorRay, out hit)) {
    13.             transform.position = hit.point;
    14.         }
    15.        
    16.         headLook.target = transform.position;
    17.     }
    18. }
    this is currently the script i am using to make the camera follow the player character when it moves:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SmoothFollowCamera : MonoBehaviour {
    5.    
    6.     public GameObject target;
    7.     public float smoothingTime = 0.5f;
    8.     public Vector3 offset = Vector3.zero;
    9.     public float rotateAngle = 45;
    10.     public float rotateTime = 25;
    11.     public bool useBounds = false;
    12.     private SmoothFollower follower;
    13.    
    14.     // Use this for initialization
    15.     void Start () {
    16.         follower = new SmoothFollower(smoothingTime);
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void LateUpdate () {
    21.         if (target==null) return;
    22.        
    23.         Vector3 targetPoint = target.transform.position;
    24.         if (useBounds) {
    25.             Renderer r = target.GetComponentInChildren(typeof(Renderer)) as Renderer;
    26.             Vector3 targetPointCenter = r.bounds.center;
    27.             if (!float.IsNaN(targetPointCenter.x)
    28.                 &targetPointCenter.magnitude < 10000) targetPoint = targetPointCenter;
    29.         }
    30.         transform.position = follower.Update(targetPoint + offset, Time.deltaTime);
    31.         transform.rotation = Quaternion.AngleAxis(
    32.             Mathf.Sin(Time.time*2*Mathf.PI/rotateTime)*rotateAngle,
    33.             Vector3.up
    34.         );
    35.     }
    36. }
    it may also be worth noting that the player character faces the cursor object based on the Head Look Controller.

    whenever the camera moves, the object that follows the cursor jitters. changing the scripts to use different combinations of Update and LateUpdate did not yield a satisfying, predictable solution (and as i understand it, the camera should always use LateUpdate.) what should i change to ensure smooth movement of both the cursor-following object and the camera itself?
     
  2. Murcho

    Murcho

    Joined:
    Mar 2, 2009
    Posts:
    309
    It sounds like you really need the first script to fire after the camera has moved. It might be worth changing it's LateUpdate method to another name that isn't automatically called by Unity, and manually call it from your camera script at the end of it's LateUpdate method. It's not ideal coding practice, but sometimes you need to get a little hacky to get the job done. :wink:
     
  3. teatime

    teatime

    Joined:
    Jun 16, 2008
    Posts:
    129
    yeah, not the cleanest solution i guess (if only there was an EvenLaterUpdate or something :wink: ), but it works great. thanks Murcho.
     
  4. Murcho

    Murcho

    Joined:
    Mar 2, 2009
    Posts:
    309
    Glad to hear it works. Not being in 100% control of event execution means sometimes you have to get a bit hacky, but if it works it works :D