Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

using System.Collections; using System.Collections.Generic; using UnityEngine; [ RequireComponent ( typeof ( CharacterController ) ) ] public class Player : MonoBehaviour { Animator animator; public Camera

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class Player : MonoBehaviour
{
Animator animator;
public Camera playerCamera;
public float walkSpeed;
public float runSpeed;
public float jumpPower;
public float gravity;
public float lookSpeed;
public float lookXLimit;
Vector3 moveDirection = Vector3.zero;
float rotationX =0;
public bool canMove = true;
bool NewBool = false;
CharacterController characterController;
// Start is called before the first frame update
Rigidbody rb;
Player player;
void Start()
{
rb = GetComponent();
characterController = GetComponent();
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
animator = GetComponent();
Debug.Log(animator);
}
// Update is called once per frame
void Update()
{
// while (Input.GetKey(KeyCode.W)|| Input.GetKey(KeyCode.UpArrow)|| Input.GetKey(KeyCode.S)|| Input.GetKey(KeyCode.DownArrow)){
// if (Input.GetKey(KeyCode.W)|| Input.GetKey(KeyCode.UpArrow))
//{
// NewBool = true; //=1;// This will move the player to the walking position(check the transition condition)
//}
// else
//{
// NewBool = false;
//}
//}
// Hadle Movement
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 ? runSpeed : walkSpeed)* Input.GetAxis("Vertical") : 0;
float curSpeedY = canMove ?(isRunning ? runSpeed : walkSpeed)* Input.GetAxis("Horizontal") : 0;
float movementDirectionY = moveDirection.y;
moveDirection =(forward * curSpeedX)+(right * curSpeedY);
//region Handles Jumping
if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
{
moveDirection.y = jumpPower;
}
else
{
moveDirection.y = movementDirectionY;
}
if (!characterController.isGrounded)
{
moveDirection.y -= gravity * Time.deltaTime;
}
// Region Handles Rotation
characterController.Move(moveDirection * Time.deltaTime);
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);
}
}
}
explain this code for me

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft SQL Server 2012 Unleashed

Authors: Ray Rankins, Paul Bertucci

1st Edition

0133408507, 9780133408508

More Books

Students also viewed these Databases questions

Question

Describe qualitative data with graphs (2.1)

Answered: 1 week ago