歩行とランを切り替える
移動速度を2段階で切り替える
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace FPS{
public enum PlayerState{
Idle, Walking, Running, Jumping
}
[RequireComponent(typeof(CharacterController), typeof(AudioSource))]
public class PlayerController : MonoBehaviour {
[Range(0.1f, 2f)]
public float walkSpeed = 1.5f;
[Range(0.1f, 10f)]
public float runSpeed = 3.5f;
private CharacterController charaController;
void Start () {
charaController = GetComponent<CharacterController> ();
}
void Update () {
Move ();
}
void Move(){
float moveH = Input.GetAxis ("Horizontal");
float moveV = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveH, 0, moveV);
if (movement.sqrMagnitude > 1) {
movement.Normalize ();
}
// ★修正
// 歩行とランを切り替える。
if (Input.GetKey (KeyCode.LeftShift)) {
charaController.Move (movement * Time.fixedDeltaTime * runSpeed);
} else {
charaController.Move (movement * Time.fixedDeltaTime * walkSpeed);
}
}
}
}
EscapeCombat
他のコースを見る移動速度を2段階で切り替える
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace FPS{
public enum PlayerState{
Idle, Walking, Running, Jumping
}
[RequireComponent(typeof(CharacterController), typeof(AudioSource))]
public class PlayerController : MonoBehaviour {
[Range(0.1f, 2f)]
public float walkSpeed = 1.5f;
[Range(0.1f, 10f)]
public float runSpeed = 3.5f;
private CharacterController charaController;
void Start () {
charaController = GetComponent<CharacterController> ();
}
void Update () {
Move ();
}
void Move(){
float moveH = Input.GetAxis ("Horizontal");
float moveV = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveH, 0, moveV);
if (movement.sqrMagnitude > 1) {
movement.Normalize ();
}
// ★修正
// 歩行とランを切り替える。
if (Input.GetKey (KeyCode.LeftShift)) {
charaController.Move (movement * Time.fixedDeltaTime * runSpeed);
} else {
charaController.Move (movement * Time.fixedDeltaTime * walkSpeed);
}
}
}
}
歩行とランを切り替える