first person movement

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();
}
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));
}
}

Hi! Welcome to Unity and the forums :slight_smile:

To make it easier for us to read your code, please properly insert it: Using code tags properly

You’re showing good effort but it’s too much. It’s really this simple:

using UnityEngine;

public class TestFPS : MonoBehaviour {

    #region "Variables"
    public Rigidbody Rigid;
    public float MouseSensitivity;
    public float MoveSpeed;
    public float JumpForce;
    #endregion
   
    void Update ()
    {
        Rigid.MoveRotation(Rigid.rotation * Quaternion.Euler(new Vector3(0, Input.GetAxis("Mouse X") * MouseSensitivity, 0)));
        Rigid.MovePosition(transform.position + (transform.forward * Input.GetAxis("Vertical") * MoveSpeed) + (transform.right * Input.GetAxis("Horizontal") * MoveSpeed));
        if (Input.GetKeyDown("space"))
            Rigid.AddForce(transform.up * JumpForce);
    }
}

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!

2 Likes

This is one of those times it’s good to do a search and see what turns up and find code examples.

1 Like

its that easy huh?

2 Likes

Yeah it’s pretty easy to necro old threads for no purpose at all…

1 Like

You can check out Brackey’s fps movement. He is the overpowered legend of C# and unity

2 Likes

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;

1 Like

ne manqk taka ni stawa

hey guys, I want good first person movement with the rigidbody component, please help anyone

nevermind, I chose the character controller component, how do I get good first person movement that way?

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

1 Like
  • please open your own thread, it’s free of charge
  • please use code tags when you post code: https://discussions.unity.com/t/481379
  • 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?
4 Likes

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.

1 Like

Yeah, ig. He moves to fast though.

Hi I just started using unity and i want to make a full fps player that is rigidbody can some body help me pls

check out Dani’s FPS movement

3 Likes

it keeps falling down

Freeze rotations in rigidbody under constraints