- Home /
Create an animation with variables?
I'm trying to make a little platform game, and I've created an enemy that when you enter your range of vision, chase you and jump on top of you to crush you. The part of "Jump and fall on you" I decided to do it by means of an animation, but my problem is that when changing position, the animation is executed in the initial position of the enemy, not in the current position of the enemy, I do not know if I explain. I have read that it can be done using animation curves, but I do not understand how it works ... Here is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Enemy2 : MonoBehaviour{
public bool followPlayer = false;
public float speedToFollow = 2f;
private Transform target;
private GameObject player;
public float distancia = 1f;
public SpriteRenderer emote;
private Animator anim;
private SpriteRenderer mySprite;
public float maxSpeed = 1f;
public float speed = 1f;
public GameObject playerCollider;
private Rigidbody2D rb2d;
public float visionRadius = 2f;
public float attackRadius;
void Start(){
target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
player = GameObject.FindGameObjectWithTag("Player");
rb2d = GetComponent<Rigidbody2D>();
emote.enabled = false;
anim = GetComponent<Animator>();
mySprite = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update(){
if (target == null)
return;
Debug.Log(rb2d.velocity.x);
//Debug.Log(Vector2.Distance(transform.position, target.position));
if (followPlayer == true && Vector2.Distance(transform.position, target.position) > distancia){
transform.position = Vector2.MoveTowards(transform.position, target.position, speedToFollow * Time.deltaTime);
//transform.position += transform.forward * speedToFollow * Time.deltaTime;
}
if (followPlayer == true && Vector2.Distance(transform.position, target.position) < distancia) {
//this is where the enemy would perform the animation of jumping on the player
}
}
void FixedUpdate(){
if (followPlayer == false) {
rb2d.AddForce(Vector2.right * speed);
float limitedSpeed = Mathf.Clamp(rb2d.velocity.x, -maxSpeed, maxSpeed);
rb2d.velocity = new Vector2(limitedSpeed, rb2d.velocity.y);
if (rb2d.velocity.x > -0.01f && rb2d.velocity.x < 0.01f){
speed = -speed;
rb2d.velocity = new Vector2(speed, rb2d.velocity.y);
}
if (speed < 0){
transform.localScale = new Vector3(1f, 1f, 1f);
}
else if (speed > 0){
transform.localScale = new Vector3(-1f, 1f, 1f);
}
//Debug.Log (rb2d.velocity.x);
}
}
private IEnumerator OnTriggerEnter2D(Collider2D collision){
if (collision.gameObject.tag == "Player") {
followPlayer = true;
anim.GetComponent<Animator>().SetBool("Attack", true);
yield return new WaitForSeconds(0.5f);
anim.GetComponent<Animator>().SetBool("Attack", false);
}
if (collision.gameObject.tag == "Vuelta"){
speed = -speed;
rb2d.velocity = new Vector2(speed, rb2d.velocity.y);
}
}
private void OnTriggerStay2D(Collider2D collision){
if (collision.gameObject.tag == "Player"){
followPlayer = true;
}
}
private void OnTriggerExit2D(Collider2D collision){
followPlayer = false;
}
private void OnDrawGizmos(){
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, visionRadius);
Gizmos.DrawWireSphere(transform.position, attackRadius);
}
private IEnumerator Wait(float seconds) {
yield return new WaitForSeconds(seconds);
}
}
Is there any way to solve this? Any help is very much appreciated.
Answer by yisusgamer55 · May 11, 2019 at 07:54 PM
Hello! In the end I found the solution. All he had to do is create a father object with the colliders of the enemy and with his scrit, leaving the animator in the main object of the enemy. By doing this, the enemy's transform varies with respect to the father's position.
I found the solution in this thread: https://answers.unity.com/questions/51178/animation-position-problem.html