Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by KreeperGaming · Sep 19, 2016 at 11:46 PM · 2daienemyplatformerenemyai

2D Enemy Ai

I just can't get it to work. At all.

he just shrinks into the ground, and he just shakes. It hurts my eyes too.

Enemy AI Script

 using UnityEngine;
 using System.Collections;
 
 public class Character : MonoBehaviour {
 
     [SerializeField]
     private GameObject player;
     private Rigidbody2D body;
     public Vector2 velocity;
 
     void Start()
     {
         body = GetComponent<Rigidbody2D>();
     }
 
     void FixedUpdate()
     {
         if(transform.position.x > player.transform.position.x)
         {
             body.velocity = velocity * -1;
         }
         else if (transform.position.x < player.transform.position.x)
         {
             body.velocity = velocity;
         }
         else
         {
             body.velocity = new Vector2(0, 0);
         }
     }
 }
 

Player Script, if you need it

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
 
     [SerializeField]
     private float movementSpeed = 5;
 
     private bool attack;
 
     private bool slide;
 
     private Rigidbody2D myRigidBody;
 
     private Animator anim;
 
     private bool facingRight;
 
     [SerializeField]
     private Transform[] groundPoints;
 
     [SerializeField]
     private float groundRadius;
 
     [SerializeField]
     private LayerMask whatIsGround;
 
     private bool isGrounded;
 
     private bool jump;
 
     private bool jumpAttack;
 
     [SerializeField]
     private bool airControl;
 
     [SerializeField]
     private float jumpForce;
 
     // Use this for initialization
     void Start () 
     {
         myRigidBody = GetComponent<Rigidbody2D>();
         facingRight = true;
         anim = GetComponent<Animator>();
     }
 
     void Update() //Check everthing in frames
     {
         HangleInput();
     }
     
     // Update is called once per frame
     void FixedUpdate () //Check Everything in seconds
     {
         float horizontal = Input.GetAxis("Horizontal");
 
         isGrounded = IsGrounded();
 
         HandleMovement(horizontal);
 
         Flip(horizontal);
 
         HandleAttacks();
 
         HandleLayers();
 
         ResetValues();
     }
 
     private void HandleMovement(float horizontal) // Handles movement
     {
 
         if(myRigidBody.velocity.y < 0)
         {
             anim.SetBool("land", true);
         }
 
 
         if(!anim.GetBool("slide") && (isGrounded || airControl))
         {
             myRigidBody.velocity = new Vector2 (horizontal * movementSpeed, myRigidBody.velocity.y);
         }
 
         anim.SetFloat("speed", Mathf.Abs(horizontal));
 
         if(isGrounded && jump)
         {
             isGrounded = false;
             myRigidBody.AddForce(new Vector2( 0, jumpForce));
             anim.SetTrigger("jump");
         }
 
         if(slide && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("Slide"))
         {
             anim.SetBool("slide", true);
         }
         else if (!this.anim.GetCurrentAnimatorStateInfo(0).IsName("Slide"))
         {
             anim.SetBool("slide", false);
         }
     }
 
     private void HandleAttacks() //Handles all attacks
     {
         if(attack && isGrounded && !this.anim.GetCurrentAnimatorStateInfo(0).IsTag("Attack"))
         {
             anim.SetTrigger("attack");
         }
         if(jumpAttack && !isGrounded && !this.anim.GetCurrentAnimatorStateInfo(1).IsName("JumpAttack"))
             {
                 anim.SetBool("jumpAttack", true);
             }
         if(!jumpAttack && !this.anim.GetCurrentAnimatorStateInfo(1).IsName("jumpAttack"))
             {
                 anim.SetBool("jumpAttack", false);
             }
     }
 
     private void HangleInput() // Handles all buttons pressed
     {
         if(Input.GetKeyDown(KeyCode.LeftShift) || Input.GetMouseButtonDown(0))
         {
             attack = true;
             jumpAttack = true;
         }
 
         if(Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S))
         {
             slide = true;
         }
 
         if(Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
         {
             jump = true;
         }
     }
 
     private void Flip (float horizontal) // flips player direction
     {
         if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
         {
             facingRight = !facingRight;
 
             Vector3 theScale = transform.localScale;
 
             theScale.x *= -1;
 
             transform.localScale = theScale;
         }
 
     }
 
     private void ResetValues() //resets values to default
     {
         attack = false;
         slide = false;
         jump = false;
         jumpAttack = false;
     }
 
     private bool IsGrounded() // checks if we are grounded
     {
         if(myRigidBody.velocity.y <= 0)
         {
             foreach (Transform point in groundPoints)
             {
                 Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
 
                 for(int i = 0; i < colliders.Length; i++)
                 {
                     if(colliders[i].gameObject != gameObject)
                     {
                         anim.ResetTrigger("jump");
                         anim.SetBool("land", false);
                         return true;
                     }
                 }
 
             }
         }
         return false;
     }
 
     private void HandleLayers()
     {
         if(!isGrounded)
         {
             anim.SetLayerWeight(1, 1);
         }
         else
         {
             anim.SetLayerWeight(1, 0);    
         }
     }
 }
 

Its long, I know.

Thanks!

Comment
Add comment · Show 2
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
avatar image Fornoreason1000 · Sep 20, 2016 at 08:01 AM 0
Share

you don't really need your player script in this Question. since your using its transform values. can you include your RigidBody2D settings for that Enemy? that will be very helpful.

avatar image KreeperGaming Fornoreason1000 · Sep 20, 2016 at 10:46 PM 0
Share

I haven't changed the settings. I am still having this problem. Thanks.

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.

Follow this Question

Answers Answers and Comments

127 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 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

AI stops following player. 1 Answer

platform ai following player direction when close, flips normal movement ,cant get my enemy to keep his sprite position when moving back to move after following during attack 1 Answer

How can I make a 4 direction animation for an enemy? 1 Answer

Vector 2 only using x (Enemy AI) 1 Answer

Enemy AI ignores the distance from the player 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