How to Make A Character Stop At Wall?
Hi!
I have currently tried to make it where there is an enemy that targets the player and moves towards him. (works)
But I have also made a block placing script, it works, but the enemy does c stop towards when ti encounters the block. I want the enemy to stop right before the block, punch it (play the attack animation 3 times with a one second intermission after each attack), then the block will be removed and the enemy will continue following the player.
My current code:
using System.Collections; using UnityEngine.UI; using System.Collections.Generic; using UnityEngine;
public class Chase : MonoBehaviour {
public Transform player;
public GameObject CurrentEnemy;
private Animation anim;
public Slider Healthbar;
public Slider PlyrHB;
public Canvas DollarCanvas;
public Canvas DamageEffectCanvas;
public Canvas GameOverScrn;
public GameObject Block;
public bool BreakingBlock = false;
public bool SpeedEnabled = true;
public float EnemyWalkInt = 0.05f;
//public GameObject Enemy;
//public bool attackingTrue = false;
//public bool setToFalse = false;
// Use this for initialization
void Start () {
//StartCoroutine(DamageUI());
anim = GetComponent<Animation> ();
DollarCanvas = DollarCanvas.GetComponent<Canvas> ();
DamageEffectCanvas = DamageEffectCanvas.GetComponent<Canvas> ();
GameOverScrn = GameOverScrn.GetComponent<Canvas> ();
DamageEffectCanvas.enabled = false;
}
void OnColliderEnter (Collider other) {
EnemyWalkInt = 0.0f;
SpeedEnabled = false;
Debug.Log ("Stage affirmation 1");
// Vector3 EnemyLastLoc = new Vector3 (CurrentEnemy.transform.position.x, CurrentEnemy.transform.position.y, CurrentEnemy.transform.position.z - 0.325f);
AttackDefense ();
Debug.Log ("Stage affirmation 2");
}
IEnumerator AttackDefense () {
if (SpeedEnabled == false) {
CurrentEnemy.transform.position = new Vector3 (Block.transform.position.x, Block.transform.position.y, Block.transform.position.z - 0.325f);
EnemyWalkInt = 0.0f;
SpeedEnabled = false;
anim.Play ("Attack");
yield return new WaitForSeconds (1);
CurrentEnemy.transform.position = new Vector3 (Block.transform.position.x, Block.transform.position.y, Block.transform.position.z - 0.325f);
EnemyWalkInt = 0.0f;
SpeedEnabled = false;
anim.Play ("Attack");
yield return new WaitForSeconds (1);
CurrentEnemy.transform.position = new Vector3 (Block.transform.position.x, Block.transform.position.y, Block.transform.position.z - 0.325f);
EnemyWalkInt = 0.0f;
SpeedEnabled = false;
anim.Play ("Attack");
yield return new WaitForSeconds (1);
Debug.Log ("Point where it should work; Affirmation of SpeedEnabled bool issue");
EnemyWalkInt = 0.05f;
SpeedEnabled = true;
}
}
// Update is called once per frame
void Update () {
if (Healthbar.value <= 0)
return;
if (DollarCanvas.enabled == false) {
Vector3 direction = player.position - this.transform.position;
float angle = Vector3.Angle (direction, this.transform.forward);
if (Vector3.Distance (player.position, this.transform.position) < 999999999999 && angle < 360) {
direction.y = 0;
this.transform.rotation = Quaternion.Slerp (this.transform.rotation,
Quaternion.LookRotation (direction), 0.1f);
if (SpeedEnabled == true) {
if (BreakingBlock == false) {
if (direction.magnitude > 1.1175) {
anim.Play ("Walk");
//attackingTrue = false;
DamageEffectCanvas.enabled = false;
//Regening = true;;
this.transform.Translate (0, 0, EnemyWalkInt);
} else {
anim.Play ("Attack");
if (GameOverScrn.enabled == false)
DamageEffectCanvas.enabled = true;
else
DamageEffectCanvas.enabled = false;
//attackingTrue = true;
//DamageUI ();
}
}
}
} else {
anim.Play ("Idle");
}
} else {
//Enemy.transform.position = new Vector3(Enemy.transform.position.x, Enemy.transform.position.y, Enemy.transform.position.z - 1.0f);
DamageEffectCanvas.enabled = false;
anim.Play ("Idle");
}
if (DamageEffectCanvas.enabled == false) {
if (PlyrHB.value >= 1 && PlyrHB.value < 99)
PlyrHB.value += 0.0375f;
}
// } else {
// anim.Play ("Idle");
// }
}
}
Comment