Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Best player movement script in c# (3D)

Discussion in 'Scripting' started by J0K3R_12QQ, Oct 11, 2016.

Thread Status:
Not open for further replies.
  1. J0K3R_12QQ

    J0K3R_12QQ

    Joined:
    Oct 11, 2016
    Posts:
    4
    So I'm kinda new in Unity but i know the basics and I'm making a 3d adventure game.
    So I've created a movement script that used transform.Translate(Vector3.....) but I just think it is the worst way of doing this. So then I wrote a script with rigidbody.velocity = Vector3.forward.... but this also has some problems like for example you can only move in one direction so gravity doesn't work etc. and I know you can use something like rigidbody.velocity = new Vector3(... , ... , ...) but this way player doesn't move in the direction you're looking Can you help me with this? this is my current script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.  
    6.     public float playerSpeed;
    7.     public float sprintSpeed = 4f;
    8.     public float walkSpeed = 2f;
    9.     public float mouseSensitivity = 2f;
    10.     public float jumpHeight = 3f;
    11.     private bool isMoving = false;
    12.     private bool isSprinting =false;
    13.     private float yRot;
    14.  
    15.     private Animator anim;
    16.     private Rigidbody rigidBody;
    17.  
    18.     // Use this for initialization
    19.     void Start () {
    20.  
    21.         playerSpeed = walkSpeed;
    22.         anim = GetComponent<Animator>();
    23.         rigidBody = GetComponent<Rigidbody>();
    24.  
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update () {
    29.  
    30.         yRot += Input.GetAxis("Mouse X") * mouseSensitivity;
    31.         transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, yRot, transform.localEulerAngles.z);
    32.  
    33.         isMoving = false;
    34.  
    35.         if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
    36.         {
    37.             //transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * playerSpeed);
    38.             rigidBody.velocity += transform.right * Input.GetAxisRaw("Horizontal") * playerSpeed;
    39.             isMoving = true;
    40.         }
    41.         if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
    42.         {
    43.             //transform.Translate(Vector3.forward * Input.GetAxis("Vertical") * playerSpeed);
    44.             rigidBody.velocity += transform.forward * Input.GetAxisRaw("Vertical") * playerSpeed;
    45.             isMoving = true;
    46.         }
    47.  
    48.         if (Input.GetKeyDown(KeyCode.Space))
    49.         {
    50.             transform.Translate(Vector3.up * jumpHeight);
    51.         }
    52.  
    53.         if (Input.GetAxisRaw("Sprint") > 0f)
    54.         {
    55.             playerSpeed = sprintSpeed;
    56.             isSprinting = true;
    57.         }else if (Input.GetAxisRaw("Sprint") < 1f)
    58.         {
    59.             playerSpeed = walkSpeed;
    60.             isSprinting = false;
    61.         }
    62.  
    63.         anim.SetBool("isMoving", isMoving);
    64.         anim.SetBool("isSprinting", isSprinting);
    65.  
    66.     }
    67. }
    68.  
     
    zykeNovela likes this.
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,017
    Do you really need physics in your game? Does your character need to bump into things, knock things over, etc.? Or is he pretty much just moving around at a fixed height from the terrain (or whatever you're using as a floor)?

    I'm thinking the transform.Translate approach may actually be the right way to go — you may just need to supplement it with some sensors to keep it at the proper height above the floor, etc. But wrestling with the physics engine could certainly work too, if you are patient and persistent enough.
     
  3. J0K3R_12QQ

    J0K3R_12QQ

    Joined:
    Oct 11, 2016
    Posts:
    4
    See the height isn't my problem with transform.Translate. My main problem with it is that you can for example walk through walls if you're smart enough and i don't want that.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,017
    OK, so prevention of walking through walls. There are a couple ways to approach that.

    One is to use physics, and let the physics engine deal with it. This is attractive at first, but it doesn't lead to, for example, enabling monsters and such to navigate the world.

    For that you will need something like NavMesh (and related classes). And if you have that anyway, you can easily use it for your player character too. So, in your situation, I'd recommend you spend a couple days exploring Unity's navigation system, and see if that doesn't solve it for you.
     
  5. J0K3R_12QQ

    J0K3R_12QQ

    Joined:
    Oct 11, 2016
    Posts:
    4
    Thanks for your advice :)
     
  6. J0K3R_12QQ

    J0K3R_12QQ

    Joined:
    Oct 11, 2016
    Posts:
    4
    D
    Dude. I'm so stupid. I just changed the void Update to void FixedUpdate... And it worked... Thanks for your help.
     
  7. mrnebthicc

    mrnebthicc

    Joined:
    Apr 16, 2020
    Posts:
    1
    u copy and paste it
     
  8. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    259
    The Internet is full of useful tutorials.
    Search for example for "unity player move tutorial", it's way better than necroposting to old forum threads.
     
    JoeStrout likes this.
Thread Status:
Not open for further replies.
unityunity