Search Unity

first person movement

Discussion in 'Scripting' started by SlinkyBanana, Sep 17, 2017.

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

    SlinkyBanana

    Joined:
    Sep 17, 2017
    Posts:
    6
    I am new to unity and am trying to set up a basic first person movement controller. While the rotation(looking around works, I can get the movement to work right. Can someone fix my script? Here it is:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerMovement : MonoBehaviour
    {

    public float speed = 15;
    public float lookSpeed = 200;

    private Rigidbody rig;

    void Start ()
    {
    rig = GetComponent<Rigidbody>();
    }
    void Update()
    {
    //movement
    float hAxis = Input.GetAxis("Horizontal");
    float vAxis = Input.GetAxis("Vertical");

    Vector3 hMove = hAxis * transform.right;
    Vector3 vMove = vAxis * transform.up;
    Vector3 Movement = (hMove + vMove).normalized * speed;

    rig.MovePosition (Movement);

    //looking//
    float MousehAxis = Input.GetAxis("Mouse X");

    Vector3 look = new Vector3(0, MousehAxis, 0) * lookSpeed * Time.deltaTime;

    rig.MoveRotation(rig.rotation * Quaternion.Euler(look));
    }
    }
     
  2. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Hi! Welcome to Unity and the forums :)

    To make it easier for us to read your code, please properly insert it: https://forum.unity.com/threads/using-code-tags-properly.143875/

    You're showing good effort but it's too much. It's really this simple:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TestFPS : MonoBehaviour {
    4.  
    5.     #region "Variables"
    6.     public Rigidbody Rigid;
    7.     public float MouseSensitivity;
    8.     public float MoveSpeed;
    9.     public float JumpForce;
    10.     #endregion
    11.    
    12.     void Update ()
    13.     {
    14.         Rigid.MoveRotation(Rigid.rotation * Quaternion.Euler(new Vector3(0, Input.GetAxis("Mouse X") * MouseSensitivity, 0)));
    15.         Rigid.MovePosition(transform.position + (transform.forward * Input.GetAxis("Vertical") * MoveSpeed) + (transform.right * Input.GetAxis("Horizontal") * MoveSpeed));
    16.         if (Input.GetKeyDown("space"))
    17.             Rigid.AddForce(transform.up * JumpForce);
    18.     }
    19. }
    20.  
    The jumping is there as an example; you'll need to have a system to prevent infinite jump height from many key presses if you want jumping.

    Also, check out the FPS controller in the Standard Assets!
     
    iydrss and williamlayadi like this.
  3. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Galaxy_World likes this.
  4. NUTFAIRY

    NUTFAIRY

    Joined:
    Jan 28, 2020
    Posts:
    1
    its that easy huh?
     
  5. DaDonik

    DaDonik

    Joined:
    Jun 17, 2013
    Posts:
    258
    Yeah it's pretty easy to necro old threads for no purpose at all...
     
    undevable likes this.
  6. Pratibhan

    Pratibhan

    Joined:
    Apr 24, 2020
    Posts:
    1
    You can check out Brackey's fps movement. He is the overpowered legend of C# and unity
     
  7. otisdagama

    otisdagama

    Joined:
    May 13, 2020
    Posts:
    1
    Im starting a code for first person control, can someone check if its ok so far (Its my first time)

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Mouselook : MonoBehaviour
    {

    public float mouseSensitivity - 100f;

    public Transform playerbody;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
    float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
     
    AdenBoi likes this.
  8. shrekgreen1

    shrekgreen1

    Joined:
    May 17, 2020
    Posts:
    1
    ne manqk taka ni stawa
     
  9. imakegamesalot

    imakegamesalot

    Joined:
    May 27, 2020
    Posts:
    2
    hey guys, I want good first person movement with the rigidbody component, please help anyone
     
  10. imakegamesalot

    imakegamesalot

    Joined:
    May 27, 2020
    Posts:
    2
    nevermind, I chose the character controller component, how do I get good first person movement that way?
     
  11. wolfcoder1

    wolfcoder1

    Joined:
    Jun 18, 2020
    Posts:
    1
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerMovementScript : MonoBehaviour
    {

    public CharacterController controller;

    public float speed = 12f;
    public float gravity = -9.81f;
    public float jumpHight = 3f;

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    Vector3 velocity;
    bool isGrounded;


    // Update is called once per frame
    void Update()
    {

    isGrounded = Physics.CheckSphere(groundCheck.position,groundDistance,groundMask);

    if (isGrounded && velocity.y < 0)
    {
    velocity.y = -2f;
    }

    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");

    Vector3 move = transform.right * x + transform.forward * z;

    controller.Move(move * speed * Time.deltaTime);

    velocity.y += gravity * Time.deltaTime;

    controller.Move(velocity * Time.deltaTime);

    if (Input.GetButtonDown("Jump") && isGrounded)
    {
    velocity.y = Mathf.Sqrt(jumpHight * -2f * gravity);
    }
    }
    }


    My first fps controller could someone fix the jump and repost
     
    Elliot2005 likes this.
  12. - please open your own thread, it's free of charge
    - please use code tags when you post code: https://forum.unity.com/threads/using-code-tags-properly.143875/
    - please state your problem
    - please describe what have you tried so far and why is it not working
    - state what to expect to happen
    - no, no one will take your code, fix it and repost, unless you pay for it, how much do you offer?
     
  13. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Everything that Ninja pointed out, plus this:

    How to report problems productively in the Unity3D forums:

    http://plbm.com/?p=220

    Help us to help you.
     
    alysonlupo likes this.
  14. No_Such_Luck

    No_Such_Luck

    Joined:
    Jul 29, 2020
    Posts:
    1
    Yeah, ig. He moves to fast though.
     
  15. josephhortel

    josephhortel

    Joined:
    Sep 1, 2020
    Posts:
    1
    Hi I just started using unity and i want to make a full fps player that is rigidbody can some body help me pls
     
  16. phantom11232

    phantom11232

    Joined:
    Oct 14, 2020
    Posts:
    1
    check out Dani's FPS movement
     
  17. aidanocabe

    aidanocabe

    Joined:
    Nov 16, 2020
    Posts:
    1
    it keeps falling down
     
  18. Chaos1107

    Chaos1107

    Joined:
    Sep 3, 2020
    Posts:
    1
    Freeze rotations in rigidbody under constraints
     
Thread Status:
Not open for further replies.