Our Website : http://npsroboticsclub.cf/index.html
| usingSystem.Collections; | |
| usingSystem.Collections.Generic; | |
| usingUnityEngine; | |
| [RequireComponent(typeof(CharacterController))] | |
| publicclassmovementscript:MonoBehaviour | |
| { | |
| publicfloatwalkingSpeed=7.5f; | |
| publicfloatrunningSpeed=11.5f; | |
| publicfloatjumpSpeed=14.0f; | |
| publicfloatgravity=-1.62f; | |
| publicCameraplayerCamera; | |
| publicfloatlookSpeed=2.0f; | |
| publicfloatlookXLimit=45.0f; | |
| CharacterControllercharacterController; | |
| Vector3moveDirection=Vector3.zero; | |
| floatrotationX=0; | |
| [HideInInspector] | |
| publicboolcanMove=true; | |
| voidStart() | |
| { | |
| characterController=GetComponent<CharacterController>(); | |
| // Lock cursor | |
| Cursor.lockState=CursorLockMode.Locked; | |
| Cursor.visible=false; | |
| } | |
| voidUpdate() | |
| { | |
| // We are grounded, so recalculate move direction based on axes | |
| Vector3forward=transform.TransformDirection(Vector3.forward); | |
| Vector3right=transform.TransformDirection(Vector3.right); | |
| // Press Left Shift to run | |
| boolisRunning=Input.GetKey(KeyCode.LeftShift); | |
| floatcurSpeedX=canMove?(isRunning?runningSpeed:walkingSpeed)*Input.GetAxis("Vertical"):0; | |
| floatcurSpeedY=canMove?(isRunning?runningSpeed:walkingSpeed)*Input.GetAxis("Horizontal"):0; | |
| floatmovementDirectionY=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); | |
| } | |
| } | |
| } |