Can anyone tell me what I’m doing wrong, or point me in the right direction? I wrote a script to control my camera. So far I have WASD, mouse wheel zoom, and WASD movement limits working. I’d like to have zoom limits, so the user can’t zoom the camera in too far, or too far out. I tried using mathf.clamp to limit movement in the y direction, which is vertical for me, but it just makes the camera jump continually between the two limits I’ve set. This is my script:
using UnityEngine;
using System.Collections;
public class CamControl : MonoBehaviour {
public float speed =1;
// Use this for initialization
void Start () {
Cursor.visible = false;
}
// Update is called once per frame
void Update () {
float horz = Input.GetAxis ("Horizontal");
float vert = Input.GetAxis ("Vertical");
transform.position += (Vector3.forward * vert + Vector3.right * horz) * speed * Time.deltaTime;
float mousewheel = Input.GetAxis ("Mouse ScrollWheel");
if (mousewheel != 0)
transform.position -= Vector3.up * mousewheel * speed * Time.deltaTime *20;
transform.position = new Vector3(Mathf.Clamp(transform.position.x, -40, 40), transform.position.y, Mathf.Clamp(transform.position.z, -60, 40));
}
Can anyone help?