Rotating Camera

I am currently making a game that is third person. I can move the player, but rather than having the camera follow the player, I want to allow the keyboard to rotate the camera using was keys. here is my code for the camera:

using UnityEngine;
using System.Collections;

public class Camerawasd : MonoBehaviour {

    public float speed;

    private Rigidbody rb;

    void Start ()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate ()
    {
        float rotateHorizontal = Input.GetAxis ("Horizontal");
        float rotateVertical = Input.GetAxis ("Vertical");

        Vector3 rotation = new Vector3 (rotateHorizontal, 0.0f, rotateVertical);

        rb.AddForce (rotation * speed);
    }
}

All it does is just move with the player when I press the arrow keys. I understand why it moves when I press the arrow keys, i haven’t mapped the was yet, but I don’t understand why it moves instead of rotating. Secondly how do I map keys in scripts? Like mapping was to rotate the camera?

Instead of AddForce, use Rigidbody.AddTorque if you want to apply a rotation.

For detecting pressed keys, use Input.GetKey.

2 Likes

@Killj0y

…and if it’s camera, I think you could get away by just moving and aiming your camera using transform, and not using rigidbody at all for camera.

2 Likes

Ok, I think I am sorting it out, but the camera provides next to NO control on its rotation. This a description of what I want it to be like, I want to stop rotating the minute I stop pressing the was keys. Instead it just keeps rotating, I also realised that the camera doesn’t follow the player. So i made the camera into the player’s child. However because the player character is a sphere, it rotates vertically along with the player depending on where I am going. Is there any way to fix this? I was hoping that I could have the player, the sphere, just move like a normal ball, but without rotating.
Here is my code:

using System.Collections;

public class Camerawasd : MonoBehaviour {

    public float speed;

    private Rigidbody rb;


    void Start ()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate ()
    {
        float rotateHorizontal = Input.GetAxis ("Horizontal");
        float rotateVertical = Input.GetAxis ("Vertical");

        Vector3 rotation = new Vector3 (rotateHorizontal, 0.0f, rotateVertical);

        rb.AddTorque (rotation * speed);
    }
}

I didn’t have to change much as the camera was somehow automatically mapped to wasd.
Please don’t be mad, Im still an absolute noob at c# and I can barely tell an argument from a variable! :wink:

EDIT: I managed to find and tweak some code to my liking, but I am still having some problems. Here is my current code now, ignore the one above:

using UnityEngine;
using UnityEngine;
using System.Collections;
public class CameraWASD2 : MonoBehaviour
{
    public int cameraMoveSpeed;


    GameObject cameraFocus;    // For referencing the point around which the camera rotates.
    Transform cameraFocusTransform;    // For referencing the Camera Focus' transform.


    void Start ()
    {
        cameraFocus = GameObject.FindWithTag("CameraFocus");
        cameraFocusTransform = cameraFocus.transform;
    }


    void Update ()
    {
        // Responds to keystrokes with camera rotation.
        /* Left and right rotations. These rotations are applied to the Camera Focus. This way, the Camera Focus'
         * lateral rotation axis rotates with it, and therefore with the camera. This way, the up and down rotations
         * always work. */
        if(Input.GetKey("a") == true)
        {
            cameraFocusTransform.Rotate(cameraFocusTransform.up, cameraMoveSpeed * Time.deltaTime);
        }
        if(Input.GetKey("d") == true)
        {
            cameraFocusTransform.Rotate(cameraFocusTransform.up, -cameraMoveSpeed * Time.deltaTime);
        }
        if(Input.GetKey("w") == true)
        {
            transform.RotateAround(cameraFocusTransform.position, cameraFocusTransform.right, cameraMoveSpeed * Time.deltaTime);
        }
        if(Input.GetKey("s") == true)
        {
            transform.RotateAround(cameraFocusTransform.position, cameraFocusTransform.right, -cameraMoveSpeed * Time.deltaTime);
        }
    }
}

When I try to rotate the camera around, it goes in a circle motion like a bracket, (, but horizontally. The vertical axis is fine. Could u guys help me have my camera follow the player, WHILE I can still move it with wasd? How can I fix my horizontal axis? I tried making it a child with my new script but it generates glitches. I need help with all of this. Thanks in advance people!