Have enemy constantly try to get in front of player and push them back
Hi, I have an enemy that is supposed to constantly try to get in front of the player and push them backwards... right now I have this as the code -
public class PeasantController : MonoBehaviour {
Transform player;
public float moveSpeed;
public float rotationSpeed;
public float minDist;
public float force;
Vector3 dir;
Vector3 oldPos;
Vector3 oldRot;
public bool playerCollide;
void Start () {
player = GameObject.FindWithTag ("Player").transform;
oldPos = transform.position;
oldRot = transform.rotation.eulerAngles;
playerCollide = false;
}
void Update () {
bool held = Input.GetKey(KeyCode.Space);
if (!playerCollide && !held) {
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation
(player.position - transform.position),
rotationSpeed * Time.deltaTime);
transform.LookAt (player.transform);
if (Vector3.Distance (transform.position, player.transform.position) <= minDist) {
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
}
if (playerCollide) {
if (held) {
GetComponent<Rigidbody>().AddForce(dir*force);
}
}
playerCollide = false;
}
void OnCollisionEnter(Collision col) {
if (col.collider.tag == "Player") {
dir = col.contacts [0].point - transform.position;
dir = -dir.normalized;
playerCollide = true;
}
}
}
and it works at first because the enemies are placed in front of the player and if you do nothing they constantly push him back right and sometimes you can knock them flying way back by pressing space bar with alot of force added...
however if you get past them they just constantly come from behind you and push you forward and the space button doesnt work to push them anymore. basically I want to check if the enemy is in front of the player, and if so do what the code up there does correctly constantly push them back, but if it gets to the side of the player or behind I want to increase the movement speed and have them speed up and chase the player and try to get in front of them again to push them back from the front again....
I tried using this code to check if the enemy was in front of the player however when I use this code my game just freezes up when I try to test it and doesnt work anymore...
using UnityEngine;
using System.Collections;
public class PeasantController : MonoBehaviour
{
Transform player;
public float moveSpeed;
public float rotationSpeed;
public float minDist;
public float force;
Vector3 dir;
Vector3 oldPos;
Vector3 oldRot;
public bool playerCollide;
bool held;
bool inFront;
void Start()
{
player = GameObject.FindWithTag("Player").transform;
oldPos = transform.position;
oldRot = transform.rotation.eulerAngles;
playerCollide = false;
held = false;
inFront = false;
}
void Update()
{
held = Input.GetKey(KeyCode.Space);
Vector3 heading = transform.position - player.position;
float dot = Vector3.Dot(heading, player.transform.forward);
if (dot != 1)
{
inFront = false;
}
if (dot == 1)
{
inFront = true;
}
if (inFront && !held)
{
moveSpeed = 10.0F;
transform.rotation = Quaternion.Slerp(transform.rotation,
Quaternion.LookRotation(player.position - transform.position),
rotationSpeed * Time.deltaTime);
transform.LookAt(player.transform);
if (Vector3.Distance(transform.position, player.transform.position) <= minDist)
{
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
}
else if (!inFront && !held)
{
while (!inFront)
{
moveSpeed = 25.0F;
transform.rotation = Quaternion.Slerp(transform.rotation,
Quaternion.Inverse(player.transform.rotation),
rotationSpeed * Time.deltaTime);
transform.LookAt(player.transform);
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
}
else
{
Debug.Log("target should be knocked away");
}
}
void OnCollisionEnter(Collision col)
{
if (col.collider.tag == "Player")
{
dir = col.contacts[0].point - transform.position;
dir = -dir.normalized;
if (held)
{
GetComponent<Rigidbody>().AddForce(dir * force);
}
held = false;
}
}
}
i also tried using code somehwat like this instead of the dot product but that made my game freeze up when pressing play as well..
Vector3 directionToTarget = transform.position - player.position;
float angle = Vector3.Angle(transform.forward, directionToTarget);
float distance = directionToTarget.magnitude;
if (Mathf.Abs(angle) != 90 && distance < 10) {
inFront = false;
}
if (Mathf.Abs(angle) == 90)
{
inFront = true;
}
any help is greatly appreciated