Question by
CoolCosmos · Oct 04, 2019 at 12:03 AM ·
collisionvelocityreflectionwalleuler angles
How to reflect object's direction when collides wall ?
Hi, I move enemy by using rigidbody.velocity and pick a random euler angle and instantiate it on floor.
i stuck on reflecting enemy when it collides objects tagged "Wall". I want enemy to reflect with random euler angle when it collides wall.
I write this code so far and enemy reflects but it sticks with wall. So can you help me to make it work properly. Thanks.
void Awake()
{
rb_Enemy = GetComponent<Rigidbody>();
PickDirection();
}
void FixedUpdate()
{
EnemyMovement();
}
public void PickDirection()
{
axes = new float[8]{12, 24, 42, 72, 192, 204, 222, 252};
int indexAxe = Random.Range(0, axes.Length);
randomAxe = axes[indexAxe];
}
public void EnemyMovement()
{
Vector3 enemyDirection = Quaternion.AngleAxis(randomAxe, Vector3.up) * transform.forward;
rb_Enemy.velocity = enemyDirection * eThrust * Time.deltaTime;
}
public void ReflectEnemy()
{
enemy.transform.Rotate(0, randomAxe +90, 0);
}
public void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Wall")
{
ReflectEnemy();
}
}
Comment