I having problem with writing the code for my game charging enemy…The enemy was suppose to like a rhino charge…Where it will detect the player location and charge at the point. But it seem to flicker after it start charging… need help with it. Thanks
using UnityEngine;
using System.Collections;
public class Enemy_Charge : MonoBehaviour
{
public GameObject Player;
public float Velocity = 0;
public Vector3 movement;
bool isCharging = false;
bool ischargeLeft = false;
bool ischargeRight = false;
void Update ()
{
Charge();
}
public void Charge()
{
if(isCharging == false)
{
if(Velocity >= 1)
{
Velocity -= 1;
}
else
{
Velocity = 0;
}
Invoke("CalculateDirection",2);
}
else
{
if (ischargeLeft)
{
if (Player.transform.position.x > this.transform.position.x || Player.transform.position.x == this.transform.position.x)
{
isCharging = false;
}
else
{
Velocity += 1;
movement.x = -0.5f * Velocity;
movement.y = this.transform.position.y;
movement.z = this.transform.position.z;
this.transform.position = movement;
}
}
else if (ischargeRight)
{
if (Player.transform.position.x < this.transform.position.x || Player.transform.position.x == this.transform.position.x)
{
isCharging = false;
}
else
{
Velocity += 1;
movement.x = 0.5f * Velocity;
movement.y = this.transform.position.y;
movement.z = this.transform.position.z;
this.transform.position = movement;
}
}
}
}
public void CalculateDirection()
{
if (Player.transform.position.x < this.transform.position.x)
{
ischargeLeft = true;
ischargeRight = false;
isCharging = true;
}
else if (Player.transform.position.x > this.transform.position.x)
{
ischargeRight = true;
ischargeLeft = false;
isCharging = true;
}
}
}