|
学习如何创建一个脚本通过键盘输入控制角色
PlayerMovement
C#- using UnityEngine;
- using System.Collections;
- public class PlayerMovement : MonoBehaviour
- {
- public AudioClip shoutingClip; // 呼喊的声音片段.
- public float turnSmoothing = 15f; // 玩家转身的平滑参数.
- public float speedDampTime = 0.1f; // 速度的阻尼系数
-
-
- private Animator anim; // 动画组件变量.
- private HashIDs hash; // 哈希表组件变量.
-
-
- void Awake ()
- {
- // 初始化变量
- anim = GetComponent<Animator>();
- hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<HashIDs>();
-
- // 设置shouting 层的权重为 1.
- anim.SetLayerWeight(1, 1f);
- }
-
-
- void FixedUpdate ()
- {
- // 获取输入.
- float h = Input.GetAxis("Horizontal");
- float v = Input.GetAxis("Vertical");
- bool sneak = Input.GetButton("Sneak");
-
- MovementManagement(h, v, sneak);
- }
-
-
- void Update ()
- {
- // 获取呼喊的输入.
- bool shout = Input.GetButtonDown("Attract");
-
- // 设置shouting变量.
- anim.SetBool(hash.shoutingBool, shout);
-
- AudioManagement(shout);
- }
-
-
- void MovementManagement (float horizontal, float vertical, bool sneaking)
- {
- // 根据输入设置当前是否进入潜行状态.
- anim.SetBool(hash.sneakingBool, sneaking);
- // 如果这个时候有输入...
- if(horizontal != 0f || vertical != 0f)
- {
- // ... 那么根据输入控制玩家转向,并设定玩家的速度为5.5.
- Rotating(horizontal, vertical);
- anim.SetFloat(hash.speedFloat, 5.5f, speedDampTime, Time.deltaTime);
- }
- else
- // ..其他情况设置玩家速度为0.
- anim.SetFloat(hash.speedFloat, 0);
- }
-
-
- void Rotating (float horizontal, float vertical)
- {
- // 根据键盘输入的值创建一个新的向量targetDirection
- Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
-
- // 计算出targetDirection绕着y轴旋转后的四元素
- Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
-
- // 从玩家当前的朝向到新的朝向间做四元数插值平滑过渡
- Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
-
- // 将平滑旋转角度重新赋给玩家,设定其当前的旋转朝向
- rigidbody.MoveRotation(newRotation);
- }
-
-
- void AudioManagement (bool shout)
- {
- // 如果玩家是在奔跑状态...
- if(anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.locomotionState)
- {
- // ... 如果脚步声没有响起...
- if(!audio.isPlaying)
- // ... 播放脚步声.
- audio.Play();
- }
- else
- // 停止播放脚步声.
- audio.Stop();
-
- // 如果玩家的shouting状态被激活...
- if(shout)
- // ... 就播放呼喊的声音片段.
- AudioSource.PlayClipAtPoint(shoutingClip, transform.position);
- }
- }
复制代码 JS- #pragma strict
- public var shoutingClip : AudioClip; // Audio clip of the player shouting.
- public var turnSmoothing : float = 15f; // A smoothing value for turning the player.
- public var speedDampTime : float = 0.1f; // The damping for the speed parameter
- private var anim : Animator; // Reference to the animator component.
- private var hash : HashIDs; // Reference to the HashIDs.
- function Awake ()
- {
- // Setting up the references.
- anim = GetComponent(Animator);
- hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent(HashIDs);
-
- // Set the weight of the shouting layer to 1.
- anim.SetLayerWeight(1, 1f);
- }
- function FixedUpdate ()
- {
- // Cache the inputs.
- var h : float = Input.GetAxis("Horizontal");
- var v : float = Input.GetAxis("Vertical");
- var sneak : boolean = Input.GetButton("Sneak");
-
- MovementManagement(h, v, sneak);
- }
- function Update ()
- {
- // Cache the attention attracting input.
- var shout : boolean = Input.GetButtonDown("Attract");
-
- // Set the animator shouting parameter.
- anim.SetBool(hash.shoutingBool, shout);
-
- AudioManagement(shout);
- }
- function MovementManagement (horizontal : float, vertical : float, sneaking : boolean)
- {
- // Set the sneaking parameter to the sneak input.
- anim.SetBool(hash.sneakingBool, sneaking);
-
- // If there is some axis input...
- if(horizontal != 0f || vertical != 0f)
- {
- // ... set the players rotation and set the speed parameter to 5.5f.
- Rotating(horizontal, vertical);
- anim.SetFloat(hash.speedFloat, 5.5f, speedDampTime, Time.deltaTime);
- }
- else
- // Otherwise set the speed parameter to 0.
- anim.SetFloat(hash.speedFloat, 0);
- }
- function Rotating (horizontal : float, vertical : float)
- {
- // Create a new vector of the horizontal and vertical inputs.
- var targetDirection : Vector3 = new Vector3(horizontal, 0f, vertical);
-
- // Create a rotation based on this new vector assuming that up is the global y axis.
- var targetRotation : Quaternion = Quaternion.LookRotation(targetDirection, Vector3.up);
-
- // Create a rotation that is an increment closer to the target rotation from the player's rotation.
- var newRotation : Quaternion = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
-
- // Change the players rotation to this new rotation.
- rigidbody.MoveRotation(newRotation);
- }
- function AudioManagement (shout : boolean)
- {
- // If the player is currently in the run state...
- if(anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.locomotionState)
- {
- // ... and if the footsteps are not playing...
- if(!audio.isPlaying)
- // ... play them.
- audio.Play();
- }
- else
- // Otherwise stop the footsteps.
- audio.Stop();
-
- // If the shout input has been pressed...
- if(shout)
- // ... play the shouting clip where we are.
- AudioSource.PlayClipAtPoint(shoutingClip, transform.position);
- }
复制代码 |
|