【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 1459|回复: 0

5-4 Player Movement控制玩家的运动

[复制链接]
发表于 2013-10-1 23:10:04 | 显示全部楼层 |阅读模式
学习如何创建一个脚本通过键盘输入控制角色
PlayerMovement
C#
  1. using UnityEngine;
  2. using System.Collections;

  3. public class PlayerMovement : MonoBehaviour
  4. {
  5.         public AudioClip shoutingClip;      // 呼喊的声音片段.
  6.         public float turnSmoothing = 15f;   // 玩家转身的平滑参数.
  7.         public float speedDampTime = 0.1f;  // 速度的阻尼系数
  8.        
  9.        
  10.         private Animator anim;              // 动画组件变量.
  11.         private HashIDs hash;               // 哈希表组件变量.
  12.        
  13.        
  14.         void Awake ()
  15.         {
  16.                 // 初始化变量
  17.                 anim = GetComponent<Animator>();
  18.                 hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<HashIDs>();
  19.                
  20.                 // 设置shouting 层的权重为 1.
  21.                 anim.SetLayerWeight(1, 1f);
  22.         }
  23.        
  24.        
  25.         void FixedUpdate ()
  26.         {
  27.                 // 获取输入.
  28.                 float h = Input.GetAxis("Horizontal");
  29.                 float v = Input.GetAxis("Vertical");
  30.                 bool sneak = Input.GetButton("Sneak");
  31.                
  32.                 MovementManagement(h, v, sneak);
  33.         }
  34.        
  35.        
  36.         void Update ()
  37.         {
  38.                 // 获取呼喊的输入.
  39.                 bool shout = Input.GetButtonDown("Attract");
  40.                
  41.                 // 设置shouting变量.
  42.                 anim.SetBool(hash.shoutingBool, shout);
  43.                
  44.                 AudioManagement(shout);
  45.         }
  46.        
  47.        
  48.         void MovementManagement (float horizontal, float vertical, bool sneaking)
  49.         {
  50.                 // 根据输入设置当前是否进入潜行状态.
  51.                 anim.SetBool(hash.sneakingBool, sneaking);
  52.                 // 如果这个时候有输入...
  53.                 if(horizontal != 0f || vertical != 0f)
  54.                 {
  55.                         // ... 那么根据输入控制玩家转向,并设定玩家的速度为5.5.
  56.                         Rotating(horizontal, vertical);
  57.                         anim.SetFloat(hash.speedFloat, 5.5f, speedDampTime, Time.deltaTime);
  58.                 }
  59.                 else
  60.                         // ..其他情况设置玩家速度为0.
  61.                         anim.SetFloat(hash.speedFloat, 0);
  62.         }
  63.        
  64.        
  65.         void Rotating (float horizontal, float vertical)
  66.         {
  67.                 // 根据键盘输入的值创建一个新的向量targetDirection
  68.                 Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
  69.                
  70.                 // 计算出targetDirection绕着y轴旋转后的四元素
  71.                 Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
  72.                
  73.                 // 从玩家当前的朝向到新的朝向间做四元数插值平滑过渡
  74.                 Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
  75.                
  76.                 // 将平滑旋转角度重新赋给玩家,设定其当前的旋转朝向
  77.                 rigidbody.MoveRotation(newRotation);
  78.         }
  79.        
  80.        
  81.         void AudioManagement (bool shout)
  82.         {
  83.                 // 如果玩家是在奔跑状态...
  84.                 if(anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.locomotionState)
  85.                 {
  86.                         // ... 如果脚步声没有响起...
  87.                         if(!audio.isPlaying)
  88.                                 // ... 播放脚步声.
  89.                                 audio.Play();
  90.                 }
  91.                 else
  92.                         // 停止播放脚步声.
  93.                         audio.Stop();
  94.                
  95.                 // 如果玩家的shouting状态被激活...
  96.                 if(shout)
  97.                         // ... 就播放呼喊的声音片段.
  98.                         AudioSource.PlayClipAtPoint(shoutingClip, transform.position);
  99.         }
  100. }
复制代码
JS
  1. #pragma strict

  2. public var shoutingClip : AudioClip;        // Audio clip of the player shouting.
  3. public var turnSmoothing : float = 15f;     // A smoothing value for turning the player.
  4. public var speedDampTime : float = 0.1f;    // The damping for the speed parameter


  5. private var anim : Animator;                // Reference to the animator component.
  6. private var hash : HashIDs;             // Reference to the HashIDs.


  7. function Awake ()
  8. {
  9.     // Setting up the references.
  10.     anim = GetComponent(Animator);
  11.     hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent(HashIDs);
  12.    
  13.     // Set the weight of the shouting layer to 1.
  14.     anim.SetLayerWeight(1, 1f);
  15. }


  16. function FixedUpdate ()
  17. {
  18.     // Cache the inputs.
  19.     var h : float = Input.GetAxis("Horizontal");
  20.     var v : float = Input.GetAxis("Vertical");
  21.     var sneak : boolean = Input.GetButton("Sneak");
  22.    
  23.     MovementManagement(h, v, sneak);
  24. }


  25. function Update ()
  26. {
  27.     // Cache the attention attracting input.
  28.     var shout : boolean = Input.GetButtonDown("Attract");
  29.    
  30.     // Set the animator shouting parameter.
  31.     anim.SetBool(hash.shoutingBool, shout);
  32.    
  33.     AudioManagement(shout);
  34. }


  35. function MovementManagement (horizontal : float, vertical : float, sneaking : boolean)
  36. {
  37.     // Set the sneaking parameter to the sneak input.
  38.     anim.SetBool(hash.sneakingBool, sneaking);
  39.    
  40.     // If there is some axis input...
  41.     if(horizontal != 0f || vertical != 0f)
  42.     {
  43.         // ... set the players rotation and set the speed parameter to 5.5f.
  44.         Rotating(horizontal, vertical);
  45.         anim.SetFloat(hash.speedFloat, 5.5f, speedDampTime, Time.deltaTime);
  46.     }
  47.     else
  48.         // Otherwise set the speed parameter to 0.
  49.         anim.SetFloat(hash.speedFloat, 0);
  50. }


  51. function Rotating (horizontal : float, vertical : float)
  52. {
  53.     // Create a new vector of the horizontal and vertical inputs.
  54.     var targetDirection : Vector3 = new Vector3(horizontal, 0f, vertical);
  55.    
  56.     // Create a rotation based on this new vector assuming that up is the global y axis.
  57.     var targetRotation : Quaternion = Quaternion.LookRotation(targetDirection, Vector3.up);
  58.    
  59.     // Create a rotation that is an increment closer to the target rotation from the player's rotation.
  60.     var newRotation : Quaternion = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
  61.    
  62.     // Change the players rotation to this new rotation.
  63.     rigidbody.MoveRotation(newRotation);
  64. }


  65. function AudioManagement (shout : boolean)
  66. {
  67.     // If the player is currently in the run state...
  68.     if(anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.locomotionState)
  69.     {
  70.         // ... and if the footsteps are not playing...
  71.         if(!audio.isPlaying)
  72.             // ... play them.
  73.             audio.Play();
  74.     }
  75.     else
  76.         // Otherwise stop the footsteps.
  77.         audio.Stop();
  78.    
  79.     // If the shout input has been pressed...
  80.     if(shout)
  81.         // ... play the shouting clip where we are.
  82.         AudioSource.PlayClipAtPoint(shoutingClip, transform.position);
  83. }
复制代码

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2024-9-28 00:19

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表