- Home /
Move camera relative to where you're facing
Most questions want to rotate an object around an axis. But I need to rotate all three axis accordingly around to the rotation of the camera. This is due to the fact I want to mimic most first person shooters: if you press the key to go forward, you go forward relative to where you're facing not the axis. For example, if I turn my camera to the left 90 degrees and I press the forward button (up arrow), the camera moves forward... as if it didn't turn at all.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour {
public float speed;
public GameObject projectile;
public GameObject hit_object;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.RightArrow))
{
transform.localPosition += Vector3.right * Time.deltaTime * speed;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.position += Vector3.left * Time.deltaTime * speed;
}
if (Input.GetKey(KeyCode.DownArrow))
{
transform.position += Vector3.back * Time.deltaTime * speed;
}
if (Input.GetKey(KeyCode.UpArrow))
{
transform.position += Vector3.forward * Time.deltaTime * speed;
}
if (Input.GetKeyDown(KeyCode.F))
{
GameObject bullet = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
bullet.GetComponent<Rigidbody>().AddRelativeForce(new Vector3(0, 0, 2000));
}
if (Input.GetKeyDown(KeyCode.E))
{
var pos_x = transform.position.x + Random.Range(-500F, 500F);
var pos_z = transform.position.z + Random.Range(-500F, 500F);
Instantiate(hit_object, new Vector3(pos_x, 400, transform.position.z + pos_z), Quaternion.identity);
}
transform.Rotate(0f, Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0f, Space.World);
transform.Rotate(-Input.GetAxis("Vertical") * speed * Time.deltaTime, 0f, 0f, Space.Self);
}
}
In the above snippet, the camera is facing in the direction of the red arrow and thus expect it to move forward in that direction. Instead, it moves according the y-axis (blue arrow). How do I fix this problem?
Answer by PythonMaster18 · Jul 12, 2017 at 08:50 PM
Now, I have solved my solution by using transform.right
and transform.forward
instead of Vector3.
The relevant part of the code now looks likes this:
if (Input.GetKey(KeyCode.RightArrow))
{[link text][1]
transform.localPosition += transform.right * Time.deltaTime * speed;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.position += -transform.right * Time.deltaTime * speed;
}
if (Input.GetKey(KeyCode.DownArrow))
{
transform.position += -transform.forward * Time.deltaTime * speed;
}
if (Input.GetKey(KeyCode.UpArrow))
{
transform.position += transform.forward * Time.deltaTime * speed;
}
I have gotten my solution here as the answerer explained the difference of using transform
and Vector3
.
Answer by Vollmondum · Jul 12, 2017 at 07:37 PM
Vector3.forward moves your object in z = 0 direction, that's wrong script line. You want to move (direction.position - transform.position) Reference
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour {
public float speed;
public GameObject projectile;
public GameObject hit_object;
private Vector3 moveDirection = Vector3.zero;
public Vector3 movementMultiplier;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
moveDirection = new Vector3(movementMultiplier.x, movementMultiplier.y, movementMultiplier.z);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetKey(KeyCode.RightArrow))
{
movementMultiplier.x = 1;
movementMultiplier.z = 0;
transform.position += moveDirection * Time.deltaTime;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
movementMultiplier.x = -1;
movementMultiplier.z = 0;
transform.position += moveDirection * Time.deltaTime;
}
if (Input.GetKey(KeyCode.DownArrow))
{
movementMultiplier.x = 0;
movementMultiplier.z = -1;
transform.position += moveDirection * Time.deltaTime;
}
if (Input.GetKey(KeyCode.UpArrow))
{
movementMultiplier.x = 0;
movementMultiplier.z = 1;
transform.position += moveDirection * Time.deltaTime;
}
if (Input.GetKeyDown(KeyCode.F))
{
GameObject bullet = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
bullet.GetComponent<Rigidbody>().AddRelativeForce(new Vector3(0, 0, 2000));
}
if (Input.GetKeyDown(KeyCode.E))
{
var pos_x = transform.position.x + Random.Range(-500F, 500F);
var pos_z = transform.position.z + Random.Range(-500F, 500F);
Instantiate(hit_object, new Vector3(pos_x, 400, transform.position.z + pos_z), Quaternion.identity);
}
}
}
Could you add code to clarify? I am not quite sure what and where to add into my code.
I'm not a C# lover, but the reference I gave you, initializes the direction you're facing. All you need to do is to add moveDirection to your transform.localPosition lines.
So what relevant pieces of code from your reference is needed?