"how to make a fps player in unity" Code Answer's
You're definitely familiar with the best coding language C# that developers use to develop their projects and they get all their queries like "how to make a fps player in unity" answered properly. Developers are finding an appropriate answer about how to make a fps player in unity related to the C# coding language. By visiting this online portal developers get answers concerning C# codes question like how to make a fps player in unity. Enter your desired code related query in the search bar and get every piece of information about C# code related question on how to make a fps player in unity.
how to make a fps player in unity
public Camera playerCamera;
public float lookSpeed = 2.0f;
public float lookXLimit = 45.0f;
CharacterController characterController;
Vector3 moveDirection = Vector3.zero;
float rotationX = 0;
[HideInInspector]
public bool canMove = true;
void Start()
{
characterController = GetComponent<CharacterController>();
// Lock cursor
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update()
{
// We are grounded, so recalculate move direction based on axes
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 right = transform.TransformDirection(Vector3.right);
// Press Left Shift to run
bool isRunning = Input.GetKey(KeyCode.LeftShift);
float curSpeedX = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Vertical") : 0;
float curSpeedY = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Horizontal") : 0;
float movementDirectionY = moveDirection.y;
moveDirection = (forward * curSpeedX) + (right * curSpeedY);
if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
{
moveDirection.y = jumpSpeed;
}
else
{
moveDirection.y = movementDirectionY;
}
// Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
// when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
// as an acceleration (ms^-2)
if (!characterController.isGrounded)
{
moveDirection.y -= gravity * Time.deltaTime;
}
// Move the controller
characterController.Move(moveDirection * Time.deltaTime);
// Player and Camera rotation
if (canMove)
{
rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
}
}
}
All those coders who are working on the C# based application and are stuck on how to make a fps player in unity can get a collection of related answers to their query. Programmers need to enter their query on how to make a fps player in unity related to C# code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about how to make a fps player in unity for the programmers working on C# code while coding their module. Coders are also allowed to rectify already present answers of how to make a fps player in unity while working on the C# language code. Developers can add up suggestions if they deem fit any other answer relating to "how to make a fps player in unity". Visit this developer's friendly online web community, CodeProZone, and get your queries like how to make a fps player in unity resolved professionally and stay updated to the latest C# updates.