- Home /
Moving Camera with WASD
Hello, I've used C# XNA so im used to C#, but I need an example of what code for moving the camera would look like with WASD in Unity. Please include the keystate "gets" and how to access the camera's position variables, as well as how to set them. Thanks!
Answer by CodeMasterMike · Nov 22, 2012 at 07:08 AM
Very simple example here:
float xAxisValue = Input.GetAxis("Horizontal");
float zAxisValue = Input.GetAxis("Vertical");
if(Camera.current != null)
{
Camera.current.transform.Translate(new Vector3(xAxisValue, 0.0f, zAxisValue));
}
So the "Input.GetAxis" variables are set in the Unity Input Manager (Edit->Project settings->Input) and in this case "Horizontal" means left and right, and "Vertical" forward and backward (z-axis). And this function returns a float value between '-Deadzone' (negative float) and 'Deadzone' (positive float).
If the button is not pressed at all, it returns 0 (zero). Remember that the GetAxis function interpolates the value, so it doesn't go straight to 0 (zero). If you want it to go straight to 0 (zero) when the key is released, you need to use "Input.GetAxisRaw" function.
So now when you have the translate value, you get your camera and you translate its transform with the values that you got. In my case above, I use the current camera, which is not so good if you have several different cameras in your project. But it works here as a example.
Little further reading:
GetAxis function documentation
GetAxisRaw function documentation
Good luck!
This, of course, makes a lot of assumptions. It assumes that the camera is on a flat plane, and is not tilted downwards in a traditional 3rd person view, or RTS camera angle.
Answer by MountDoomTeam · Nov 22, 2012 at 07:13 AM
keyboardorbit is a standard unity code in js and cs. look for keyboardOrbit in your project window search option, it should be there if you have the standard assets all installed.
this is it-
//WASD to orbit, left Ctrl/Alt to zoom
using UnityEngine;
[AddComponentMenu("Camera-Control/Keyboard Orbit")]
public class KeyboardOrbit : MonoBehaviour {
public Transform target;
public float distance = 20.0f;
public float zoomSpd = 2.0f;
public float xSpeed = 240.0f;
public float ySpeed = 123.0f;
public int yMinLimit = -723;
public int yMaxLimit = 877;
private float x = 22.0f;
private float y = 33.0f;
public void Start () {
x = 22f;
y = 33f;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
public void LateUpdate () {
if (target) {
x -= Input.GetAxis("Horizontal") * xSpeed * 0.02f;
y += Input.GetAxis("Vertical") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
distance -= Input.GetAxis("Fire1") *zoomSpd* 0.02f;
distance += Input.GetAxis("Fire2") *zoomSpd* 0.02f;
Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
public static float ClampAngle (float angle, float min, float max) {
if (angle < -360.0f)
angle += 360.0f;
if (angle > 360.0f)
angle -= 360.0f;
return Mathf.Clamp (angle, min, max);
}
}
That last answer gives 19 parsing errors in Unity! Anything better?
Answer by CarterG81 · Mar 24, 2017 at 09:20 AM
For anyone who wants a QUICK Copy/Paste script just to move the Camera along the X/Z axis
using UnityEngine;
public class Debug_CameraMovement : MonoBehaviour
{
public int Speed = 50;
void Update()
{
float xAxisValue = Input.GetAxis("Horizontal") * Speed;
float zAxisValue = Input.GetAxis("Vertical") * Speed;
transform.position = new Vector3(transform.position.x + xAxisValue, transform.position.y, transform.position.z + zAxisValue);
}
}
And if you want Q&E to zoom in/out Y-axis
using UnityEngine;
public class Debug_CameraMovement : MonoBehaviour
{
public int Speed = 50;
void Update()
{
float xAxisValue = Input.GetAxis("Horizontal") * Speed;
float zAxisValue = Input.GetAxis("Vertical") * Speed;
float yValue = 0.0f;
if (Input.GetKey(KeyCode.Q))
{
yValue = -Speed;
}
if (Input.GetKey(KeyCode.E))
{
yValue = Speed;
}
transform.position = new Vector3(transform.position.x + xAxisValue, transform.position.y + yValue, transform.position.z + zAxisValue);
}
}
Answer by pseudonimus · Feb 08, 2017 at 08:20 PM
I have my version
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RTS;
using System;
public class UserInput : MonoBehaviour {
private Player player;
// Use this for initialization
void Start () {
player = transform.root.GetComponent<Player>();
}
// Update is called once per frame
void Update () {
if (player.human)
{
MoveCamera();
RotateCamera();
}
}
private void RotateCamera()
{
Vector3 origin = Camera.main.transform.eulerAngles;
Vector3 destination = origin;
//detect rotation amount if ALT is being held and the Right mouse button is down
if ((Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt)) && Input.GetMouseButton(1))
{
destination.x -= Input.GetAxis("Mouse Y") * ResourceManager.RotateAmount;
destination.y += Input.GetAxis("Mouse X") * ResourceManager.RotateAmount;
}
//if a change in position is detected perform the necessary update
if (destination != origin)
{
Camera.main.transform.eulerAngles = Vector3.MoveTowards(origin, destination, Time.deltaTime * ResourceManager.RotateSpeed);
}
}
private void MoveCamera()
{
float xpos = Input.mousePosition.x;
float ypos = Input.mousePosition.y;
Vector3 movement = new Vector3(0, 0, 0);
//Move the GameObject
if (Input.GetKey("a"))
{
movement.x -= ResourceManager.ScrollSpeed;
}
if (Input.GetKey("s"))
{
movement.z -= ResourceManager.ScrollSpeed;
}
if (Input.GetKey("d"))
{
movement.x += ResourceManager.ScrollSpeed;
}
if (Input.GetKey("w"))
{
movement.z += ResourceManager.ScrollSpeed;
}
//horizontal camera movement
if (xpos >= 0 && xpos < ResourceManager.ScrollWidth)
{
movement.x -= ResourceManager.ScrollSpeed;
}
else if (xpos <= Screen.width && xpos > Screen.width - ResourceManager.ScrollWidth)
{
movement.x += ResourceManager.ScrollSpeed;
}
//vertical camera movement
if (ypos >= 0 && ypos < ResourceManager.ScrollWidth)
{
movement.z -= ResourceManager.ScrollSpeed;
}
else if (ypos <= Screen.height && ypos > Screen.height - ResourceManager.ScrollWidth)
{
movement.z += ResourceManager.ScrollSpeed;
}
movement = Camera.main.transform.TransformDirection(movement);
movement.y = 0;
//away from ground movement
movement.y -= ResourceManager.ScrollSpeed * Input.GetAxis("Mouse ScrollWheel");
//calculate desired camera position based on received input
Vector3 origin = Camera.main.transform.position;
Vector3 destination = origin;
destination.x += movement.x;
destination.y += movement.y;
destination.z += movement.z;
if (destination.y > ResourceManager.MaxCameraHeight)
{
destination.y = ResourceManager.MaxCameraHeight;
}
else if (destination.y < ResourceManager.MinCameraHeight)
{
destination.y = ResourceManager.MinCameraHeight;
}
//if a change in position is detected perform the necessary update
if (destination != origin)
{
Camera.main.transform.position = Vector3.MoveTowards(origin, destination, Time.deltaTime * ResourceManager.ScrollSpeed);
}
}
}
Answer by siman007 · Jan 31 at 07:08 PM
I needed a simple script to move the camera in a 3D scene using the keyboard or mouse for Windows/Mac users. It covers most of the basic WASD/Arrow/Mouse movements and has taken far more effort than I ever thought it would. Please feel free to use it as needed.
Your answer
Follow this Question
Related Questions
AddForce to Camera 1 Answer
RTS Camera Rotation and Movement 0 Answers
movement disable when camera changes view 1 Answer
Move camera forward/backwards with mouse wheel 1 Answer
Constraining a camera movement 0 Answers