Search Unity

Changing movement based on camera direction

Discussion in 'Scripting' started by Alienchild, Aug 31, 2011.

  1. Alienchild

    Alienchild

    Joined:
    Oct 14, 2010
    Posts:
    364
    Hi

    I'm sitting here contemplating a way to solve a rather difficult (to a newbie like myself) problem; I have a script that moves a player around. I also have a camera that can rotate based on mouse movement. However I would always want the forward from the camera to be the forward of the movement for the player. As far as Ive come is to maybe draw a ray from the camera transform and to the player transform. That direction is always the forward direction. Would this be correct? How would I this change the player movement? I have no idea how to tackle it :(

    Code (csharp):
    1. var MoveSpeed : float;
    2.  
    3. function Update ()
    4. {
    5.     // MOVEMENT
    6.     translationVertical = Input.GetAxis ("Vertical");
    7.     translationVertical = translationVertical * MoveSpeed;
    8.     translationVertical *= Time.deltaTime;
    9.     rigidbody.position -= Vector3(0,0,translationVertical);
    10.  
    11.     translationHorizontal = Input.GetAxis ("Horizontal");
    12.     translationHorizontal = translationHorizontal * MoveSpeed;
    13.     translationHorizontal *= Time.deltaTime;
    14.     rigidbody.position -= Vector3(translationHorizontal,0,0);
    15. }
     
  2. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    You need to convert from the camera's local space to world space. A simple way to do this is by using TransformDirection.

    If your camera is looking down you might need to compensate for that, like dropping the y component of the final direction and normalizing it before applying the speed multiplication.
     
    Last edited: Aug 31, 2011
  3. Alienchild

    Alienchild

    Joined:
    Oct 14, 2010
    Posts:
    364
    Thank you very much Jasper, I now got it working exactly like I want it. In case someone else stumbles upon this post, this is how the movement code ended like. Oh and dont forget to get a reference to the camera (the camera look script is not included as it was not written by me).

    Code (csharp):
    1.    
    2. translationVertical = Input.GetAxis ("Vertical");
    3. translationVertical = translationVertical * MoveSpeed;
    4. translationVertical *= Time.deltaTime;
    5.  
    6. translationHorizontal = Input.GetAxis ("Horizontal");
    7. translationHorizontal = translationHorizontal * MoveSpeed;
    8. translationHorizontal *= Time.deltaTime;
    9.  
    10. var cameraRelative : Vector3 = theCamera.main.transform.TransformDirection (0, 0, -1);
    11. rigidbody.position -= Vector3((cameraRelative.x * translationVertical), 0,(cameraRelative.z * translationVertical) );
    12. cameraRelative = theCamera.main.transform.TransformDirection (-1, 0, 0);
    13.  
    14. rigidbody.position -= Vector3((cameraRelative.x * translationHorizontal), 0,(cameraRelative.z * translationHorizontal) );
     
  4. magma

    magma

    Joined:
    Jan 11, 2011
    Posts:
    6
    Very nice. I personally like to move characters as ridigbodies with forces.

    Code (JavaScript):
    1. var speed : float = 5.0;
    2. var movementVector : Vector2 = Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
    3. var force = speed * movementVector.magnitude;
    4. var direction : Vector3 = movementVector.y * mainCamera.forward + movementVector.x * mainCamera.right;
    5.        
    6. rigidbody.AddForce(direction * force);