Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. //CharacterControllerを入れる
  5. [RequireComponent(typeof(CharacterController))]
  6.  
  7. public class FirstPerson : MonoBehaviour
  8. {
  9. private float speed;
  10. //落ちる速さ、重力
  11. public float gravity = 10f;
  12. private float jumpSpeed;
  13.  
  14. //通常時のスピードとジャンプ力
  15. public float normalSpeed = 5;
  16. public float normalJump = 10;
  17. //左Shift押したときのスピードとジャンプ力
  18. public float shiftSpeed = 10;
  19. public float shiftJump = 20;
  20.  
  21. //Playerの移動や向く方向を入れる
  22. Vector3 moveDirection;
  23.  
  24. //CharacterControllerを変数にする
  25. CharacterController controller;
  26.  
  27. //マウスの横縦に動かす速さ
  28. public float horizontalSpeed = 2.0F;
  29. public float verticalSpeed = 2.0F;
  30.  
  31. //Main Cameraを入れる
  32. GameObject came;
  33.  
  34. //ジャンプボタン離したことを判定
  35. //二段ジャンプを防ぐため
  36. private bool jumpUpEnd = false;
  37.  
  38. //ジャンプボタン押している時間を判定
  39. //その時間を制限するため
  40. [SerializeField] float jumpTime;
  41.  
  42. void Start()
  43. {
  44. //CharacterControllerを取得
  45. controller = GetComponent<CharacterController>();
  46.  
  47. //Main Cameraを検索し子オブジェクトにしてPlayerに設置する
  48. came = Camera.main.gameObject;
  49. came.transform.parent = this.transform;
  50. //カメラを目線の高さにする
  51. came.transform.localPosition = new Vector3(0, 0.4f, 0);
  52. //カメラの向きをこのオブジェクトと同じにする
  53. came.transform.rotation = this.transform.rotation;
  54. }
  55.  
  56. void Update()
  57. {
  58. //左右どちらかのShift押した場合と離している場合
  59. if (Input.GetKey(KeyCode.LeftShift)
  60. || Input.GetKey(KeyCode.RightShift))
  61. {
  62. speed = shiftSpeed;
  63. jumpSpeed = shiftJump;
  64. }
  65. else
  66. {
  67. speed = normalSpeed;
  68. jumpSpeed = normalJump;
  69. }
  70.  
  71. //マウスでカメラの向きとPlayerの横の向きを変える
  72. float h = horizontalSpeed * Input.GetAxis("Mouse X");
  73. float v = verticalSpeed * Input.GetAxis("Mouse Y");
  74. transform.Rotate(0, h, 0);
  75. came.transform.Rotate(v, 0, 0);
  76.  
  77. //Playerが地面に設置していることを判定
  78. if (controller.isGrounded)
  79. {
  80. //XZ軸の移動と向きを代入する
  81. //WASD,上下左右キー
  82. moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0,
  83. Input.GetAxis("Vertical"));
  84. moveDirection = transform.TransformDirection(moveDirection);
  85. moveDirection *= speed;
  86.  
  87. // Y軸方向にジャンプさせる
  88. if (Input.GetButtonDown("Jump"))
  89. moveDirection.y = jumpSpeed;
  90.  
  91. //ジャンプ中に関わる変数
  92. //地面についている時にリセット
  93. jumpUpEnd = false;
  94. jumpTime = 0;
  95. } else
  96. {
  97. //ジャンプボタン押していると上昇
  98. //押している時間3秒まで、jumpUpEndがfalseの場合有効
  99. if (Input.GetButton("Jump") && jumpTime < 3f && !jumpUpEnd)
  100. {
  101. //ジャンプボタン押している秒数を加算
  102. jumpTime += Time.deltaTime;
  103. moveDirection.y = jumpSpeed;
  104. }
  105.  
  106. //ジャンプ中にジャンプボタン離したことを記録
  107. //jumpUpEndがfalseの場合有効
  108. if (Input.GetButtonUp("Jump") && !jumpUpEnd)
  109. {
  110. //二回ジャンプできなくする
  111. jumpUpEnd = true;
  112. }
  113. }
  114.  
  115. // 重力を設定しないと落下しない
  116. moveDirection.y -= gravity * Time.deltaTime;
  117.  
  118. // Move関数に代入する
  119. controller.Move(moveDirection * Time.deltaTime);
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement