Search Unity

Player Movement Questions

Discussion in 'Scripting' started by argeunes, Jul 23, 2019.

  1. argeunes

    argeunes

    Joined:
    Feb 5, 2019
    Posts:
    20
    I am attempting to move my player forward, backward, left, and right based on the EDSF keys but not on an axis; rather, based on where the mouse is looking. I am having a lot of trouble figuring it out....either my player stays on the axis no matter where the mouse is looking or my player falls over or flips upside down because of the mouse look...please help!!
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    5,417
    Did you looked into tutorials?
    Please show us what you tried.
     
    angrypenguin likes this.
  3. argeunes

    argeunes

    Joined:
    Feb 5, 2019
    Posts:
    20
    I've tried tutorials on unity and udemy as well as youtube (I've been stuck on this for days --literally). I would say I am a beginner at C#..so I've tried transform/translate and Vector3. I currently have the following code...which 1. stays on the axes and 2. rotates the player to fall on its side if the mouse looks in a certain direction..

    Code (csharp):
    1. public class PlayerMovement : MonoBehaviour
    2. {
    3.     public float moveSpeed = 4.0f;
    4.     public float rotateSpeed = 100.0f;
    5.     public bool canMoveSideways = true;
    6.     public float gravity;
    7.     public float jumpForce = 500f;
    8.     public float runSpeed = 8.0f;
    9.     void Update ()
    10.     {
    11.         Movement();
    12.     }
    13.  
    14.     void Movement()
    15.     {
    16.         //move front to back
    17.         if (Input.GetKey(KeyCode.E))
    18.         {
    19.             Debug.Log("Key E Pressed.");
    20.             transform.position += Vector3.forward * Time.deltaTime * moveSpeed;
    21.         }
    22.         else if (Input.GetKey(KeyCode.D))
    23.         {
    24.             Debug.Log("Key D Presses.");
    25.             transform.position += Vector3.back * Time.deltaTime * moveSpeed;
    26.         }
    27.  
    28.      
    29.         //move side to side
    30.         if (Input.GetKey(KeyCode.S))
    31.         {
    32.                 Debug.Log("Key S Pressed.");
    33.                 transform.position += Vector3.left * Time.deltaTime * moveSpeed;
    34.         }
    35.         else if (Input.GetKey(KeyCode.F))
    36.         {
    37.             Debug.Log("Key F Pressed");
    38.             transform.position += Vector3.right * Time.deltaTime * moveSpeed;
    39.         }
    40.     }
    41.     void jump()
    42.     {
    43.         Vector3 Motion;
    44.         Motion = Vector3.zero;
    45.         CharacterController controller;
    46.         controller = gameObject.GetComponent<CharacterController>();
    47.         //jump
    48.         if (Input.GetKey(KeyCode.Space) || controller.isGrounded)
    49.         {
    50.             Motion.y = jumpForce;
    51.         }
    52.         Motion.y -= gravity * Time.deltaTime;
    53.         controller.Move(Motion * Time.deltaTime);
    54.     }

    It could be something simply wrong that I am doing, but I am open to any help. As I said, I am a beginner.
     
    Last edited: Jul 23, 2019
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    5,417
    Because you are using directly Vector3.someDirecton, like
    Code (CSharp):
    1. transform.position += Vector3.right * Time.deltaTime * moveSpeed;
    you simply move along axis, x (left/right).

    So you need some form of direction detection. Does your character rotates toward mouse?
    If so, you can use instead Vector3.right, use transform.right.

    There is multiple ways, to achieve the same mind.

    Also, please use
    Using code tags properly
    You can edit main post.
     
  5. argeunes

    argeunes

    Joined:
    Feb 5, 2019
    Posts:
    20
    The following code that I have is:
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class LookAtMouse : MonoBehaviour
    6. {
    7.     private Vector3 worldpos;
    8.     private float mouseX;
    9.     private float mouseY;
    10.     private float cameraDif;
    11.     public GameObject fpc;
    12.     void Start()
    13.     {
    14.         cameraDif = GetComponent<Camera>().transform.position.y - fpc.transform.position.y;
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         mouseX = Input.mousePosition.x;
    20.         mouseY = Input.mousePosition.y;
    21.         worldpos = GetComponent<Camera>().ScreenToWorldPoint(new Vector3(mouseX, mouseY, cameraDif));
    22.         Vector3 turretLookDirection = new Vector3(worldpos.x, fpc.transform.position.y, worldpos.z);
    23.         fpc.transform.LookAt(turretLookDirection);
    24.     }
    25. }
    AND

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public float moveSpeed = 4.0f;
    8.     public float rotateSpeed = 100.0f;
    9.     public bool canMoveSideways = true;
    10.     public float gravity;
    11.     public float jumpForce = 500f;
    12.     public float runSpeed = 8.0f;
    13.     Rigidbody rigidBody;
    14.     void Start()
    15.     {
    16.         //Fetch the Rigidbody component you attach from your GameObject
    17.         rigidBody = GetComponent<Rigidbody>();
    18.     }
    19. void Update()
    20.     {
    21.         if (Input.GetKey(KeyCode.E))
    22.         {
    23.             //Move the Rigidbody forwards constantly at speed you define (the blue arrow axis in Scene view)
    24.             rigidBody.velocity = transform.forward * moveSpeed;
    25.         }
    26.         if (Input.GetKey(KeyCode.D))
    27.         {
    28.             //Move the Rigidbody backwards constantly at the speed you define (the blue arrow axis in Scene view)
    29.             rigidBody.velocity = -transform.forward * moveSpeed;
    30.         }
    31.         if (Input.GetKey(KeyCode.F))
    32.         {
    33.             //Rotate the sprite about the Y axis in the positive direction
    34.             transform.Rotate(new Vector3(0, 1, 0) * Time.deltaTime * moveSpeed, Space.World);
    35.         }
    36.         if (Input.GetKey(KeyCode.S))
    37.         {
    38.             //Rotate the sprite about the Y axis in the negative direction
    39.             transform.Rotate(new Vector3(0, -1, 0) * Time.deltaTime * moveSpeed, Space.World);
    40.         }
    41.     }
    42. }

    I figured this out based on your last suggestion. So thank you!
    However, the "player" moves unnaturally...for instance, when looking down and to the side, it is a slight sideways view instead of the "player" rotating to accommodate. Is there a way to fix that? (image attached) Capture.PNG
     
    Last edited: Jul 24, 2019
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    5,417
    Looks much better.
    However, I can not picture in my mind, what exactly you mean by
    I got some thoughts, but would be better, if you describe, draw, or record short vid, to show what is going on.
     
  7. argeunes

    argeunes

    Joined:
    Feb 5, 2019
    Posts:
    20
    I just attached the image :)
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    5,417
    Just to confirm:
    1. When looking round (no movement), is all ok?
    2. When moving without looking round (look forward), is ok?
    3. When look round horizontally, is moving ok? Is moving sideways / forward ok?
    4. When look up down and moving, is the issue? Is moving sideways / forward ok / problem?
     
  9. argeunes

    argeunes

    Joined:
    Feb 5, 2019
    Posts:
    20
    I determined the issue ( I still had a different script attached, so both were trying to be used at the same time). However, now I have another issue; When I look down and use D for backward, I jump. Is there a way to fix that? (p.s. sorry for the many questions--I am greatly appreciative for your assistance!). Should I be adding isGrounded?
     
    Last edited: Jul 24, 2019
  10. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    5,417
    Is the same case, when you look up and move forward? Does it jump?
     
  11. argeunes

    argeunes

    Joined:
    Feb 5, 2019
    Posts:
    20
    Yes it does. Would an angle clamp be a valid solution?
     
  12. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    5,417
    There is multiple ways to solve the problem.

    Probably the easiest, is to have camera / head as child of character, and only allow to rotate it vertically.
    While body (parent) can rotate horizontally, with camera together.

    Clamping may cause, that you lose movement speed, higher you look.
    So if you look totally up, you wont be moving at all. If that makes sense?
     
  13. argeunes

    argeunes

    Joined:
    Feb 5, 2019
    Posts:
    20
    Perfect! Thanks again! I greatly appreciate it!
     
    Antypodish likes this.