Question by
nymr12 · Nov 12, 2018 at 03:04 PM ·
animationunity 5top down shooter
Can't get animator to activate parameter more than once (Unity2D).
I'm having a problem with the animator in Unity 2018. On my script I try to call the same parameters more than once for a walking/sprinting animation (top down). I have attached the script below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Controller : MonoBehaviour {
//Rigidbody2D body;
//float horizontal;
//float vertical;
//float moveLimiter = 0.7f;
public float walkSpeed = 1.5f;
public float runSpeed = 3.0f;
//public Transform target;
private Vector3 targetPosition;
private Vector2 mousePos;
private Vector2 objectPos;
private float playerRotationAngle;
public Transform targetPlayer;
Animator anim;
float angle;
//Vector3 myRot;
Transform myTrans;
Vector3 myPos;
public float rotationSpeed;
void Start ()
{
//body = GetComponent<Rigidbody2D>();
myTrans = transform;
myPos = myTrans.position;
//myRot = myTrans.rotation.eulerAngles;
anim = GetComponent<Animator>();
}
void FixedUpdate ()
{
Debug.DrawRay (transform.position, transform.right * 5, Color.red); //X
Debug.DrawRay (transform.position, transform.forward * 5, Color.blue); //Z
mousePos = Input.mousePosition;
objectPos = Camera.main.WorldToScreenPoint(targetPlayer.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
playerRotationAngle = Mathf.Atan2 (mousePos.y,mousePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler (new Vector3(0,0,playerRotationAngle));
angle = myTrans.eulerAngles.magnitude * Mathf.Deg2Rad;
//move object Forward & Backward
/*if (Input.GetButton ("Forward")) {
anim.SetBool("walk",true);
anim.SetBool("run", false);
anim.SetFloat("speed", 0.5f);
myPos.x += (Mathf.Cos (angle) * walkSpeed) * Time.deltaTime;
myPos.y += (Mathf.Sin (angle) * walkSpeed) * Time.deltaTime;
}
else if (Input.GetButtonUp ("Forward"))
{
anim.SetBool("walk", false);
anim.SetBool("run", false);
anim.SetFloat("speed", 0.0f);
}
if (Input.GetButton ("Shift")) {
anim.SetBool("walk",false);
anim.SetBool("run", true);
anim.SetFloat("speed", 1.0f);
myPos.x += (Mathf.Cos (angle) * runSpeed) * Time.deltaTime;
myPos.y += (Mathf.Sin (angle) * runSpeed) * Time.deltaTime;
}
else if (Input.GetButtonUp ("Shift"))
{
anim.SetBool("walk", false);
anim.SetBool("run", false);
anim.SetFloat("speed", 0.0f);
}
if (Input.GetButton ("Backward")) {
anim.SetBool("walk",true);
anim.SetBool("run", false);
anim.SetFloat("speed", 0.5f);
myPos.x -= (Mathf.Cos (angle) * walkSpeed) * Time.deltaTime;
myPos.y -= (Mathf.Sin (angle) * walkSpeed) * Time.deltaTime;
}
else if (Input.GetButtonUp ("Backward"))
{
anim.SetBool("walk", false);
anim.SetBool("run", false);
anim.SetFloat("speed", 0.0f);
}*/
if (Input.GetKey (KeyCode.W) || Input.GetKey (KeyCode.UpArrow))
{
/*anim.SetBool("walk", true);
anim.SetBool("run", false);
anim.SetFloat("speed", 0.5f);*/
Walk ();
myPos.x += (Mathf.Cos (angle) * walkSpeed) * Time.deltaTime;
myPos.y += (Mathf.Sin (angle) * walkSpeed) * Time.deltaTime;
}
else
{
anim.SetBool("walk", false);
anim.SetBool("run", false);
anim.SetFloat("speed", 0.0f);
//bool foo = anim.GetBool("walk");
//Debug.Log(foo + " for bool walk.");
}
if (Input.GetKey (KeyCode.W) && Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.UpArrow) && Input.GetKey (KeyCode.RightShift))
{
anim.SetBool("walk", false);
anim.SetBool("run", true);
anim.SetFloat("speed", 1.0f);
myPos.x += (Mathf.Cos (angle) * runSpeed) * Time.deltaTime;
myPos.y += (Mathf.Sin (angle) * runSpeed) * Time.deltaTime;
}
else
{
anim.SetBool("walk", false);
anim.SetBool("run", false);
anim.SetFloat("speed", 0.0f);
}
if (Input.GetKey (KeyCode.S) || Input.GetKey (KeyCode.DownArrow))
{
/*anim.SetBool("walk", true);
anim.SetBool("run", false);
anim.SetFloat("speed", 0.5f);*/
Walk ();
myPos.x -= (Mathf.Cos (angle) * walkSpeed) * Time.deltaTime;
myPos.y -= (Mathf.Sin (angle) * walkSpeed) * Time.deltaTime;
}
else
{
anim.SetBool("walk", false);
anim.SetBool("run", false);
anim.SetFloat("speed", 0.0f);
}
myTrans.position = myPos;
//myTrans.rotation = Quaternion.Euler (myRot);
}
void Walk ()
{
anim.SetBool("walk", true);
//anim.SetTrigger("Walk");
anim.SetBool("run", false);
anim.SetFloat("speed", 0.5f);
//bool foo = anim.GetBool("walk");
//Debug.Log(foo + " for bool walk.");
}
void OnCollisionEnter2D (Collision2D col)
{
if(col.gameObject.tag == "Walls")
{
//walkSpeed = 0.0f;
Debug.Log("Wall");
}
}
/*void OnCollisionExit2D (Collision2D col)
{
if(col.gameObject.tag == "Walls")
{
walkSpeed = 1.5f;
}
}*/
}
,
Comment