Search Unity

Obtaining Camera Forward Facing Vector

Discussion in 'Scripting' started by CoffeeConundrum, Dec 30, 2013.

  1. CoffeeConundrum

    CoffeeConundrum

    Joined:
    Dec 30, 2013
    Posts:
    46
    Hi there,

    I've recently started to learn Unity and have decided to make a small game with First Person Camera (saves extra coding and animation!) and while I've managed to get the camera moving with the mouse, I'm having difficulties with the actual movement of the camera with WASD keys. I understand that I need to obtain the vector to tell the camera which way is forward and can then determine the left vector(I think this is correct?) and from there I can begin to move the camera.

    Since I'm new to Unity I was wondering how I would accomplish this. After having a look at this myself it's seemed to go a bit over my head!

    Thanks in advance!
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,445
  3. CoffeeConundrum

    CoffeeConundrum

    Joined:
    Dec 30, 2013
    Posts:
    46
    Thanks for the reply mgear! I'm now just having difficulty on trying to figure out how to apply these to the camera to move it within my scene. Any help with that?
     
  4. Loulabelette

    Loulabelette

    Joined:
    Dec 11, 2013
    Posts:
    34
    I'm not used with FPS but I think the application should be the same.
    I suggest you to create a cube, attach the camera to hit and move the cube instead of the camera directly.
    Why ? Because first, you can switch camera if you wants to and you won't have to make a clone of this one with the script, secondly, you can apply, (I think) more effect and component to a cube and easily use it game Object.

    a command like this.transform.forward can give you the cube forward vector (you can hide it if you wants to but really, this cube is usefull in debugging, colllision test and else).
     
  5. Deleted User

    Deleted User

    Guest

    this is pretty straight forward. You just multiply the forward Vector with your speed and the delta time ( makes it Framerate indepandent ) and add ir to your position and your camera will move forward. Same goes for right with the right Vector

    Code (csharp):
    1.  
    2.  
    3. myCameraTransform.position += myCameraTransform.forward * speed * Time.deltaTime
    4.  
    5.  
     
  6. CoffeeConundrum

    CoffeeConundrum

    Joined:
    Dec 30, 2013
    Posts:
    46
    element_wsc - What is your myCameraTranform varible defined as ?

    EDIT : Nevermind, defined it as a Tranform and it's working.

    Thanks very much!
     
    Last edited: Dec 30, 2013
  7. Deleted User

    Deleted User

    Guest

    its the Transform of your Camera!

    if the script is attached to your Camera itself, you can use transform ( lower case ) instead. But you should cache it anyways.

    example ( out of my head and might have typos )

    Code (csharp):
    1.  
    2.  
    3. public class MoveCam : Monobehavior
    4. {
    5.     private float speed = 5f;
    6.     private Transform myCameraTransform;
    7.  
    8.     private void Start()
    9.     {
    10.         myCameraTransform = transform;
    11.     }
    12.  
    13.     private void Update()
    14.     {
    15.         if( Input.GetKey( KeyCode.W ) )
    16.         {
    17.             myCameraTransform.position += myCameraTransform.forward * speed * Time.deltaTime;
    18.         }
    19.     }
    20. }
    21.  
    22.  
     
  8. CoffeeConundrum

    CoffeeConundrum

    Joined:
    Dec 30, 2013
    Posts:
    46
    Thanks again for the help element_wsc! Appreciate it.