- Home /
Camera with arrow keys?
I want my camera only to move then I hit my arrow keys. can anyone help? This script only dose it with a mouse
using UnityEngine; using System.Collections;</p> <p>public class camera : MonoBehaviour { public Transform target; public float walkDistance; public float runDistance; public float height; public float zoom; public float xSpeed = 250.0f; public float ySpeed = 120.0f;</p> <pre><code>private Transform _myTransform; private float x; private float y; private float BaseValue; //me // Use this for initialization void Start () { if(target == null) Debug.LogWarning("we have no tageet"); _myTransform = transform; } void LateUpdate() { if(Input.GetMouseButton(0)) { x += Input.GetAxis("Mouse X") * xSpeed * 0.02f; y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f; // y = ClampAngle(y, yMinLimit, yMaxLimit); Quaternion rotation = Quaternion.Euler(y, x, 0); Vector3 position = rotation * new Vector3(0.0f, 0.0f, -walkDistance) + target.position; _myTransform.rotation = rotation; _myTransform.position = position; } } </code></pre> <p>}
Answer by ina · May 07, 2011 at 08:38 PM
Simply replace Input.GetAxis
with Input.GetKey
You would probably want to map the left arrow as a negative input, and down as negative input etc.
var factor = 0.02f; // change this depending on interval to move camera if(Input.GetKey(KeyCode.LeftArrow)) x -= xSpeed * factor; else if(Input.GetKey(KeyCode.RightArrow)) x += xSpeed * factor if(Input.GetKey(KeyCode.UpArrow)) y -= xSpeed * factor; else if(Input.GetKey(KeyCode.DownArrow)) y += xSpeed * factor
ahh i know im asking a lot but could you put it int the script b/c im getting
error CS1502: The best overloaded method match for UnityEngine.Input.GetButtonDown(string)' has some invalid arguments error CS1503: Argument
#1' cannot convert UnityEngine.$$anonymous$$eyCode' expression to type
string'
sorry, i typo'd keycode ins$$anonymous$$d of $$anonymous$$eyCode ... should work now if you replace the x and y axis getstuff with this