unity3D---跑酷小游戏(三)

最后一篇,为游戏添加动画

在window窗口中点击animator,找到movement,将run-static右键单击,设置为set as layer default state;(即人物进入游戏就开始跑)

 上图表示,在walk-static到run-static中,只有满足速度高于0.5才可以;如果出现,人物进入游戏后,只跑动一下,就停止了,这有可能是速度设定的原因,当速度小于0.5时,就不会跑了,可以在movement状态下将speed_f这个参数改成1.0;

出现了当人物撞击障碍物时倒下了和人物跑的很慢,而背景移动的太快的现象;

解决人物倒下的问题:在Player中,组件Rigidbody中的Constraints属性中的,Freeze Rotation XYZ全部勾选,这样人物倒下,将不会出现旋转;

解决人物与背景移动速度不匹配的问题,如何调节人物动画的速度(即调整动画的播放速度):

在animator中,点击run_static,在inspector中,修改speed;

出现人物跳跃的时候,还继续在空中奔跑的效果和游戏结束之后,人物脚步依然在运动的问题;

解决人物起跳后继续在空中奔跑的动作,设置变量,用变量来控制,用变量来触发产生动作的条件;

 1 public class playerController : MonoBehaviour
 2 {
 3     private Rigidbody rb;
 4     //设置动画的变量
 5     private Animator anim;
 6     public  float jumpForce;
 7     public float gravityModifier;
 8     public bool isGround;//默认是false;
 9     public bool isGameOver;
10     // Start is called before the first frame update
11     void Start()
12     {
13         rb = GetComponent<Rigidbody>();
14         //给动画变量赋值
15         anim = GetComponent<Animator>();
16         //增加物体本身的重力,加快回落的速度
17         Physics.gravity *= gravityModifier;
18     }
19 
20     // Update is called once per frame
21     void Update()
22     {
23         if (Input.GetKeyDown(KeyCode.Space)&&isGround) {
24             rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
25             isGround = false;
26             //转换跑和跳,jump_trig被触发
27             anim.SetTrigger("Jump_trig");
28 
29         }
30        
31     }
32     //刚刚触碰物体时
33     private void OnCollisionEnter(Collision other)//除本身之外碰撞的物体
34     {
35         //通过地面标签获取
36         if (other.gameObject.CompareTag("Ground"))
37         {
38             isGround = true;
39         }
40         //通过障碍物的标签获取,触碰到障碍物的时候,游戏结束,在console中显示Game Over
41         if (other.gameObject.CompareTag("Obstacle")) {
42             isGameOver = true;
43             Debug.Log("Game Over!!");
44         }
45     }
46 }

解决人物碰撞到障碍物后,人物脚步依然在动,即设置人物死亡后的状态:

上图中,是为了使圆圈中的状态发生转变,箭头指向的是为了使状态转变,需要达到的条件;

 1 public class playerController : MonoBehaviour
 2 {
 3     private Rigidbody rb;
 4     //设置动画的变量
 5     private Animator anim;
 6     public  float jumpForce;
 7     public float gravityModifier;
 8     public bool isGround;//默认是false;
 9     public bool isGameOver;
10     // Start is called before the first frame update
11     void Start()
12     {
13         rb = GetComponent<Rigidbody>();
14         //给动画变量赋值
15         anim = GetComponent<Animator>();
16         //增加物体本身的重力,加快回落的速度
17         Physics.gravity *= gravityModifier;
18     }
19 
20     // Update is called once per frame
21     void Update()
22     {
23         if (Input.GetKeyDown(KeyCode.Space)&&isGround) {
24             rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
25             isGround = false;
26             //转换跑和跳,jump_trig被触发
27             anim.SetTrigger("Jump_trig");
28 
29         }
30        
31     }
32     //刚刚触碰物体时
33     private void OnCollisionEnter(Collision other)//除本身之外碰撞的物体
34     {
35         //通过地面标签获取
36         if (other.gameObject.CompareTag("Ground"))
37         {
38             isGround = true;
39         }
40         //通过障碍物的标签获取,触碰到障碍物的时候,游戏结束,在console中显示Game Over
41         if (other.gameObject.CompareTag("Obstacle")) {
42             isGameOver = true;
43             Debug.Log("Game Over!!");
44             //人物死亡时的动画效果
45             anim.SetBool("Death_b",true);
46             anim.SetInteger("DeathType_int",1);
47         }
48     }
49 }

