質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

588閲覧

Unity アニメーションの実行

0mizunomitai

総合スコア1

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/10/08 12:40

前提・実現したいこと

UnityでRunアニメーションを実行

発生している問題・エラーメッセージ

Runアニメーションが上下か左右しかできない。 GetComponent<Animator>().SetFloat("Speed",Mathf.Abs(add_pos.x)); GetComponent<Animator>().SetFloat("Speed",Mathf.Abs(add_pos.z)); これを一つにまとめて上下左右で移動アニメーションが実行されるようになりたいです。アニメーションのパラメーターはfloatで0.01で設定しています。

該当のソースコード

Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
Animator animator;
public float MoveSpeed = 5.0f;

// Start is called before the first frame update void Start() { animator = GetComponent<Animator>(); } // Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.Space)) { animator.SetTrigger("Jump"); } Movement(); } void Movement() { Vector3 add_pos = Vector3.zero; if (Input.GetKey(KeyCode.RightArrow)) { add_pos.x += 3.0f; } else if (Input.GetKey(KeyCode.LeftArrow)) { add_pos.x -= 3.0f; } if (Input.GetKey(KeyCode.UpArrow)) { add_pos.z += 3.0f; } else if (Input.GetKey(KeyCode.DownArrow)) { add_pos.z -= 3.0f; } GetComponent<Animator>().SetFloat("Speed",Mathf.Abs(add_pos.x)); add_pos *= Time.deltaTime; transform.localPosition += add_pos; }

}

### 試したこと GetComponent<Animator>().SetFloat("Speed",Mathf.Abs(add_pos.x)); GetComponent<Animator>().SetFloat("Speed",Mathf.Abs(add_pos.z)); 上から順に実行されるため、Z軸でしか移動できない。 GetComponent<Animator().SetFloat("Speed",Mathf.Abs(add_pos.x),Mathf.Abs(add_pos.z); 1つしか指定できない。 ### Unity 2019.4.11f1 Unity初心者でプログラミング自体にも慣れていないため解決する方法が見つかりません。足りない情報などあると思いますがよろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

if (Input.GetKey(KeyCode.W))
{
animator.SetBool("IsRun", true);
animator.SetBool("IsIdle", false);
}
else if (Input.GetKey(KeyCode.A))
{
animator.SetBool("IsRun", true);
animator.SetBool("IsIdle", false);
}
else if (Input.GetKey(KeyCode.D))
{
animator.SetBool("IsRun", true);
animator.SetBool("IsIdle", false);
}
else if (Input.GetKey(KeyCode.S))
{
animator.SetBool("IsRun", true);
animator.SetBool("IsIdle", false);
}
else
{
animator.SetBool("IsRun", false);
animator.SetBool("IsIdle", true);
}

Boolでアニメーションを制御することで少し不自然ですが、ちゃんと動くようになりました。

投稿2020/10/09 11:09

0mizunomitai

総合スコア1

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問