码迷,mamicode.com
首页 > 其他好文 > 详细

Animation.CrossFade Animation.Play

时间:2017-08-27 11:11:52      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:tran   anim   float   unity   force   ati   date()   span   public   

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class PlayerControll : MonoBehaviour
 6 {
 7     Transform playerTransform;
 8     Animation playerAnimation;
 9     Rigidbody playerRigidbody;
10     public float moveSpeed;
11     public float jumpAbility;
12     bool canJump;
13 
14     void Start()
15     {
16         playerTransform = GetComponent<Transform>();
17         playerAnimation = GetComponent<Animation>();
18         playerRigidbody = GetComponent<Rigidbody>();
19         Debug.Log(playerTransform.name);
20     }
21 
22     void Update()
23     {
24         Move_Control();
25     }
26 
27     void Move_Control()
28     {
29         if (Input.GetKey(KeyCode.W))
30         {
31             playerTransform.Translate(Vector3.forward * Time.deltaTime * moveSpeed, Space.Self);
32             playerAnimation.Play("runforward");
33         }
34 
35         if (Input.GetKeyUp(KeyCode.W))
36         {
37             playerAnimation.CrossFade("idle", 0.3f);
38         }
39 
40         if (Input.GetKey(KeyCode.S))
41         {
42             playerTransform.Translate(Vector3.back * Time.deltaTime * moveSpeed, Space.Self);
43             playerAnimation.Play("runbackwards");
44         }
45         if (Input.GetKeyUp(KeyCode.S))
46         {
47             playerAnimation.CrossFade("idle", 0.3f);
48         }
49 
50         if (Input.GetKey(KeyCode.A))
51         {
52             playerTransform.Translate(Vector3.left * Time.deltaTime * moveSpeed, Space.Self);
53             playerAnimation.Play("strafeleft");
54         }
55 
56         if (Input.GetKeyUp(KeyCode.A))
57         {
58             playerAnimation.CrossFade("idle", 0.3f);
59         }
60 
61         if (Input.GetKey(KeyCode.D))
62         {
63             playerTransform.Translate(Vector3.right * Time.deltaTime * moveSpeed, Space.Self);
64             playerAnimation.Play("straferight");
65         }
66 
67         if (Input.GetKeyUp(KeyCode.D))
68         {
69             playerAnimation.CrossFade("idle", 0.3f);
70         }
71 
72         if (Input.GetKey(KeyCode.Space))
73         {
74             if (canJump)
75             {
76                 playerRigidbody.AddForce(Vector3.up * jumpAbility, ForceMode.Impulse);
77                 playerAnimation.Play("jump");
78                 canJump = false;
79             }
80         }
81     }
82 
83     private void OnCollisionStay(Collision collision)
84     {
85         if (collision.transform.tag == "Ground")
86         {
87             canJump = true;
88             //playerAnimation.Play("idle");
89             playerAnimation.CrossFade("idle",0.2f);
90         }
91     }
92 }

按88行那样写只摁WSAD中的一个按键人物正常移动,但是不正常执行动画,人物会漂移;按89行那样写只摁WSAD中的一个按键人物正常移动,并且正常执行动画。

但是同时摁下WD或WA或SD或SA,人物都会漂移。

Animation.CrossFade Animation.Play

标签:tran   anim   float   unity   force   ati   date()   span   public   

原文地址:http://www.cnblogs.com/Peng18233754457/p/7439714.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!