Best way of moving player
Hi. I'm making some sort of Minecraft clone (i know... i know...) and I have question about player movement. Every method I used has the same problem. If i fall down and stick into wall my player is glitching or even stopping.
What is the best way to control the player and avoid these problems? I could disable moving if player is not touching ground but I want some control in air and it is important because if i jump i want to move in air to the next block.
Answer by whereswaldo · Jan 28, 2017 at 05:42 PM
I'd recommend using unity's First Person Character Controller (which can be found in the 'characters' folder when you go to the import assets tab in the editor) with a sphere collider around the camera. It's simple but should fix all the issues you're having.
Answer by Loui_Studios · Jan 28, 2017 at 03:36 PM
Well, there is really no best way to control a character on screen. It all depends on what kind of game you're making.
Since you're making a Minecraft clone, I don't think realism or physics-based movement matters that much, so I'd recommend a simple First Person Character Controller.
Attach a character controller to your player, and then create a c# script called FPSController.
Just paste my code into it and the rest is pretty straightforward.
using UnityEngine;
public class FPSController : 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 forwardSpeed, horizontalSpeed, jumpPower, gravity;
[HideInInspector] public float jumpSpeed, verticalRotation;
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;
}
Vector3 motion = new Vector3(horizontal * horizontalSpeed, jumpSpeed, forward * forwardSpeed);
characterController.Move((transform.rotation * motion) * Time.deltaTime);
}
}
Let me know how this works for you, and if you have any problems!
Thank You but I don't want to use Character Controller because it has capsule collider, it's $$anonymous$$ecraft style game so i want to use box collider for player collider
I don't think that would be very much fun to use. Besides, doesn't $$anonymous$$inecraft already use a capsule collider?
I love this code. Thank you. I got this to work with the following Heirarchy
Player ( 3D Object(s) )
|-------- PlayerPOV (Camera)
Your answer
Follow this Question
Related Questions
How do I make my player's movement relative to Main Camera's direction? 1 Answer
Save/Load Player System 0 Answers
How can i make my player look at the same direction where its moving? [JOYSTICK ANDROID] 0 Answers
Player not moving in the right direction instantly 1 Answer
How do i ajust the player height acording to the height of the roof in a first person game? 1 Answer