プレーヤーを走らせる
走る機能の実装
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
[RequireComponent(typeof(AudioSource))]
public class PlayerController : MonoBehaviour
{
public float walkSpeed;
// ★追加
public float runSpeed;
private Vector3 movement;
private CharacterController characterController;
private AudioSource audioSource;
private void Start()
{
characterController = GetComponent<CharacterController>();
audioSource = GetComponent<AudioSource>();
}
private void Update()
{
PlayerMove();
}
void PlayerMove()
{
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
movement = new Vector3(moveH, 0, moveV);
// ★改良(walkとrunを切り替える)
if (Input.GetKey(KeyCode.LeftShift))
{
// 左シフトキーを押しながら移動すると走る。
characterController.Move(movement * Time.deltaTime * runSpeed);
}
else
{
characterController.Move(movement * Time.deltaTime * walkSpeed);
}
}
}
【2019版】X_Mission(基礎/全51回)
他のコースを見る走る機能の実装
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
[RequireComponent(typeof(AudioSource))]
public class PlayerController : MonoBehaviour
{
public float walkSpeed;
// ★追加
public float runSpeed;
private Vector3 movement;
private CharacterController characterController;
private AudioSource audioSource;
private void Start()
{
characterController = GetComponent<CharacterController>();
audioSource = GetComponent<AudioSource>();
}
private void Update()
{
PlayerMove();
}
void PlayerMove()
{
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
movement = new Vector3(moveH, 0, moveV);
// ★改良(walkとrunを切り替える)
if (Input.GetKey(KeyCode.LeftShift))
{
// 左シフトキーを押しながら移動すると走る。
characterController.Move(movement * Time.deltaTime * runSpeed);
}
else
{
characterController.Move(movement * Time.deltaTime * walkSpeed);
}
}
}
プレーヤーを走らせる