- Home /
Question by
bryanlincoln · Jul 12, 2015 at 12:01 PM ·
animationjavascripttriggermecanimnot working
Why isn't my animation being played?
The animation works fine with my C# code, but it doesn't play when I try to trigger it from the JS code.
JS Code (where the animations doesn't play):
#pragma strict
var speed : float = 10.0;
internal var anim : Animator;
var rotation: GameObject;
var x : float;
var z : float;
var angle : float;
function Start() {
anim = GameObject.Find("Pernas").GetComponent(Animator) as Animator;
}
function Update () {
// Arrows or WASD
x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
// Animations
if (Mathf.Abs(x) > 0.1 || Mathf.Abs(z) > 0.1) anim.SetBool("walk", true);
else anim.SetBool("walk", false);
// Rotation based on mouse position
var playerPlane = new Plane(Vector3.up, transform.position);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hitdist = 0.0;
if (playerPlane.Raycast (ray, hitdist)) {
var targetPoint = ray.GetPoint(hitdist);
var targetRotation = Quaternion.LookRotation(targetPoint - gRotacao.transform.position);
rotation.transform.rotation = Quaternion.Slerp(rotation.transform.rotation, targetRotation, speed * Time.deltaTime);
}
// Apply the player's movement
transform.Translate(x, 0, z);
}
} // Update
C# Code (working fine):
using UnityEngine;
using System.Collections;
public class Joystick : MonoBehaviour
{
public GameObject gJoystick;
public CNAbstractController MovementJoystick;
public Animator anim;
public float speed;
public float x;
public float z;
private void Start() {
anim = GameObject.Find("Pernas").GetComponent<Animator>();
}
private void Update() {
// Receive the joystick's values
x = MovementJoystick.GetAxis("Horizontal") * Time.deltaTime * speed;
z = MovementJoystick.GetAxis("Vertical") * Time.deltaTime * speed;
// Animations
if (Mathf.Abs(x) > 0.1 || Mathf.Abs(z) > 0.1) anim.SetBool("walk", true);
else anim.SetBool("walk", false);
// Apply player's movement
transform.Translate(x, 0, z);
}
}
I've tried so many things that I'm starting to think it's an Unity bug.
Thanks for any help!
Comment
Because I use this C# script to handle the virtual joysticks axis values and the JS script to handle the default vertical and horizontal axis. The values are received properly, I don't think that's the problem. Thanks!
Your answer
Follow this Question
Related Questions
Can't find Mecanim? 2 Answers
Animator Trigger Not Working 1 Answer
Mecanim Triggers Not Deactivating 0 Answers
Trigger Animation Instantly? 2 Answers
How to properly place a rifle weapon into the Character's hands and change pose? 0 Answers