Search Unity

First Person move in direction camera is facing

Discussion in 'Scripting' started by soulclaimed, Aug 1, 2015.

  1. soulclaimed

    soulclaimed

    Joined:
    Jul 18, 2015
    Posts:
    3
    Hi

    I have been searching everywhere for an answer to this but i just cant get any of the code suggestions to work as i am an absolute beginner and im just getting my head round things in order to start developing for android and cardboard.

    I have a code from a tutorial for movement which is below but need help finalizing it by getting it to move in the direction of the camera

    please could someone fix the code or explain to me how to fix it as ive been following several tutorials and tried the prefabs that all break in android.

    Heres the code:
    [code ]
    public class PlayerController : MonoBehaviour {

    public float speed = 6.0F;
    public float gravity = 20.0F;

    private Vector3 moveDirection = Vector3.zero;
    public CharacterController controller;

    void Start(){
    // Store reference to attached component
    controller = GetComponent<CharacterController>();
    }

    void Update()
    {
    // Character is on ground (built-in functionality of Character Controller)
    if (controller.isGrounded)



    {
    // Use input up and down for direction, multiplied by speed
    moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    moveDirection = transform.TransformDirection(moveDirection);
    moveDirection *= speed;

    }
    // Apply gravity manually.
    moveDirection.y -= gravity * Time.deltaTime;
    // Move Character Controller
    controller.Move(moveDirection * Time.deltaTime);
    }
    }
    [/code ]


    any help will be greatly appreciated
     
    Last edited: Aug 1, 2015
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    [code ][/code ] tags, as is stickied at the top of every forum here.
     
  3. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Attach the camera to the player, have a script to rotate the player based on mouse. Then the player rotates, the camera is always facing forward of the character so moving forward will always be the direction the camera is facing
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    If you want to move the character relative to the rotation of the camera rather than relative to its own current rotation, then you need to adjust:
    Code (csharp):
    1. moveDirection = transform.TransformDirection(moveDirection);
    for which "transform" means "current object's position/rotation" to be:
    Code (csharp):
    1. moveDirection = Camera.main.transform.TransformDirection(moveDirection);
    so that the input is being compared to the camera's transform and not the object's.
     
    williampatricklobis2 and AcrosM like this.
  5. soulclaimed

    soulclaimed

    Joined:
    Jul 18, 2015
    Posts:
    3
    you are amazing, thanks so much
     
  6. Stavros-Dimou

    Stavros-Dimou

    Joined:
    Nov 18, 2013
    Posts:
    42
    That kind of worked, but not perfectly.
    You see, while the movement now indeed follows the camera, it is also following it if the player looks up, meaning the player will fly.
     
  7. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Zeroing out the Y value of the euler angle for the rotation should fix that I think, before applying it to the character's movement, but frankly if you're going to have an adjustable camera that way, then movement based on the camera direction probably isn't a great idea. Well, it probably wasn't a great idea in the OP's case either, but he specifically asked how to make the character move from the camera's direction, so that's what I gave him, and he seemed happy enough with it. *shrugs*

    My first instinct here, as always, is just parent the camera to the character- that's the starting point for most camera controllers for a reason. If you need linked positional values but disparate rotational values, then have the character and camera both be separate children of a root object, and move the root object for the position while the camera and character both self-rotate.

    Anyways, this thread is really old now, and there's a good chance that your requirements for movement and camera controls are different- if only subtly. Perhaps you should explain exactly what you want so there can be a discussion of how to accomplish that, rather than forcing this solution to work for you.
     
  8. Stavros-Dimou

    Stavros-Dimou

    Joined:
    Nov 18, 2013
    Posts:
    42
    Yes, you are right.
    Well what I personally want is to move my character with physics enabled, so he doesn't walk up steep cliffs or can climb walls by jumping on them, and at the same time I want my player to be able to jump.

    But from what I read, Move() allows you to jump but leaves it up to you to implement gravity, while SimpleMove() gives you gravity but doesn't allow you to jump.

    I made some jump / gravity code but there is a bug. The bug is that some times the player is being pulled backwards. Not downwards, backwards. I mean the exact opposite direction from where the player is looking. Not that this is not what happens all the time, but only every now and then.

    Check this here:

    This is the initialization of the float verticalVelocity, which is my 'gravity'.
    Code (CSharp):
    1.         if (playerCharController.isGrounded == false)
    2.             verticalVelocity += Physics.gravity.y * (Time.deltaTime * 2);
    3.         else
    4.             verticalVelocity = 0;
    If the player presses the jump button, I add "opposite gravity" to the float, that will help him jump in the air.
    Code (CSharp):
    1.         if (Input.GetButtonDown("Jump") && canJump == true && curStam > 10)
    2.         {
    3.             verticalVelocity += jumpUnits;
    4.             GetComponentInParent<PlayerCharacterInfo>().StaminaPoints -= 10f;
    5.             canJump = false;
    6.         }
    And then I apply the jump / gravity.
    Code (CSharp):
    1. playerCharController.Move(new Vector3(0, verticalVelocity, 0) * Time.deltaTime);


    When I start play mode the character walks well, jumps, gets pulled downwards etc. But sometimes at what seems to me to be random, he starts getting pulled backwards.