x


RTS camera movement - easing?

I am trying to create a simple RTS camera that scrolls when the mouse is at the edges of the screen, here is my code:

public float scrollSpeed = 5.0F;
    public float scrollBuffer = 10.0F;
    public float damping = 2.0F;

    private Transform _tr;

    void Awake () {
       _tr = transform;
    }

    void Update () {
       Vector3 newPos = _tr.position;
       if (Input.mousePosition.x >= Screen.width-scrollBuffer)
         newPos.x = Mathf.Lerp (_tr.position.x, _tr.position.x + scrollSpeed, damping * Time.deltaTime);
       if (Input.mousePosition.x <= scrollBuffer)
         newPos.x = Mathf.Lerp (_tr.position.x, _tr.position.x - scrollSpeed, damping * Time.deltaTime);
       if (Input.mousePosition.y >= Screen.height-scrollBuffer)
         newPos.z = Mathf.Lerp (_tr.position.z, _tr.position.z + scrollSpeed, damping * Time.deltaTime);
       if (Input.mousePosition.y <= scrollBuffer)
         newPos.z = Mathf.Lerp (_tr.position.z, _tr.position.z - scrollSpeed, damping * Time.deltaTime);

       _tr.position = newPos;
    }

I want the camera to come to a smooth halt when the mouse moves away from the screen bounds, but the "damping" method I have implemented above doesn't seem to work. Does anyone know what I am doing wrong?

more ▼

asked Nov 29 '11 at 10:36 PM

Disaster gravatar image

Disaster
482 48 57 67

(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

I've seen it somewhere:

if ( Input.GetKey("d") || Input.mousePosition.x >= Screen.width * (1 - ScrollEdge) ) { transform.Translate(Vector3.right * Time.deltaTime * ScrollSpeed); } else if ( Input.GetKey("a") || Input.mousePosition.x <= Screen.width * ScrollEdge ) { transform.Translate(Vector3.right * Time.deltaTime * -ScrollSpeed); }

    if ( Input.GetKey("w") || Input.mousePosition.y >= Screen.height * (1 - ScrollEdge) )
    {
       transform.Translate(Vector3.forward * Time.deltaTime * ScrollSpeed);
    }
    else if ( Input.GetKey("s") || Input.mousePosition.y <= Screen.height * ScrollEdge )
    {
       transform.Translate(Vector3.forward * Time.deltaTime * -ScrollSpeed);
    }
more ▼

answered Jan 09 '12 at 11:14 PM

Kvazir gravatar image

Kvazir
31 1 1 1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3000
x1279
x20

asked: Nov 29 '11 at 10:36 PM

Seen: 668 times

Last Updated: Jan 09 '12 at 11:14 PM