Unity - Enemy AI; Run away from player (x and y)
By : Matt M
Date : March 29 2020, 07:55 AM
should help you out You want the campers to run away from the player right? Then you should do code :
Vector3 moveDirection = transform.position - Player.transform.position
character.Move(moveDirection.normalized * speed * Time.deltaTime);
|
Instantiated Enemy outputs double damage for every enemy spawning after him
By : Diego Solis
Date : March 29 2020, 07:55 AM
it fixes the issue There are a few problems with your scripts... I don't think you understand what a static variable is. code :
public static bool inSight;
PlayerMovement.isEnemyAttacking
using UnityEngine;
using UnityEngine.UI;
public class PlayerHealth : MonoBehaviour
{
public static float Health;
public static float maxHealth = 100;
private Text healthText;
void Start()
{
healthText = GameObject.FindGameObjectWithTag("HealthPoints").GetComponent<Text>();
//Make it full 100% health on start
Health = maxHealth;
RefreshHealthBar();
}
public void TakeDamage(float damage)
{
Health -= damage;
RefreshHealthBar();
if (Health <= 0)
Die();
}
public void Die()
{
Health = 0;
RefreshHealthBar();
//TODO: Your code
}
void RefreshHealthBar()
{
healthText.text = "HEALTH: " + Health;
}
}
using System.Collections;
using UnityEngine;
public class EnnemyAI : MonoBehaviour
{
public float AttackDamage = 1.0f;
public float AttackSpeed = 1.0f;
public float AttackRange = 1.0f;
private bool isPlayerInSight;
private GameObject target;
private NavMeshAgent agent;
// Use this for initialization
void Start ()
{
target = GameObject.FindGameObjectWithTag("Player");
agent = GetComponent<NavMeshAgent>();
StartCoroutine(AttackLoop());
}
// Update is called once per frame
void Update ()
{
if (isPlayerInSight)
{
agent.destination = target.transform.position;
}
}
IEnumerator AttackLoop()
{
while (true)
{
//I don't know your attacking logic, so lets say they can attack in a 1 unit range
while (Vector3.Distance(target.transform.position, this.transform.position) <= AttackRange)
{
Attack();
yield return new WaitForSeconds(AttackSpeed);
}
yield return 0;
}
}
void Attack()
{
target.GetComponent<PlayerHealth>().TakeDamage(AttackDamage);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
isPlayerInSight = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
isPlayerInSight = false;
}
}
}
|
Unity Survival Shooter Enemy taking no damage - Android
By : faris zaini
Date : March 29 2020, 07:55 AM
this will help You are already using Layer to make sure that the raycast hit just the GameObjects on the shootableMask layer which is the "Shootable" layer so layer issue problem is eliminated. The only possible problem left is that the EnemyHealth script is not attached to the GameObject or maybe some of the GameObjects.
|
Cause player to take damage in Unity 2D
By : user2066269
Date : March 29 2020, 07:55 AM
Any of those help I made two scripts. One that'll keep track of the player's health, health bar and cause the screen to flash when the player is damaged. The other script is meant to be placed on any object I wish to do damage to the player, on contact. My problem is, Nothing seems to be doing any damage to the player. , Make sure that the players Collider has isTrigger enabled. code :
[Range(1,100)] public int attackDamage = 10;
private void OnTriggerEnter2D(Collider2D col)
{
// gets PlayerHealth component on this or any parent object
var health = col.GetComponentInParent<PlayerHealth>();
if (health == playerHealth)
{
Attack();
}
}
private void Update()
{
damaged = false;
}
public void TakeDamage(int amount)
{
damaged = true;
currentHealth -= amount;
healthSlider.value = currentHealth;
}
player = GameObject.FindGameObjectWithTag("Player");
playerHealth = player.GetComponent<PlayerHealth>();
playerHealth = FindObjectOfType<PlayerHealth>();
private void OnTriggerEnter2D(Collider2D col)
{
// gets PlayerHealth component on this or any parent object
var health = col.GetComponentInParent<PlayerHealth>();
if (health != null)
{
Attack(health);
}
}
void Attack(PlayerHealth health)
{
timer = 0f;
if(health.currentHealth > 0)
{
health.TakeDamage(attackDamage);
}
}
|
Unity 2D rotate the AI enemy to look at player
By : Joshua Evenson
Date : March 29 2020, 07:55 AM
I hope this helps . You need a function that would flip your sprite, you can do that by changing the scale of the transform, and keep a boolean to check where it's facing bool facingRight;, so something like this code :
void Flip(){
Vector3 scale = transform.localScale;
scale.x *= -1;
transform.localScale = scale;
facingRight = !facingRight;
}
if (Vector3.Distance(target.position,transform.position)<20)
{
transform.position=Vector2.MoveTowards(transform.position, target.position,speed*Time.deltaTime);
if(target.position.x > transform.position.x && !facingRight) //if the target is to the right of enemy and the enemy is not facing right
Flip();
if(target.position.x < transform.position.x && facingRight)
Flip();
}
|