top of page
Search

First Person Game

  • Writer: Keshav Batra
    Keshav Batra
  • Feb 11, 2021
  • 1 min read

Updated: Mar 2, 2021

At first I had trouble getting the jumping function to work and the movement to work, but then I realized that my ground was in the wrong place and that my jump code was in the wrong area. I also downloaded an environment because it was easier than making one. I hope that by the end of this project I can turn this into a first person shooter set in space.




Camera Movement: void Start()

{

Cursor.lockState = CursorLockMode.Locked;

}


// 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;



xRotation -= mouseY;

xRotation = Mathf.Clamp(xRotation, -90f, 90f);


transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);

playerBody.Rotate(Vector3.up * mouseX);

}

}



Player Movement and Jump: public CharacterController controller;

public float speed = 12f;

public float gravity = -9.81f;

public float jumpHeight = 3f;


public Transform groundCheck;

public float groundDistance = 0.4f;

public LayerMask groundMask;


Vector3 velocity;

bool isGrounded;

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

if (Input.GetButtonDown("Jump") && isGrounded)

{

velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);

}


controller.Move(move * speed* Time.deltaTime);


velocity.y += gravity * Time.deltaTime;


controller.Move(velocity * Time.deltaTime);

}

}


 
 
 

Comments


bottom of page