添加粒子特效:

将粒子特效拖拽到Hierarchy中,由于希望这个粒子特效可以跟随着人物的跑动,所以可以将它拖拽成Player的子集,实现跟着人物跑动;父物体的子集是会跟着父物体进行运动变化的。注意粒子特效inspector中的looping(循环)和play on Awake(游戏开始就换唤醒);

解决人物跳跃在空中时,粒子特效不会消失的问题,以及添加人物死亡后粒子特效爆炸的效果,用代码控制解决:

注意在这里声明两个粒子特效变量的时候,变量的赋值(组件的获得)要回到unity中拖拽;

 1 public class playerController : MonoBehaviour
 2 {
 3     private Rigidbody rb;
 4     //设置动画的变量
 5     private Animator anim;
 6 
 7     public ParticleSystem dirtParticle, explotionParticle;
 8     public  float jumpForce;
 9     public float gravityModifier;
10     public bool isGround;//默认是false;
11     public bool isGameOver;
12     // Start is called before the first frame update
13     void Start()
14     {
15         rb = GetComponent<Rigidbody>();
16         //给动画变量赋值
17         anim = GetComponent<Animator>();
18         //增加物体本身的重力,加快回落的速度
19         Physics.gravity *= gravityModifier;
20     }
21 
22     // Update is called once per frame
23     void Update()
24     {
25         if (Input.GetKeyDown(KeyCode.Space)&&isGround) {
26             rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
27             isGround = false;
28             //转换跑和跳,jump_trig被触发
29             anim.SetTrigger("Jump_trig");
30             //粒子特效停止
31             dirtParticle.Stop();
32 
33         }
34        
35     }
36     //刚刚触碰物体时
37     private void OnCollisionEnter(Collision other)//除本身之外碰撞的物体
38     {
39         //通过地面标签获取
40         if (other.gameObject.CompareTag("Ground"))
41         {
42             isGround = true;
43             //粒子特效播放
44             dirtParticle.Play();
45         }
46         //通过障碍物的标签获取,触碰到障碍物的时候,游戏结束,在console中显示Game Over
47         if (other.gameObject.CompareTag("Obstacle")) {
48             isGameOver = true;
49             Debug.Log("Game Over!!");
50             //人物死亡时的动画效果
51             anim.SetBool("Death_b",true);
52             anim.SetInteger("DeathType_int",1);
53             //粒子特效播放
54             explotionParticle.Play();
55         }
56     }
57 }

 

------------恢复内容开始------------

最后一篇,为游戏添加动画

在window窗口中点击animator,找到movement,将run-static右键单击,设置为set as layer default state;(即人物进入游戏就开始跑)

 上图表示,在walk-static到run-static中,只有满足速度高于0.5才可以;如果出现,人物进入游戏后,只跑动一下,就停止了,这有可能是速度设定的原因,当速度小于0.5时,就不会跑了,可以在movement状态下将speed_f这个参数改成1.0;

出现了当人物撞击障碍物时倒下了和人物跑的很慢,而背景移动的太快的现象;

解决人物倒下的问题:在Player中,组件Rigidbody中的Constraints属性中的,Freeze Rotation XYZ全部勾选,这样人物倒下,将不会出现旋转;

解决人物与背景移动速度不匹配的问题,如何调节人物动画的速度(即调整动画的播放速度):

在animator中,点击run_static,在inspector中,修改speed;

出现人物跳跃的时候,还继续在空中奔跑的效果和游戏结束之后,人物脚步依然在运动的问题;

