• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by ness3756 · Jun 12, 2017 at 08:43 AM · unity 2ddirection

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--;
             
         }
     }
         
 }
 

alt text (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.

스크린샷-2017-06-09-오후-95740.png (487.0 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

106 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Prefab Direction - shooting 2 Answers

Adjust object rotation based on player direction 0 Answers

Unity 2D: how to fire a renderline with raycast upward? 0 Answers

When I make my game objects Prefabs all the linked scripts break, when i relink all scripts in inspector, it dosnt work , but code and linked scripts works fine as objects 0 Answers

Specify currency for IAP in Unity's IAP Catalog 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges