Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CharacterMove : MonoBehaviour {
  6. public Transform cameraTransform;
  7.  
  8. public float moveSpeed = 10.0f;
  9. public float jumpSpeed = 10.0f;
  10. public float gravity = -20.0f;
  11.  
  12. CharacterController characterController = null;
  13. float yVelocity = 0.0f;
  14.  
  15. // Use this for initialization
  16. void Start () {
  17. characterController = GetComponent<CharacterController>();
  18. }
  19.  
  20. // Update is called once per frame
  21.  
  22. void Update()
  23. {
  24. float x = Input.GetAxis("Horizontal");
  25. float z = Input.GetAxis("Vertical");
  26.  
  27. Vector3 moveDirection = new Vector3(x, 0, z);
  28. moveDirection = cameraTransform.TransformDirection(moveDirection); //월드좌표계로 변환하여 moveDirection으로 넣음.
  29.  
  30. moveDirection *= moveSpeed;
  31. if (characterController.isGrounded)
  32. {
  33. yVelocity = 0.0f;
  34. if (Input.GetButtonDown("Jump")) //점프면
  35. {
  36. yVelocity = jumpSpeed; //점프 스피드를 y속도값에 넣음
  37. }
  38. }
  39. yVelocity += (gravity * Time.deltaTime); //중력에 의하여 점점 스피드가 줄어듬.
  40. moveDirection.y = yVelocity; //y값에 y속도값을 넣는다.
  41.  
  42. characterController.Move(moveDirection * Time.deltaTime); //컨트롤러.Move를 통하여 움직인다.
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement