- Home /
Question by
realyashjangid · Mar 08, 2020 at 12:33 AM ·
c#unity 5camerascripting problemplayer movement
Not able to make fps character controller player movement.,Not able to make Fps character controller player movement properly.
,I'm trying to make an FPS player controller but my player moves in the same direction no matters in which direction the camera is facing.
Here is my player movement script :
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
//Assignables
[SerializeField]
CharacterController controller;
// The movement
public float speed = 12f;
public float jumpHeight = 3f;
// The Gravity & GroundCheck
public float gravity = -10f;
public Transform groundCheck;
public float groundDistance = 0.2f;
public LayerMask groundMask;
Vector3 velocity;
bool isgrounded;
void Update()
{
IsGrounded();
PlayerMovement();
PlayerJumping();
}
void IsGrounded()
{
isgrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isgrounded && velocity.y < 0)
{
velocity.y = -2f; // If grounded then change the velocity to -2
}
}
void PlayerMovement()
{
float x = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
float y = Input.GetAxis("Vertical") * speed * Time.deltaTime;
Vector3 move = new Vector3(x, 0f, y);
controller.Move(move * speed * Time.deltaTime);
}
void PlayerJumping()
{
if (Input.GetButtonDown("Jump") && isgrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
Comment
Your answer
Follow this Question
Related Questions
How to detect an object which be in FOV of certain camera ? 1 Answer
Lock player movement within screen bounds 2 Answers
The raycasthit does not see the cillider when hitting it 2 Answers
"Only assignment, call, increment, decrement and new object expressions can be used as statements" 1 Answer
How to access camera preview frames with MediaCapture Class in Unity? 0 Answers