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 ![]()
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.