先週はじめたばかりの独学初心者です。
Terrainから歩いて落下する際のアニメーションをJumpingにしたいのですが、Walkのまま落下してしまいます。
TerrainのTag:Groundに接していない時Jumpingアニメーションに移行するというようなイメージで表現したくて試行錯誤していますがうまくいきません
どこを抜粋すればよいのかもわからないのでそのままScriptを記述しました。
よろしければアドバイスをお願いします。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MoveAction : MonoBehaviour
{
private Animator animator;
private AudioSource audioSource;
public AudioClip[] Idlesound;
public AudioClip[] Walksound;
public AudioClip[] Jumpsound;
public bool randomizePitch = true;
public float pitchRange = 0.1f;
private Rigidbody rb;
public float upForce = 200f;
private bool isGround;
void Start()
{
animator = GetComponent<Animator>();
audioSource = GetComponent<AudioSource>();
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (Input.GetKey("up"))
{
transform.position += transform.forward * 0.05f;
animator.SetBool("Walk", true);
}
else
{
animator.SetBool("Walk", false);
}
if (Input.GetKey("right"))
{
transform.Rotate(0, 2, 0);
}
if (Input.GetKey("left"))
{
transform.Rotate(0, -2, 0);
}
if (Input.GetKey("down"))
{
transform.position += transform.forward * -0.05f;
animator.SetBool("Back", true);
}
else
{
animator.SetBool("Back", false);
}
if (isGround == true)
{
if (Input.GetKeyDown("space"))
{
audioSource.PlayOneShot(Jumpsound[Random.Range(0, Jumpsound.Length)]);
animator.SetBool("Jumping", true);
isGround = false;
rb.AddForce(new Vector3(0, upForce, 0));
}
}
if (isGround == false)
{
animator.SetBool("Jumping", true);
}
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Ground")
{
isGround = true;
animator.SetBool("Jumping", false);
audioSource.PlayOneShot(Jumpsound[Random.Range(0, Jumpsound.Length)]);
}
else
{
isGround = false;
}
if (other.gameObject.tag == "died")
{
{
int sceneIndex = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(sceneIndex);
}
}
}
public void PlayWalkingMusic()
{
if (randomizePitch)
audioSource.pitch = 1.0f + Random.Range(-pitchRange, pitchRange);
audioSource.PlayOneShot(Walksound[Random.Range(0, Walksound.Length)]);
}
public void PlayIdleMusic()
{
if (randomizePitch)
audioSource.pitch = 1.0f + Random.Range(-pitchRange, pitchRange);
audioSource.PlayOneShot(Idlesound[Random.Range(0, Idlesound.Length)]);
}
}