解决人物起跳后继续在空中奔跑的动作,设置变量,用变量来控制,用变量来触发产生动作的条件;

 1 public class playerController : MonoBehaviour
 2 {
 3     private Rigidbody rb;
 4     //设置动画的变量
 5     private Animator anim;
 6     public  float jumpForce;
 7     public float gravityModifier;
 8     public bool isGround;//默认是false;
 9     public bool isGameOver;
10     // Start is called before the first frame update
11     void Start()
12     {
13         rb = GetComponent<Rigidbody>();
14         //给动画变量赋值
15         anim = GetComponent<Animator>();
16         //增加物体本身的重力,加快回落的速度
17         Physics.gravity *= gravityModifier;
18     }
19 
20     // Update is called once per frame
21     void Update()
22     {
23         if (Input.GetKeyDown(KeyCode.Space)&&isGround) {
24             rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
25             isGround = false;
26             //转换跑和跳,jump_trig被触发
27             anim.SetTrigger("Jump_trig");
28 
29         }
30        
31     }
32     //刚刚触碰物体时
33     private void OnCollisionEnter(Collision other)//除本身之外碰撞的物体
34     {
35         //通过地面标签获取
36         if (other.gameObject.CompareTag("Ground"))
37         {
38             isGround = true;
39         }
40         //通过障碍物的标签获取,触碰到障碍物的时候,游戏结束,在console中显示Game Over
41         if (other.gameObject.CompareTag("Obstacle")) {
42             isGameOver = true;
43             Debug.Log("Game Over!!");
44         }
45     }
46 }

解决人物碰撞到障碍物后,人物脚步依然在动,即设置人物死亡后的状态:

上图中,是为了使圆圈中的状态发生转变,箭头指向的是为了使状态转变,需要达到的条件;

 1 public class playerController : MonoBehaviour
 2 {
 3     private Rigidbody rb;
 4     //设置动画的变量
 5     private Animator anim;
 6     public  float jumpForce;
 7     public float gravityModifier;
 8     public bool isGround;//默认是false;
 9     public bool isGameOver;
10     // Start is called before the first frame update
11     void Start()
12     {
13         rb = GetComponent<Rigidbody>();
14         //给动画变量赋值
15         anim = GetComponent<Animator>();
16         //增加物体本身的重力,加快回落的速度
17         Physics.gravity *= gravityModifier;
18     }
19 
20     // Update is called once per frame
21     void Update()
22     {
23         if (Input.GetKeyDown(KeyCode.Space)&&isGround) {
24             rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
25             isGround = false;
26             //转换跑和跳,jump_trig被触发
27             anim.SetTrigger("Jump_trig");
28 
29         }
30        
31     }
32     //刚刚触碰物体时
33     private void OnCollisionEnter(Collision other)//除本身之外碰撞的物体
34     {
35         //通过地面标签获取
36         if (other.gameObject.CompareTag("Ground"))
37         {
38             isGround = true;
39         }
40         //通过障碍物的标签获取,触碰到障碍物的时候,游戏结束,在console中显示Game Over
41         if (other.gameObject.CompareTag("Obstacle")) {
42             isGameOver = true;
43             Debug.Log("Game Over!!");
44             //人物死亡时的动画效果
45             anim.SetBool("Death_b",true);
46             anim.SetInteger("DeathType_int",1);
47         }
48     }
49 }

添加粒子特效:

将粒子特效拖拽到Hierarchy中,由于希望这个粒子特效可以跟随着人物的跑动,所以可以将它拖拽成Player的子集,实现跟着人物跑动;父物体的子集是会跟着父物体进行运动变化的。注意粒子特效inspector中的looping(循环)和play on Awake(游戏开始就换唤醒);

解决人物跳跃在空中时,粒子特效不会消失的问题,以及添加人物死亡后粒子特效爆炸的效果,用代码控制解决:

