Search Unity

Hi guys, I got a problem with camera orientation

Discussion in 'Scripting' started by BrainOverFlow, Aug 16, 2017.

  1. BrainOverFlow

    BrainOverFlow

    Joined:
    Jul 26, 2017
    Posts:
    15
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class CameraController : MonoBehaviour {

    public int speed;
    public float sensitivity;
    public Camera eyes;

    private float moveFB;
    private float moveLR;


    private float rotX;
    private float rotY;

    CharacterController player;

    private void Start()
    {
    player = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update () {
    moveFB = Input.GetAxis("Vertical") * speed;
    moveLR = Input.GetAxis("Horizontal") * speed;


    rotX = Input.GetAxis("Mouse X") * sensitivity;
    rotY = Input.GetAxis("Mouse Y") * sensitivity;

    Vector3 movement = new Vector3(moveLR, 0, moveFB);
    movement = transform.rotation * movement;

    transform.Rotate(0, rotX, 0);
    eyes.transform.Rotate(-rotY, 0, 0);

    player.Move(movement * Time.deltaTime);

    }
    }



    Above is my Camera controller, I want to make my camera like a flying vehicle, but at current stage I can't change the y direction of my character(of course, y == 0 in Vector3 movement = new Vector3(moveLR, 0, moveFB);) I want to control my camera so it can move to any direction.(i.e. the camera can move diagonaly or translate or even roll).
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    for one... use some code tags. this will help us read your code better.

    for two... how are you going to define how the camera moves. I see where you pull the mouse x and y and apply it to the rotation of the character controller and camera. I can only assume that the camera is a child of the character so that it will move with the character.

    for three... when you create the movement variable, you are using the current rotation of the transform to gather the direction, when you should be using the rotation after the mouse movement. (move the transform.Rotate... line above it)

    If you goal is to separate the camera completely from the player model, then you need a different kind of script. (a free movement script) This script would use WASD and mouse movement to move the camera.

    The only part I cannot see you working with right now is Roll. You do not seem to have any controls for roll in your script.
     
  3. BrainOverFlow

    BrainOverFlow

    Joined:
    Jul 26, 2017
    Posts:
    15
    Haha Thanks for the suggestions, TBH this is my first time post on the unity community.

    I will define the camera move in the way below:
    1: use wasd to cantrol the movement of the camera. But it is reletively fwd back, left and right, depends on the orientation of the camera. I've already achieved the left and right part. But, my forward and backward is still move in the same plane.(i.e. no matter the camera is facing down or up, it's still move forward and backward). I know there is something to do with the y direction but I can't figure it out though. :(

    For the third point u mentioned, I can't really see the difference if I move the "transform.Rotate(0, rotX, 0);" above the "movement = transform.rotation * movement;". I am pretty sure there is a difference, but I can't tell the diff after changeing the code when I enter the play mode.

    And for now, I don't have any code about roll, but I will add soon.

    Thank you very much.
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    OK, so I kind of see what you are trying, how ever, your code is referencing things that are kind of planar. When you use Movement = new Vector3(x, 0, y)... that makes it planar.

    So, that being said. What you are asking for is actually incredibly simple. What I did was to basically rebuild your code with the concept that I want the camera to be independent of a player.

    So barring any stupid syntax errors (which you may get) I think this code is correct:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class CameraIn3D : MonoBehaviour {
    7.     public float speed = 5;
    8.     public float sensitivity = 20;
    9.     // Update is called once per frame
    10.     void Update () {
    11.         var moveFB = Input.GetAxis("Vertical") * speed;
    12.         var moveLR = Input.GetAxis("Horizontal") * speed;
    13.         var moveUD = (Input.GetKey(KeyCode.Space) ? 1 : input.GetKey(KeyCode.X) : -1 : 0) * speed;
    14.        
    15.         var move = new Vector3(moveLR, moveUD, moveLR);
    16.        
    17.         rotX = Input.GetAxis("Mouse X") * sensitivity;
    18.         rotY = Input.GetAxis("Mouse Y") * sensitivity;
    19.         rotZ = (Inut.GetKey(KeyCode.Q) ? 1 : Input.GetKey(KeyCode.E) ? -1 : 0) * sensitivity;
    20.        
    21.         var rot = new Vector3(rotX, rotY, rotZ);
    22.         transform.Rotate(rot  * Time.deltaTime, Space.Self);
    23.         transform.Translate(move * Time.deltaTime , Space.Self);
    24.     }
    25. }
     
  5. BrainOverFlow

    BrainOverFlow

    Joined:
    Jul 26, 2017
    Posts:
    15
    Oh that's exactly what I want. Thanks for helping!!