Playerをジャンプさせる
![D0dfd004 0d12 4e95 a25b e00948514c9c](https://codegenius.org/uploads/slide/image/8185/d0dfd004-0d12-4e95-a25b-e00948514c9c.jpeg)
ジャンプの実装
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float walkSpeed;
private float speed;
private Vector3 movement;
private CharacterController controller;
private int clickCount = 0;
public float runSpeed;
private Vector3 moveDir = Vector3.zero;
private float gravity = 3f;
// ★追加(ジャンプ)
public float jumpPower;
public AudioClip jumpSound;
private AudioSource audioS;
void Start()
{
// ★追加(ジャンプ)
audioS = GetComponent<AudioSource>();
controller = GetComponent<CharacterController>();
speed = walkSpeed;
}
void Update()
{
PlayerMove();
}
void PlayerMove()
{
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
movement = new Vector3(moveH, 0, moveV);
Vector3 desiredMove = Camera.main.transform.forward * movement.z + Camera.main.transform.right * movement.x;
moveDir.x = desiredMove.x * 3f;
moveDir.z = desiredMove.z * 3f;
moveDir.y -= gravity * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime * speed);
if (Input.GetKeyDown(KeyCode.W))
{
clickCount += 1;
Invoke("ResetCount", 0.3f);
}
if (Input.GetKeyUp(KeyCode.W))
{
speed = walkSpeed;
}
// ★追加(ジャンプ)
if (controller.isGrounded)
{
moveDir.y = 0;
if (Input.GetKeyDown(KeyCode.Space))
{
moveDir.y = jumpPower;
audioS.clip = jumpSound;
audioS.Play();
}
}
}
void ResetCount()
{
if (clickCount != 2)
{
clickCount = 0;
return;
}
else
{
clickCount = 0;
speed = runSpeed;
}
}
}
![2f1896af dfa8 4c32 9be3 053853381a40](https://codegenius.org/uploads/slide/image/8186/2f1896af-dfa8-4c32-9be3-053853381a40.jpeg)
![391b58b0 a724 4290 9226 fd57e03768d7](https://codegenius.org/uploads/slide/image/8187/391b58b0-a724-4290-9226-fd57e03768d7.jpeg)
【2021版】X_Mission(全34回)
他のコースを見る![D0dfd004 0d12 4e95 a25b e00948514c9c](https://codegenius.org/uploads/slide/image/8185/d0dfd004-0d12-4e95-a25b-e00948514c9c.jpeg)
ジャンプの実装
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float walkSpeed;
private float speed;
private Vector3 movement;
private CharacterController controller;
private int clickCount = 0;
public float runSpeed;
private Vector3 moveDir = Vector3.zero;
private float gravity = 3f;
// ★追加(ジャンプ)
public float jumpPower;
public AudioClip jumpSound;
private AudioSource audioS;
void Start()
{
// ★追加(ジャンプ)
audioS = GetComponent<AudioSource>();
controller = GetComponent<CharacterController>();
speed = walkSpeed;
}
void Update()
{
PlayerMove();
}
void PlayerMove()
{
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
movement = new Vector3(moveH, 0, moveV);
Vector3 desiredMove = Camera.main.transform.forward * movement.z + Camera.main.transform.right * movement.x;
moveDir.x = desiredMove.x * 3f;
moveDir.z = desiredMove.z * 3f;
moveDir.y -= gravity * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime * speed);
if (Input.GetKeyDown(KeyCode.W))
{
clickCount += 1;
Invoke("ResetCount", 0.3f);
}
if (Input.GetKeyUp(KeyCode.W))
{
speed = walkSpeed;
}
// ★追加(ジャンプ)
if (controller.isGrounded)
{
moveDir.y = 0;
if (Input.GetKeyDown(KeyCode.Space))
{
moveDir.y = jumpPower;
audioS.clip = jumpSound;
audioS.Play();
}
}
}
void ResetCount()
{
if (clickCount != 2)
{
clickCount = 0;
return;
}
else
{
clickCount = 0;
speed = runSpeed;
}
}
}
![2f1896af dfa8 4c32 9be3 053853381a40](https://codegenius.org/uploads/slide/image/8186/2f1896af-dfa8-4c32-9be3-053853381a40.jpeg)
![391b58b0 a724 4290 9226 fd57e03768d7](https://codegenius.org/uploads/slide/image/8187/391b58b0-a724-4290-9226-fd57e03768d7.jpeg)
Playerをジャンプさせる