problems with following object while jumping animation,hello
hello i tring to make boss fight, and i wrote the behavior script to jump animation, that allow to the boss character move after the player, but the boss character doesnt jump on the player. He just stay on the same position.(i need to the boss character follow the player character while jumping)
jumpBihavior:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class JumpBehavior : StateMachineBehaviour { private float timer; public float minTime; public float maxTime;
private Transform playerPos;
public float speed;
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
playerPos = GameObject.FindGameObjectWithTag("PlayerCube").GetComponent<Transform>();
timer = Random.Range(minTime, maxTime);
}
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
if (timer <= 0)
{
animator.SetTrigger("idle");
}
else
{
timer -= Time.deltaTime;
}
Vector2 target = new Vector2(playerPos.position.x, animator.transform.position.y);
animator.transform.position = Vector2.MoveTowards(animator.transform.position, target, speed * Time.deltaTime);
}
}
boss script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class BossBattle : MonoBehaviour { public float speed; public int health; public int damage; private float timeBtwDamage = 1.5f;
public Slider healthBar;
private Animator anim;
public bool isDead;
private void Start()
{
anim = GetComponent<Animator>();
}
private void Update()
{
if (health <= 250)
{
anim.SetTrigger("StageTwo");
}
if (health <= 0)
{
anim.SetTrigger("Death");
}
// give the player some time to recover before taking more damage !
if (timeBtwDamage > 0)
{
timeBtwDamage -= Time.deltaTime;
}
healthBar.value = health;
}
private void OnTriggerEnter2D(Collider2D other)
{
// deal the player damage !
if (other.CompareTag("PlayerCube") && isDead == false)
{
if (timeBtwDamage <= 0)
{
other.GetComponent<PlayereControllerScript>().health -= damage;
}
}
}
},
Your answer
![](https://koobas.hobune.stream/wayback/20220612231950im_/https://answers.unity.com/themes/thub/images/avi.jpg)