Question by
bruinsquad3 · Mar 12, 2020 at 03:56 AM ·
errorplayerfps
My player won't move!,Why is my player not moving?
I have just got into Unity and have encountered my first problem. My player will not move, There are no error scripts in the console. I am using Unity 2018.3.14f1 as well as with Microsoft Studio 2015.
Code 1: Player_Motor { using UnityEngine;
[RequireComponent(typeof(Rigidbody))] public class Player_Motor : MonoBehaviour {
private Vector3 velocity = Vector3.zero;
private Rigidbody rb;
private Vector3 veloctiy;
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Gets a movement vector
public void Move(Vector3 _velocity)
{
veloctiy = _velocity;
}
//Run every physics interation
void FixedUpdate()
{
PeformMovement();
}
//Peform movement based on velocity variable
void PeformMovement()
{
if (velocity != Vector3.zero)
{
rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
}
}
}
Code 2: Player_Controller using UnityEngine; [RequireComponent(typeof(Player_Motor))] public class Player_Controller : MonoBehaviour {
[SerializeField]
private float speed = 5f;
private Player_Motor motor;
void Start()
{
motor = GetComponent<Player_Motor>();
}
void Update()
{
//Calculate movement velocity as a 3D vector
float _xMov = Input.GetAxisRaw("Horizontal");
float _zMov = Input.GetAxisRaw("Vertical");
Vector3 _movHorizontal = transform.right * _xMov;
Vector3 _movVertical = transform.forward * _zMov;
//Final movement vector
Vector3 _velocity = (_movHorizontal + _movVertical).normalized * speed;
//Apply movement
motor.Move(_velocity);
}
Comment
Answer by hnm938 · Mar 13, 2020 at 05:16 PM
Assuming you're trying to make an FPS Player Controller, try and use the Character Controller component.
Here is a simple Player Controller script using the Character Controller.
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public CharacterController characterController;
[Header("Camera")]
public Camera cam;
public float XSensitivity, YSensitivity;
public bool invertPitch, clamp;
public int minY, maxY;
[Header("Movement")]
public bool grounded, jumping;
public float walkSpeed, sprintSpeed, jumpPower, gravity;
[HideInInspector] public float jumpSpeed, verticalRotation, movementSpeed;
private void Start()
{
movementSpeed = walkSpeed;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X"), mouseY = Input.GetAxis("Mouse Y");
if (!invertPitch)
mouseY = -mouseY;
verticalRotation += mouseY * YSensitivity;
if (clamp)
verticalRotation = Mathf.Clamp(verticalRotation, minY, maxY);
transform.Rotate(0, mouseX * XSensitivity, 0);
cam.transform.localRotation = Quaternion.Euler(verticalRotation, 0, 0);
if (Input.GetMouseButtonDown(0))
Cursor.lockState = CursorLockMode.Locked;
float horizontal = Input.GetAxis("Horizontal"), forward = Input.GetAxis("Vertical");
grounded = characterController.isGrounded;
if (grounded)
{
jumping = false;
jumpSpeed = 0;
}
else
jumpSpeed -= (gravity * 25) * Time.deltaTime;
if (Input.GetAxisRaw("Jump") != 0 && !jumping)
{
jumping = true;
jumpSpeed = jumpPower;
}
if (Input.GetKey(KeyCode.LeftShift))
{
movementSpeed = sprintSpeed;
}
else if (Input.GetKeyUp(KeyCode.LeftShift))
{
movementSpeed = walkSpeed;
}
Vector3 motion = new Vector3(horizontal * movementSpeed, jumpSpeed, forward * movementSpeed);
characterController.Move((transform.rotation * motion) * Time.deltaTime);
}
}