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
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.
Well, at least I am learning to write my own first person movement script. For some reason I can only move backwards. Forwards does nothing, and I can strafe very very slowly. I am writing this for my multiplayer sorta test that uses PUN. public...
1 Like
DaDonik
January 28, 2020, 11:52am
5
NUTFAIRY:
its that easy huh?
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
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
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
Freeze rotations in rigidbody under constraints