- Home /
Jerky movement of camera
I have following script which is part of my TPP movement system:
public class moveUpDown : MonoBehaviour {
public float mouseSensitivity;
public float maxHeight;
private float realmaxHeight;
public float minHeight;
public GameObject lookAtObject;
private Vector3 lpos;
public bool minHeightIsPlayerHeight;
// Use this for initialization
void Start () {
realmaxHeight = maxHeight;
}
// Update is called once per frame
void Update () {
if (minHeightIsPlayerHeight){
minHeight=GameData.groundLevel;
realmaxHeight = GameData.groundLevel+maxHeight;
}
lpos = this.transform.position;
float mouseDeltaY = Input.GetAxis("Mouse Y") * mouseSensitivity;
lpos.y+=mouseDeltaY;
if (lpos.y>realmaxHeight) {
lpos.y=realmaxHeight;
}
if (lpos.y<minHeight) {
lpos.y=minHeight;
}
if (lookAtObject!=null){
this.transform.LookAt(lookAtObject.transform);
}
this.transform.position = lpos;
}
}
This script is designed to move camera up/down with mouse in some constraints, so player can rotate camera freely, not only horizontally.
Unfortunately it shows some tearing/ghosting (second, half-transparent "ghost" of object is visible and movement in general show some "jumping") while moving mouse up/down.
For config part, mouseSensitivity is 6.31, maxHeight is 10, minHeight is unimportant as there is minHeightIsPlayerHeight checked (I have helper, static class called GameData with static float field that is set into ground level of place where player is standing - it isn't perfect solution, especially for slopes, but since my game will be mainly consist of flat surfaces it is good enough) which overrides minHeight. Of course lookAtObject is set to player character.
Here is my scene hierarchy:
PlayerCharacter <-player controller,etc.
|--Main Camera <- script with source in this question
|--some objects, like GUI textures, etc.
Any help on that?
Answer by darkhog · Jan 07, 2013 at 11:47 PM
Fixed by swapping this:
if (lookAtObject!=null){
this.transform.LookAt(lookAtObject.transform);
}
with this:
this.transform.position = lpos;