Enemy direction going crazy after collision
Well, this is my first time here friends! I wrote a simple code for moving an enemy. But when he collides with anything he seems to lose direction (following the player).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mouthless_Behaviour : MonoBehaviour
{
public Transform target;
public float rotationSpeed = 0.1f;
public float movementSpeed = 1.0f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (target != null)
{
transform.position += transform.forward * Time.deltaTime * movementSpeed;
var rotation = Quaternion.LookRotation (target.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation,
rotation, Time.deltaTime * rotationSpeed);
}
}
}
If he collides with anything he change his direction and stop following the player. Here a GIF from this issue: Here!
Does your enemy GO have a Rigidbody component on it? Because this is pretty weird since the direction should change as long as you don't use rigid bodies.
tested his code and the only thing i can think about is his rigidbody is getting insane velocity, do you need the enemy to have the rigidbody?, can you test and make the bullet trigger so is not getting pushed? or maybe oncollisionenter change the bodys velocity to 0 so is not getting pushed away, not sure whats the best aproach for your game, abut really cool shader :)
Thank you friend!! :DD not tested your way but I found the solution, exactly at the rigidbody, just need to stop rigidbody rotation since my enemy rotation is by code. It's working fine now. Thank you for the help btw :DD
Yes he have a Rigidbody and I just find the solution :DD Just need to freeze the rigidbody rotation and its fine now :D btw thanks for the help!
Answer by smile044 · Jan 21, 2019 at 08:26 PM
I found the solution, just stopped the Rigidbody rotation since my enemy rotation is by code :D something like this:
rigidbody.angularVelocity = Vector3.zero;
You can simply freeze Rigidbody rotation on the component in the inspector