Hello everyone, I’m new to Unity, and this is my first forum post. Recently, I tried to learn a bit more about the animation system (Mecanim), so that I could add some life into a little game I’m doing. So far, I’ve managed to import animations from another software, and call on an running animation when the enemy is moving. However, I want the enemy to switch between animations at a given point(ex: When it gets close to the player, it pounces on him), but unfortunately, I haven’t got a clue as to how to do it properly…
I was thinking of creating an “Action_Field” (basically a box with a collider and a rigidbody) that would follow the player and change the enemy’s animation when it comes in contact with it. (OnCollisionEnter)
Unfortunately, that didn’t work, and I’ve been busy trying to find a solution these past few days, but I’m afraid I might not be able to find it on my own… I’d be grateful if someone could help me figure this out, or even simply give me a nudge in the right direction.
Here’s the code in its written form, along with a few helpful snapshots:
Action Field Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ActionField_Script : MonoBehaviour {
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
void OnCollisionEnter (Collision hit)
{
if (hit.gameObject.tag == "Enemy")
{
var enemy = hit.collider.GetComponent <EnemyAI> ();
enemy.animSwitch = true;
if (enemy.animSwitch)
{
enemy.animate.SetBool ("run", false);
enemy.animate.SetBool ("leap", true);
}
}
}
}
Enemy_AI
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAI : MonoBehaviour {
Transform target;
//private Vector3 originalPosition;
//private Vector3 currentPosition;
//private Vector3 enemyPosition;
public Animator animate;
private GameObject enemy;
public bool animSwitch;
Transform enemyTransform;
public float speed;
public float rotationSpeed;
public float detectionRange;
// Use this for initialization
void Start ()
{
animSwitch = false;
enemyTransform = this.GetComponent <Transform> ();
animate = GetComponent<Animator> ();
enemy = GameObject.FindWithTag ("Enemy");
//originalPosition = enemy.transform.position;
//currentPosition = originalPosition * Time.deltaTime;
//enemyPosition = originalPosition;
}
// Update is called once per frame
void Update ()
{
if (GameObject.FindWithTag ("Player") != null)
{
target = GameObject.FindWithTag ("Player").transform;
Vector3 targetHeading = target.position - transform.position;
Vector3 targetDirection = targetHeading.normalized;
transform.rotation = Quaternion.LookRotation (targetDirection);
transform.eulerAngles = new Vector3 (0, transform.eulerAngles.y, 0);
enemyTransform.position += enemyTransform.forward * speed * Time.deltaTime;
}
else if (GameObject.FindWithTag ("Player") == null)
{
target = GameObject.FindWithTag ("Character").transform;
Vector3 newTargetHeading = target.position - transform.position;
Vector3 newTargetDirection = newTargetHeading.normalized;
transform.rotation = Quaternion.LookRotation (newTargetDirection);
transform.eulerAngles = new Vector3 (0, transform.eulerAngles.y, 0);
enemyTransform.position += enemyTransform.forward * speed * Time.deltaTime;
animate.SetBool ("run", true);
}
}
}