I have been attempting to allow the player to move their Camera through wasd. Since I am new to Unity I am kind of in a pickle here so to speak. Here is the code:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
void Start () {
}
void Update () {
// To move the Camera get the players key.
if (string.ReferenceEquals(Input.anyKeyDown, "w")){
UnityEngine.GameObject.Main_Camera.Translate(Vector3.up);
}
if (string.ReferenceEquals(Input.anyKeyDown, "s")) {
UnityEngine.GameObject.Main_Camera.Translate(Vector3.down);
}
if (string.ReferenceEquals(Input.anyKeyDown, "a")) {
UnityEngine.GameObject.Main_Camera.Translate(Vector3.right);
}
if (string.ReferenceEquals(Input.anyKeyDown, "d")) {
UnityEngine.GameObject.Main_Camera.Translate(Vector3.left);
}
}
}
The output is that Main Camera is not a defined object.
Assuming you haven’t changed the main camera’s tag, you can reference the camera by Camera.main. So to translate you can do:
Camera.main.transform.Translate(Vector3.up);
Also for capturing your board events consider:
if (Input.GetKeyDown(KeyCode.A))
Try this instead - if you want to make the speed of movements framerate-independent (which, generally, you always do), you should always adjust any movements by the amount of time which has passed since the last frame, which is what the * Time.deltaTime term is for :
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.W)){
Camera.main.transform.Translate(Vector3.up * Time.deltaTime);
}
if(Input.GetKey(KeyCode.S)){
Camera.main.transform.Translate(Vector3.down * Time.deltaTime);
}
if(Input.GetKey(KeyCode.A)){
Camera.main.transform.Translate(Vector3.right * Time.deltaTime);
}
if(Input.GetKey(KeyCode.D)){
Camera.main.transform.Translate(Vector3.left * Time.deltaTime);
}
}
}