- Home /
The enemy movement script I'm modeling after my player's isn't working.
I don't have the traditional movement script for my player. The way it works in a nutshell is I click on a wall in a 360 radius around the player and they lerp to that position with a W.I.P collision system I implemented(Uses raycast to stop lerping if they "collide" with the wall) I thought it would be simple to remove my enemy's movement script and edit my player's movement script a little bit to have it work for them but it apparently isn't that simple and it won't work in alot of ways.
Problems/insights with the enemy's lerping script
The timer to dash at the player won't reset even if I move the player towards the enemy
The timer in the movement courintine won't move form zero
The enemy won't dash at the player
The enemy is supposed to have it's rotation fixed on the player but when it dashes it locks to lerp towards the position the player was at when the timer to charge up a attack reached 0 but only seems to lock on(I think it's in a perpetual state of trying to lerp towards the player?) UPDATE(I put some Debug logs in my code and I noticed CurrentPointToLerpFromPosition is 0, 0, 0 on my enemy script at all times but on my player script it gets a value after I click on a wall?)
Enemy script
using UnityEngine;
using System.Collections;
public class BasicEnemyAi : MonoBehaviour {
public GameObject Enemy;
public GameObject Player;
public float ChargeUpTimer = 2;
private bool LerpToPosition;
private bool OneTimeStuff;
private Vector3 ThePointToLerpTo;
private static GameOverConditions GameOver;
public float speed;
private float TimerStart;
private GameObject dagger;
public Animator anim;
public Vector3 PointToLerpFrom;
private Vector3 RayPointPreviousPlace;
public Vector3 CurrentPointToLerpFromPosition;
private Vector3 DoesThisPoint;
Vector3 newPosition;
public float timer;
public float DistanceInRay;
void Start ()
{
newPosition = transform.position;
TimerStart = ChargeUpTimer;
Player = GameObject.FindGameObjectWithTag("Player");
}
// Update is called once per frame
void Update ()
{
Debug.Log(Vector3.Distance(newPosition, Player.transform.position));
Vector3 rayDir = Player.transform.position - transform.position;
Ray ray = new Ray(transform.position, rayDir);
Debug.DrawRay(transform.position, rayDir);
PointToLerpFrom = ray.origin + (ray.direction * DistanceInRay);
if (Vector3.Distance(Player.transform.position, Enemy.transform.position) <= 8)
{
anim.SetTrigger("InProximity");
}
if (LerpToPosition == false)
{
if (Player != null)
Enemy.transform.LookAt(Player.transform);
// Enemy.transform.eulerAngles = Enemy.transform.eulerAngles + new Vector3(180, 180, );
ChargeUpTimer -= Time.deltaTime;
}
if (ChargeUpTimer <= 0)
{
LerpToPosition = true;
if (LerpToPosition == true)
{
if (Player != null)
{
if (OneTimeStuff == true)
{
newPosition = Player.transform.position;
OneTimeStuff = false;
}
TimerCheck();
}
if(Vector3.Distance(newPosition, Player.transform.position) <= 3)
{
LerpToPosition = false;
OneTimeStuff = true;
ChargeUpTimer = TimerStart;
}
}
else
{
LerpToPosition = false;
}
}
}
private void TimerCheck()
{
if(ChargeUpTimer <= 0)
{
StartCoroutine("LerpPos");
}
}
private IEnumerator LerpPos()
{
if (DoesThisPoint != Player.transform.position)
{
timer = 0;
}
//DoesThisPoint = NewPosition;
while (Vector3.Distance(CurrentPointToLerpFromPosition, newPosition) > 3)
{
Debug.Log("My gawd");
timer += Time.deltaTime;
RayPointPreviousPlace = CurrentPointToLerpFromPosition;
CurrentPointToLerpFromPosition = Vector3.Lerp(PointToLerpFrom, newPosition, timer * speed);
transform.position = RayPointPreviousPlace;
yield return null;
}
}
}
Player script
using UnityEngine;
using System.Collections;
public class Dash : MonoBehaviour
{
public GameObject Previouscollided; // the point last lerped from
private GameObject collided; //A check to see if the camera clicked on the wall
public LayerMask Wall; //Self documenting but its basicly the wall that you click on
Vector3 newPosition; //The Position the player assumes once lerping is done
public GameObject RecentWall; //The wall that was clicked on before Current Wall
public float speed;//How fast lerping happens
[SerializeField]
public int CurrentWall; //The last wall that was clicked on
public float DistanceInRay; //The length to in the camera's array to set PointToLerpFrom
private Vector3 PointToLerpFrom; //The point after all calculations that the object lerps from
private Vector3 RayPointPreviousPlace;
private Vector3 CurrentPointToLerpFromPosition;
private Vector3 DoesThisPoint;
public float timer;
void Start()
{
newPosition = transform.position;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, Wall.value))
{
PointToLerpFrom = ray.origin + (ray.direction * DistanceInRay);
collided = hit.collider.gameObject;
RecentWall = collided;
newPosition = hit.point;
Previouscollided.layer = LayerMask.NameToLayer("Wall");
Previouscollided = hit.collider.gameObject;
CollidedCheck();
}
}
}
private void CollidedCheck()
{
if (collided.tag == "Wall")
{
StartCoroutine("LerpPos");
}
}
private IEnumerator LerpPos()
{
if (DoesThisPoint != newPosition)
{
timer = 0;
}
//DoesThisPoint = NewPosition;
while (Vector3.Distance(CurrentPointToLerpFromPosition, newPosition) > 3)
{
timer += Time.deltaTime;
RayPointPreviousPlace = CurrentPointToLerpFromPosition;
CurrentPointToLerpFromPosition = Vector3.Lerp(PointToLerpFrom, newPosition, timer * speed);
transform.position = RayPointPreviousPlace;
yield return null;
}
}
}
I wanted to trying to stop relying on unity answers and have been working on this for a lot of hours but I don't know what I'm doing wrong :/
Answer by tanoshimi · Jul 23, 2017 at 07:09 AM
In the process of transferring your player script to work for your enemies, you don't seem to have updated many of the important references.
Vector3 rayDir = Player.transform.position - transform.position;
for example - why is the enemy casting a ray from the player's position?
actually no you're wrong. Its being cast from the enemy's position as when I switch them the ray is pointing backwards from the player
Your answer

Follow this Question
Related Questions
My object now lerps but it only lerps once 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
External Libraries in C# scripts 1 Answer
Why my object not disappear? 1 Answer