Saving camera position after middle mouse click

So thanks for all your help so far guys, I’ve gotten my AI working absolutely great now on this city builder I’m playing with, I’m going to be experimenting with variables soon and getting more serious with that, among other things, it’s great how generally easy to use Unity is.

However, I’ve got a small problem which is just a simple case again of me not knowing the correct syntax or being able to find tutorials on the subject that match my question :frowning:

I’ve got my free look camera working almost exactly how I want it but it’s behaving fairly annoying and it snaps back to the default position. Now I’ve tried various things and I’m almost certain it’s simply a matter of calling up the main camera’s position with the Camera.main command it doesn’t seem like it should be a difficult problem but I think I’m just not looking at it right.

using UnityEngine;
using System.Collections;

public class FreeLook : MonoBehaviour {
  
    public float speed = 50.0f;
    public float sensitivity = 20.0f;
    public bool inverted = false;
    public float scrollSensitivity = 10.0f;


    private Vector3 lastMouse = new Vector3(225,225,225);
  
    // Use this for initialization
    void Start () {


    }
  
    // Update is called once per frame
    void Update () {



        if (Input.GetMouseButton (2))
      
        {
            lastMouse = Input.mousePosition - lastMouse;
            if (!inverted)
            lastMouse.y = - lastMouse.y;
            lastMouse *= sensitivity;
            lastMouse = new Vector3 (transform.eulerAngles.x + lastMouse.y, transform.eulerAngles.y + lastMouse.x, 0);
            transform.eulerAngles = lastMouse;
      
            lastMouse = Input.mousePosition;
        }
  
      
        Vector3 dir = new Vector3 ();
      
        if (Input.GetKey (KeyCode.W)) dir.z += 1.0f;
        if (Input.GetKey (KeyCode.S)) dir.z -= 1.0f;
        if (Input.GetKey (KeyCode.A)) dir.x -= 1.0f;
        if (Input.GetKey (KeyCode.D)) dir.x += 1.0f;
      
      
        dir.Normalize ();
      
        transform.Translate(dir * speed * Time.deltaTime);
      
        transform.position += new Vector3(0f, Input.GetAxis("Mouse ScrollWheel") * scrollSensitivity * Time.deltaTime, 0f);

        if (Input.GetKeyDown (KeyCode.Escape))
        {
            Application.Quit ();
        }
      
    }
}

Here’s the code and you’ll see for yourself if you load it up, from what I can see the translate parts of the code are likely to be what’s causing the snap back as the numbers are either set to 0f or default. I tried putting in a simple variable to detect the Camera but the compiler didn’t like that because the code is using Vector3.

After you hijacked the other thread, I decided I would take a look.

It seemed kind of like a roundabout way of doing mouse look, but I seem to come up with a solution:
If you were referring to the jump that happens when you first press MouseButton2, your problem is your recording of the lastMouse reference. In testing, I noticed that it was only an issue on the first click… so I wondered why that was.

Turns out you are only saving lastMouse while button2 is being held down. So when you release Button2, last mouse stays where it was when you last released it. So I thought I could just move it to be recorded all the time in Update(), but then nothing happened. And I realized execution order matters here. If you check lastMouse against current Input this frame, and then subtract Input from it when a button is held, there will be no change, because lastMouse is always updating and will always be the same as Input.mousePosition.

So it turns out all that needed to be done was to move this line:

 lastMouse = Input.mousePosition;

outside of the brackets when checking Input.GetMouseButton. And they have to be after the brackets. So now, you’re updating lastMouse every frame, but only after an initial check to see if Input has changed from that lastMouse. And if so, then it matches the euler angles. After that, it worked perfectly for me :slight_smile:

1 Like

Of course! That makes total bloody sense! You stare at this thing for hours and it’s right in front of your face the whole time. Yes, it’s as you say, because that particular function is only happening on a mouse click the camera resets itself and you need it to be constantly updating instead, so no wonder the camera was snapping while I was trying to move it after a second time.

I figured that I had to do something complicated like make that piece of code read off the camera position from the actual object hiearchy but I was overthinking it, thanks for saving me all that effort.

Working beautifully now, thanks again, Startopia clone here I come :stuck_out_tongue:

1 Like

Haha I’d love to see the finished game when it’s complete!