So I want the enemy to look at the player and then after a second charge towards the player, but in a straight line. If the player moves, I want the enemy to run right past him into the wall. Here’s what I have so far, but it’s pretty wrong for many reasons. Just looking for guidance on how to at least get this part done. Currently he doesn’t rotate right obviously because it’s only being called for 1 frame or so and then movetowards is in update so its just a constant follow.
using UnityEngine;
using System.Collections;
public class BossRam : MonoBehaviour {
GameObject player; //for finding the player
public Transform target;
Transform myTransform;
public float speed = 12f;
bool ramming = false; //decide whether or not he's looking and about to ram
bool charge = false;
void Awake()
{
myTransform = transform;
}
void Start()
{
player = GameObject.Find("Player");
target = player.transform;
}
void Update()
{
if (ramming == false)
{
Ram();
}
if (charge == true) //this code will always find him though. may need to do a vector3.position or something and then have him go past it?
{
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
}
void Ram()
{
ramming = true;
Vector3 vectorToTarget = target.position - transform.position;
float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * speed);
StartCoroutine(Ramming());
}
void OnCollisionEnter(Collision other)
{
if(other.gameObject.CompareTag("Wall"))
{
speed = 0;
StartCoroutine(RamCooldown());
}
}
IEnumerator Ramming()
{
yield return new WaitForSeconds(1);
//charge
charge = true;
}
IEnumerator RamCooldown()
{
charge = false;
yield return new WaitForSeconds(1);
//recover
ramming = false;
}
}