How can i make a code about the fixed object's direction ?
I am making a code the enemy character that doesn't move and shoot the ball when the player character was enter the specific range. But, there is a problem that the enemy character can look right and left when player character is more or less than enemy's x position. But the enemy can't look front and back when player character is more or less than enemy's y position. Of course, the enemy's attacking is also applied.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyTypeTwo : MonoBehaviour {
public int hp = 5;
//private float damageTime=0.0f;
//private float moveTime=0.01f;
// Use this for initialization
public GameObject soul;
public GameObject monsterBall;
private GameObject target;
private float fireRate = 2.0f;
private float nextFire = 0.0f;
private float reactionRange=10f;
private bool mLeft, mRight, mUp, mDown=false;
private Animator animator;
void Start()
{
animator = GetComponent<Animator> ();
target = GameObject.Find("Player");
}
void Update()
{
Vector2 enemyPosition = new Vector2 (transform.position.x,transform.position.y);
Vector2 playerPosition = new Vector2 (target.transform.position.x,target.transform.position.y);
Vector2 distance = playerPosition - enemyPosition;
float ang = Mathf.Atan2 (distance.y,distance.x);
Debug.Log ("x:"+distance.x+"y:"+distance.y+"ang:"+ang);
if (hp == 0)
{
int rnd = Random.Range(0, 2);
if (rnd == 1)
{
Instantiate(soul, transform.position, Quaternion.identity);
}
GameObject.Destroy(gameObject);
portal.instance.count--;
portal.instance.PortalCheck();
}
if (transform.position.x>target.transform.position.x) {
//Vector3 scale = transform.localScale;
//scale.x = -Mathf.Abs(scale.x);
//transform.localScale = scale;
animator.SetBool ("IsLeft", true);
animator.SetBool ("IsRight", false);
animator.SetBool ("IsFront", false);
animator.SetBool ("IsBack", false);
animator.SetBool ("IsAttack", false);
animator.Play ("Flower_Left");
} else if (transform.position.x<target.transform.position.x) {
//Vector3 scale = transform.localScale;
//scale.x = Mathf.Abs(scale.x);
//transform.localScale = scale;
animator.SetBool ("IsLeft", false);
animator.SetBool ("IsRight", true);
animator.SetBool ("IsFront", false);
animator.SetBool ("IsBack", false);
animator.SetBool ("IsAttack", false);
animator.Play ("Flower_Right");
} else if (transform.position.y>target.transform.position.y) {
animator.SetBool ("IsLeft", false);
animator.SetBool ("IsRight", false);
animator.SetBool ("IsFront", true);
animator.SetBool ("IsBack", false);
animator.SetBool ("IsAttack", false);
animator.Play ("Flower_Front");
} else if (transform.position.y<target.transform.position.y) {
animator.SetBool ("IsLeft", false);
animator.SetBool ("IsRight", false);
animator.SetBool ("IsFront", false);
animator.SetBool ("IsBack", true);
animator.SetBool ("IsAttack", false);
animator.Play ("Flower_Back");
}
if (target.transform.position.x >= transform.position.x - reactionRange && target.transform.position.x < transform.position.x && Time.time > nextFire) {
mLeft = true;
mRight = false;
mUp = false;
mDown = false;
nextFire = Time.time + fireRate;
animator.SetBool ("IsLeft", true);
animator.SetBool ("IsRight", false);
animator.SetBool ("IsFront", false);
animator.SetBool ("IsBack", false);
animator.SetBool ("IsAttack", true);
Attack ();
animator.Play ("Flower_leftAttack");
} else if (target.transform.position.x <= transform.position.x + reactionRange && target.transform.position.x > transform.position.x && Time.time > nextFire) {
mLeft = false;
mRight = true;
mUp = false;
mDown = false;
nextFire = Time.time + fireRate;
animator.SetBool ("IsLeft", false);
animator.SetBool ("IsRight", true);
animator.SetBool ("IsFront", false);
animator.SetBool ("IsBack", false);
animator.SetBool ("IsAttack", true);
Attack ();
animator.Play ("Flower_rightAttack");
} else if (target.transform.position.y <= transform.position.y + reactionRange && target.transform.position.y > transform.position.y && Time.time > nextFire) {
mLeft = false;
mRight = true;
mUp = false;
mDown = false;
nextFire = Time.time + fireRate;
//animator.SetBool ("IsLeft", false);
//animator.SetBool ("IsRight", true);
//animator.SetBool ("IsFront", false);
//animator.SetBool ("IsBack", false);
//animator.SetBool ("IsAttack", true);
Attack ();
//animator.Play ("Flower_rightAttack");
} else if (target.transform.position.x >= transform.position.x + reactionRange && target.transform.position.x < transform.position.x && Time.time > nextFire) {
mLeft = false;
mRight = true;
mUp = false;
mDown = false;
nextFire = Time.time + fireRate;
//animator.SetBool ("IsLeft", false);
//animator.SetBool ("IsRight", true);
//animator.SetBool ("IsFront", false);
//animator.SetBool ("IsBack", false);
//animator.SetBool ("IsAttack", true);
Attack ();
//animator.Play ("Flower_rightAttack");
}
}
void Attack()
{
GameObject mBall=(GameObject)Instantiate(monsterBall,this.transform.position,Quaternion.identity);
Rigidbody2D EnemyBall = mBall.GetComponent<Rigidbody2D> ();
if (mLeft == true) {
//xBallMove = -ballSpeed * Time.deltaTime;
//ball.transform.Translate (Vector2.left*ballSpeed*Time.deltaTime);
EnemyBall.AddForce(new Vector2(-5,0),ForceMode2D.Impulse);
//transform.Translate(-ballSpeed*Time.deltaTime,0,0);
}
if (mRight == true) {
//xBallMove = ballSpeed * Time.deltaTime;
//ball.transform.Translate (Vector2.right*ballSpeed*Time.deltaTime);
EnemyBall.AddForce(new Vector2(5,0),ForceMode2D.Impulse);
//transform.Translate(ballSpeed*Time.deltaTime,0,0);
}
if (mUp == true) {
//xBallMove = ballSpeed * Time.deltaTime;
//ball.transform.Translate (Vector2.right*ballSpeed*Time.deltaTime);
EnemyBall.AddForce(new Vector2(0,5),ForceMode2D.Impulse);
//transform.Translate(ballSpeed*Time.deltaTime,0,0);
}
if (mDown == true) {
//xBallMove = ballSpeed * Time.deltaTime;
//ball.transform.Translate (Vector2.right*ballSpeed*Time.deltaTime);
EnemyBall.AddForce(new Vector2(0,-5),ForceMode2D.Impulse);
//transform.Translate(ballSpeed*Time.deltaTime,0,0);
}
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Player")
{
}
if (col.gameObject.tag == "Ball")
{
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Ball")
{
hp--;
}
}
}
(Green circle is enemy character and black one is player character.) I want that the enemy character look at the direction forward player when the player character enter the specific range like picture. And it's important that the enemy's position is based (For example, the enemy's position is based, the player character entered the right range like right of X shape. )
Of course, I want to make an enemy's action that throwing the ball forward up and down.
Your answer
![](https://koobas.hobune.stream/wayback/20220612123832im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Adjust object rotation based on player direction 0 Answers
Prefab Direction - shooting 2 Answers
How do I get the positions of the corners of a sprite? 2 Answers
Why does my reflection script not work? 0 Answers
GameObject null after SetParent() 1 Answer