【问题标题】:Unity: Animation not working (c#)Unity:动画不起作用(c#)
【发布时间】:2015-11-28 21:28:52
【问题描述】:

我正在关注YouTube 上有关使用混合树的播放器动画的视频教程。我按照那个人的教程去喝茶,我在控制台中没有收到任何错误,唯一的问题是,当我玩游戏时,无论我点击多少次,我的播放器都没有播放动画。我为我的玩家创建了一个点击移动脚本(c#)来移动,并且(如上所述)完全按照这个人的教程进行操作。我检查了动画窗口(在玩游戏时),看到我的播放器空闲状态仍在播放,而不是在播放行走状态,无论我移动到哪里。我想可能是因为:

 void Update () 
 {
   if (Input.GetMouseButtonDown(0)) 
   {
     target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     target.z = transform.position.z;
   }
   transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
 }

但我不是天才!我想要的是让我的播放器完全按照视频教程所做的,但使用 (Input.GetMouseButtonDown(0))。谁能帮我解决我的问题!谢谢,这是我的完整代码:

  using UnityEngine;
  using System.Collections;
  public class move : MonoBehaviour 
  {
    private Animator anim;
    public float speed = 15f;
    private Vector3 target;

    void Start () 
    {
       target = transform.position;
       anim = GetComponent<Animator> ();
    }

    void Update () 
    {
      if (Input.GetMouseButtonDown(0)) 
      {
        target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        target.z = transform.position.z;
      }
      transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
      float inputX = Input.GetAxis ("Horizontal");
      float inputY = Input.GetAxis ("Vertical");
      anim.SetFloat ("SpeedX", inputX);
      anim.SetFloat ("SpeedY", inputY);
    }
    void FixedUpdate () 
    {
      float LastInputX = Input.GetAxis ("Horizontal");
      float LastInputY = Input.GetAxis ("Vertical");
      if (LastInputX != 0 || LastInputY != 0) {
        anim.SetBool ("walking", true);
        if (LastInputX > 0) {
           anim.SetFloat ("LastMoveX", 1f);
        } else if (LastInputX < 0) {
           anim.SetFloat ("LastMoveX", -1f);
       } else {
           anim.SetBool ("walking", false);
       }
        if (LastInputY > 0) {
          anim.SetFloat ("LastMoveY", 1f);
       } else if (LastInputY < 0) {
           anim.SetFloat ("LastMoveY", -1f);
      } else {
           anim.SetFloat ("LastMoveY", 0f);
       } 
    } else {
       anim.SetBool ("walking", false);
   }
 }  

【问题讨论】:

    标签: c# animation unity3d


    【解决方案1】:

    据我所知,您没有将 animator 输入变量设置为任何内容,因此您需要在 if 语句中放入 animator 变量编辑器。

    if (Input.GetMouseButtonDown(0)) 
      {
        target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        target.z = transform.position.z;
      //right here
      }
    

    【讨论】:

      【解决方案2】:

      您仍在使用 Input.GetAxis 来确定您是否在移动,但这是行不通的。您需要做的是向目标移动,并根据您的移动设置动画浮动值和步行布尔值。当你到达目标时,你应该将walking设置为false。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-16
        • 1970-01-01
        • 2011-03-27
        • 2022-01-15
        • 1970-01-01
        • 2015-11-03
        • 1970-01-01
        • 2015-09-23
        相关资源
        最近更新 更多