Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Move relative to camera but not vertical

Discussion in 'Scripting' started by Hero101, Jul 16, 2015.

  1. Hero101

    Hero101

    Joined:
    Jul 14, 2015
    Posts:
    158
    Hey guys,

    I have my player which is an empty game object with a capsule collider and rigidbody. The player has a child which is the camera. I have a script attached to the camera that is essentially for a mouse look. I have a playercontroller script attached to the player that I want to use to control the player's movement. I'm struggling with getting the player to move towards the "forward" direction the camera is facing but only on the horizontal axis (I the player is looking up or down with the mouse I don't want the player to start flying up or down).

    Any help would be greatly appreciated. I've been googling solutions for hours but each one I test has yet to work out. I must be missing something.
     
  2. EliteWalrus

    EliteWalrus

    Joined:
    Aug 16, 2014
    Posts:
    44
    I'm personally not familiar with the playercontroller script but if you want to get the forward direction of the camera try:

    Camera.main.transform.forward
    (You could also just store the camera in a Transform variable and use: variableName.forward)

    Remember:
    Transform.forward is local space.
    Vector3.forward is global space.
     
  3. Hero101

    Hero101

    Joined:
    Jul 14, 2015
    Posts:
    158
    Me either I am not using the playercontroller script I was making my own. It did not cross my mind to use a Transform variable I will look into it.

    Second question if you don't mind: Is it possible to get the Input.GetAxis("Horizontal") of a different game object that the script isn't attached to? I tried something like:
    public GameObject cam;
    float h = cam.Input.GetAxis("Horizontal");

    But that obviously threw all kinds of errors haha I'm essentially wondering if my player object/class can get that horizontal axis imformation from the camera in the scene (which is a child of the player)?
     
  4. EliteWalrus

    EliteWalrus

    Joined:
    Aug 16, 2014
    Posts:
    44
    Input.GetAxis ("Horizontal") actually only returns a float value between -1 and 1 depending on the buttons your pressing (a and d). The idea would be to multiply a direction by the input data and then multiply that by another float to change the speed.

    Try something like:
    myRigidbody.MovePosition(transform.position + camera.transform.forward * Input.GetAxis ("Horizontal") * speed * Time.deltaTime); //Time.deltaTime will make it move at the same speed regardless of your framerate

    I'm still a little confused about your goal here, but you might want to use the "Vertical" axis instead.
     
  5. Hero101

    Hero101

    Joined:
    Jul 14, 2015
    Posts:
    158
    Yes Vertical is what I meant haha

    All of this wouldn't be an issue if I could just control the camera's movement as the player. But I had to create an empty object with the capsule collider and make the camera a parent of it. I did this because when I had the camera as the only object for the player with a collider the collider would rotate with the mouse look and I can't have that happen. Sorry if that is confusing. Nonetheless, thank you for your suggestion it gives me something to play with.
     
  6. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Just zero out the Y component of the camera's transform forward.

    Code (csharp):
    1.  
    2. Vector3 direction = Camera.main.transform.forward;
    3. direction.y = 0;
    4.  
    5. transform.position += direction * speed * Time.deltaTime;
    6.  
     
    JGameMaker92 likes this.
  7. Hero101

    Hero101

    Joined:
    Jul 14, 2015
    Posts:
    158
    Yes that's it! Didn't realize I could zero it out that way. Thank you so much for your help dude :)
     
    GroZZleR likes this.
  8. JGameMaker92

    JGameMaker92

    Joined:
    Oct 15, 2014
    Posts:
    83
    Almost 5 years later and that was the answer to my problem too! Thanks! Was searching for hours.