- Home /
How to make enemy to be pushed out when collision with weapon?
I wrote a script that make enemy to be pushed out when collision with weapon but it might not working well. This is my idea:
I want when enemy touch the blade, it will move slowly from B to C but my script make it move from B to C immediately.
Here is my code
public class bat_controller : MonoBehaviour { public Transform hero; public float distance;
private void Awake()
{
hero = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update () {
direction = (hero.position - transform.position).normalized;
private void OnTriggerEnter2D(Collider2D col)
{
if (col.CompareTag("blade"))
{
if (!ishurt)
{
Vector3 startpos = transform.position;
Vector3 endpos = startpos + (-movedir * distance);
transform.position = Vector3.Lerp(startpos, endpos, 3*Time.deltaTime);
}
}
}
}
and sorry for my English :
Answer by fredkoto · Dec 25, 2017 at 11:26 AM
You can try and AddForce to the enemy's rigidbody on detected collision
You can add bounciness to the blade and the enemy and make it used only between them.
Why are you multiplying your Time.deltaTime by 3? This will make it go crazy fast (I think). Maybe try using MoveTowards instead of Lerp.
Your answer
Follow this Question
Related Questions
how to make enemy shoot and follow player in fps 1 Answer
MouseWheel Lerp Smoothing Problem 1 Answer
How do I scale a 2D sprite over time? 2 Answers
Re-joining relative orbital position after being removed 1 Answer
Linear interpolating (Lerping) a Gameobject between random positions of Other GameObjects 1 Answer