0
\$\begingroup\$

I'm trying to have a camera rotate mirror the player's rotation, however, this isn't happening and the camera isn't rotating at all. Can anyone explain what I'm doing wrong and how to fix it?

public class PlayerActions:MonoBehaviour{
    Camera mainCamera;
    public Rigidbody player;
    public GameObject playerShot;
    public Transform shotSpawn;
    public Boundary boundary;

    public int speed;
    public int tilt;
    public int bank;
    public float fireRate;
    private float shotDelay;

    void Start(){
        mainCamera = Camera.main;
        player.GetComponent<Rigidbody>();
    }

    void FixedUpdate(){
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        Vector3 move = new Vector3(moveHorizontal, moveVertical, 0f);
        player.velocity = move * speed;
        player.position = new Vector3(Mathf.Clamp(player.position.x, boundary.xMin, boundary.xMax), Mathf.Clamp(player.position.y, boundary.yMin, boundary.yMax), 0f);
        transform.rotation = Quaternion.Euler(0f, 0f, player.velocity.x * -bank);
    }

    void Update(){
        if(Input.GetKey("space") && Time.time > shotDelay){
            shotDelay = Time.time + fireRate;
            Instantiate(playerShot, shotSpawn.position, shotSpawn.rotation);
        }
    }
}

As it's working now, the player is rotating, but the camera isn't.

If you notice in the video linked below, the ship is rotating, but if you look at the static asteroid, you'll notice that the camera isn't. Not Rotating Game View Not Rotating Editor View

Heirarchy enter image description here

Inspector

enter image description here

\$\endgroup\$
1
\$\begingroup\$

I think the problem here is that you're applying the rotation to the RigidBody of the player, and not to the GameObject transform. It also looks like you're modifying the velocity and position of the RigidBody, which may lead to unexpected results (e.g. the physics engine will be trying to reconcile the new velocity with the new position)

To answer your main question though, I think that simply applying your calculation to the GameObject and not the RigidBody should solve your issue, like so:

public class PlayerActions : MonoBehaviour{
    // ...
    void FixedUpdate(){
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        Vector3 move = new Vector3(moveHorizontal, moveVertical, 0f);
        player.velocity = move * speed;
        player.position = new Vector3(Mathf.Clamp(player.position.x, boundary.xMin, boundary.xMax), Mathf.Clamp(player.position.y, boundary.yMin, boundary.yMax), 0f);
        // Apply rotation to the GameObject
        transform.rotation = Quaternion.Euler(0f, 0f, player.velocity.x * -bank);
    }

    void Update(){
        if(Input.GetKey("space") && Time.time > shotDelay){
            shotDelay = Time.time + fireRate;
            Instantiate(playerShot, shotSpawn.position, shotSpawn.rotation);
        }
    }
}

Notice that there should now be no need to change the camera rotation, as Unity should deal with this for you

\$\endgroup\$
  • \$\begingroup\$ You mention applying the velocity and rotation to the rigidbody and that it would cause problems. What should I be applying it to? I'm new to Unity and am still trying to learn. \$\endgroup\$ – Robert Nov 24 '16 at 19:46
  • \$\begingroup\$ Actually re-reading your code I don't think this will be a problem. I initially read that you were setting the velocity (e.g. telling the physics engine how fast your object is going so that it can update the position) and then also updating the position. In actual fact what you're doing is setting the velocity, but then stopping the player going over certain boundaries. Plus, you're doing all of this in the fixed update, so it should be fine :-) \$\endgroup\$ – hobnob Nov 25 '16 at 10:52
  • \$\begingroup\$ So then it should be working the way I have it? \$\endgroup\$ – Robert Nov 25 '16 at 11:58
  • \$\begingroup\$ It should be working so long as you use the code I provided above. It should just be a case of changing player.rotation to transform.rotation and removing the last line of the Update method (where you change the camera rotation) \$\endgroup\$ – hobnob Nov 25 '16 at 12:01
  • \$\begingroup\$ Alright, I appreciate the help, I'll make that change and let you know if it works. \$\endgroup\$ – Robert Nov 25 '16 at 12:03
1
\$\begingroup\$
  1. "mainCamera" is accessing the Gameobject with the Tag "MainCamera".

To see if its the right camera use: Debug.Log(mainCamera);

2. As far as I understood you want the Camera to move always behind the player (like 3rd person) or inside the player (like in a FPS)

if you want that:

You can either Parent the Camera to the Player. (So the Player Gameobject has a child Gameobject: "Camera")

Or you can Calculate the current position of the Camera. Calculating a Parent/Child relationsship is for pretty advanced users though.

EDIT: 3. As far as I can see, you already parented already a camera to another GameObject ( I assume its the player)

Try using not the rigidbody.rotation but "transform.rotation"

be warned: It should already work if the camera is parented. are you sure you are not modifying the camera position/rotation somewhere else?

\$\endgroup\$
  • \$\begingroup\$ Thank you. As far as your 3rd edit, yes, the camera is a child of the player, which is why I'm confused as to why it's not rotating with the player, but it may be because I need to use the transform rather than rigidbody like you said. I am trying to keep the camera "behindish" the player. I'll have to test your solution tonight, but I'll also add video to my question to better show what's going on. I'm definitely not skilled enough to try to do the calculation stuff. \$\endgroup\$ – Robert Nov 24 '16 at 18:14
-1
\$\begingroup\$

Just write mainCamera.forwar = player.forward in your FixedUpdate that will make the camera face the back of the player.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.