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 /
avatar image
0
Question by ragefordragons · Jun 09, 2018 at 12:08 AM · enemyvelocitytransform.translateartificial intelligence

Help fixing AI?

So my AI works pretty well right now, but there are things I want it to do that wont work with the code I have later on. Im am currently moving it towards the player using transform.translate, but I want to switch it to velocity so it wil have collision and i will be able to mak things like knockback. Although, I dont have any real idea how to switch it velocity from here, so I was wondering if anyone could help me out. (I assume its pretty simple but I cant seem to figure it out)

Heres my enemy AI script:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class GruntAI : MonoBehaviour {

 public float moveSpeed;

  
 public float shootSpeed;

 private bool inRange;
 private bool canGen;

 public float patrolArea;
 

 private Transform playerPostion;

 private Vector2 randPosition;

 private Rigidbody2D rb;


 // Use this for initialization
 void Start () {
     playerPostion = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();

     inRange = false;
     canGen = true;

     randPosition = new Vector2(Random.Range(patrolArea * -1, patrolArea), Random.Range(patrolArea * -1, patrolArea));

     rb = GetComponent<Rigidbody2D>();

  
 }
 
 // Update is called once per frame
 void Update () {

     if (inRange == true)
     {

         transform.position = Vector2.MoveTowards(transform.position, playerPostion.position, moveSpeed * Time.deltaTime);

         

     } else
     {

      transform.position = Vector2.MoveTowards(transform.position, randPosition, moveSpeed / 2 * Time.deltaTime);

     }

     if (canGen == true)
     {
         StartCoroutine(generatePosition());
     }

     
     
 }


 private void OnTriggerEnter2D(Collider2D other)
 {
     if (other.tag == "Player")
     {
     inRange = true;

     }
 }

 private void OnTriggerExit2D(Collider2D other)
 {
     if (other.tag == "Player")
     {
         inRange = false;

     }
 }

 IEnumerator generatePosition()
 {
     canGen = false;

     randPosition = new Vector2(Random.Range(patrolArea * -1, patrolArea), Random.Range(patrolArea * -1, patrolArea));

     yield return new WaitForSeconds(1);

     canGen = true;
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by MT369MT · Jun 09, 2018 at 10:31 AM

Hi, try with this:

     public float moveSpeed;
 
     public float shootSpeed;
     private bool inRange;
     public float patrolArea;
 
     private Transform playerPosition;
     private Vector2 randPosition;
     private Rigidbody2D rb;
 
     // Use this for initialization
     void Start()
     {
         playerPosition = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
         inRange = false;
         randPosition = new Vector2(Random.Range(patrolArea * -1, patrolArea), Random.Range(patrolArea * -1, patrolArea));
         rb = GetComponent<Rigidbody2D>();
         InvokeRepeating("generatePosition", 0, 1);
     }
 
     // Update is called once per frame
     void Update()
     {
         if (inRange == true)
         {
             rb.velocity = (playerPosition.position - transform.position).normalized * moveSpeed * Time.deltaTime;
 
         }
         else
         {
             rb.velocity = (new Vector3(randPosition.x, randPosition.y, 0) - transform.position).normalized * moveSpeed * Time.deltaTime;
         }
 
     }
     private void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Player")
         {
             inRange = true;
         }
     }
     private void OnTriggerExit2D(Collider2D other)
     {
         if (other.tag == "Player")
         {
             inRange = false;
         }
     }
     public void generatePosition()
     {
         randPosition = new Vector2(Random.Range(-patrolArea, patrolArea), Random.Range(-patrolArea, patrolArea));
     }
 

If when you press play the enemy doesn't move increase the moveSpeed. I also changed the Coroutine, WaitForSeconds with a InvokeRepeating Function. If you don't need it copy only the part with rb.velocity = ...

Comment
Add comment · Show 4 · Share
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 ragefordragons · Jun 09, 2018 at 05:05 PM 0
Share

Yup, that works! I havent really used an Invoke Repeating yet so thats good to know. Do you have any idea how adding animations to an enemy based on how its moving would work? Thanks a ton though!

avatar image MT369MT · Jun 09, 2018 at 06:02 PM 0
Share

How many move animations do you have? One for each direction (up, down, right, left) or its only one and you want to make it facing your direction?

avatar image ragefordragons MT369MT · Jun 10, 2018 at 03:45 AM 0
Share

Yeah 2 frames of walking in each direction.

avatar image MT369MT ragefordragons · Jun 10, 2018 at 09:11 AM 0
Share

I found here a good answer about it. In the second part of the answer there is the code if you have 4 directions animation and you want to have the closest one (if you walk 45º it will be an Up Animation)

https://answers.unity.com/questions/515880/how-to-play-correct-animation-based-on-direction.html

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

84 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

Related Questions

Error: Method not found: 'UnityEngine.Rigidbody.Translate'. 1 Answer

My bullet has no velocity after it is spawned. 1 Answer

Rigidbody Enemy and Collisions 2 Answers

Unity2D Parent of prefab is instantiating, but not moving. 0 Answers

Help!!! with rain indie with fps control!!! 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