注意在这里声明两个粒子特效变量的时候,变量的赋值(组件的获得)要回到unity中拖拽;进行拖拽的时候,一定要是player底下的粒子特效进行拖拽;

 1 public class playerController : MonoBehaviour
 2 {
 3     private Rigidbody rb;
 4     //设置动画的变量
 5     private Animator anim;
 6 
 7     public ParticleSystem dirtParticle, explotionParticle;
 8     public  float jumpForce;
 9     public float gravityModifier;
10     public bool isGround;//默认是false;
11     public bool isGameOver;
12     // Start is called before the first frame update
13     void Start()
14     {
15         rb = GetComponent<Rigidbody>();
16         //给动画变量赋值
17         anim = GetComponent<Animator>();
18         //增加物体本身的重力,加快回落的速度
19         Physics.gravity *= gravityModifier;
20     }
21 
22     // Update is called once per frame
23     void Update()
24     {
25         if (Input.GetKeyDown(KeyCode.Space)&&isGround) {
26             rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
27             isGround = false;
28             //转换跑和跳,jump_trig被触发
29             anim.SetTrigger("Jump_trig");
30             //粒子特效停止
31             dirtParticle.Stop();
32 
33         }
34        
35     }
36     //刚刚触碰物体时
37     private void OnCollisionEnter(Collision other)//除本身之外碰撞的物体
38     {
39         //通过地面标签获取
40         if (other.gameObject.CompareTag("Ground"))
41         {
42             isGround = true;
43             //粒子特效播放
44             dirtParticle.Play();
45         }
46         //通过障碍物的标签获取,触碰到障碍物的时候,游戏结束,在console中显示Game Over
47         if (other.gameObject.CompareTag("Obstacle")) {
48             isGameOver = true;
49             Debug.Log("Game Over!!");
50             //人物死亡时的动画效果
51             anim.SetBool("Death_b",true);
52             anim.SetInteger("DeathType_int",1);
53             //粒子特效播放
54             explotionParticle.Play();
 dirtParticle.Stop();
55 } 56 } 57 }

 接下来添加声音

在Main Camera中的Audio Lisener,相当于人的耳朵,添加一个就可以了,音乐包括背景音乐都称之为音乐片段;在Main Camera中添加音源Audio Source,将背景音乐放进去;

接下来为人物撞击障碍物和跳跃添加声音,所以在player下面添加音源Audio Source;

可以在一个物体下,添加多个音源;

我们现在用一个音源,多个声音,用代码进行控制,声音何时进行播放;

 1 public class playerController : MonoBehaviour
 2 {
 3     private Rigidbody rb;
 4     //设置动画的变量
 5     private Animator anim;
 6 
 7     //添加声音
 8     public AudioSource audioSource;
 9     public AudioClip crashClip, jumpClip;
10     public ParticleSystem dirtParticle, explotionParticle;
11     public  float jumpForce;
12     public float gravityModifier;
13     public bool isGround;//默认是false;
14     public bool isGameOver;
15     // Start is called before the first frame update
16     void Start()
17     {
18         rb = GetComponent<Rigidbody>();
19         //给动画变量赋值
20         anim = GetComponent<Animator>();
21         //增加物体本身的重力,加快回落的速度
22         Physics.gravity *= gravityModifier;
23         //获取声音组件
24         audioSource = GetComponent<AudioSource>();
25 
26     }
27 
28     // Update is called once per frame
29     void Update()
30     {
31         if (Input.GetKeyDown(KeyCode.Space)&&isGround) {
32             rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
33             isGround = false;
34             //转换跑和跳,jump_trig被触发
35             anim.SetTrigger("Jump_trig");
36             //粒子特效停止
37             dirtParticle.Stop();
38             audioSource.PlayOneShot(jumpClip,1.0f);
39 
40         }
41        
42     }
43     //刚刚触碰物体时
44     private void OnCollisionEnter(Collision other)//除本身之外碰撞的物体
45     {
46         //通过地面标签获取
47         if (other.gameObject.CompareTag("Ground"))
48         {
49             isGround = true;
50             //粒子特效播放
51             dirtParticle.Play();
52         }
53         //通过障碍物的标签获取,触碰到障碍物的时候,游戏结束,在console中显示Game Over
54         if (other.gameObject.CompareTag("Obstacle")) {
55             isGameOver = true;
56             Debug.Log("Game Over!!");
57             //人物死亡时的动画效果
58             anim.SetBool("Death_b",true);
59             anim.SetInteger("DeathType_int",1);
60             //粒子特效播放
61             explotionParticle.Play();
62             dirtParticle.Stop();
63             audioSource.PlayOneShot(crashClip,1.0f);//播放的声音片段和音量
64         }
65     }
66 }

 

posted @ 2021-10-15 15:42  无敌小金刚  阅读(495)  评论(0)    收藏  举报