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 Klasmic · Jul 18, 2017 at 07:22 AM · aimovement scriptenemyai

Make an object move towards a Player

I have been trying to make an ai that goes to the player and attacks him, and I have looked up tutorials on how to do it but they all use a patrol system, which I don't want to use.heres what I have so far using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class Deformed : MonoBehaviour {
 
     Rigidbody2D rb2D;
     Rigidbody2D playerRb2D;
     Transform trans;
     Transform playerTrans;
     public float Distancebetween;
 
     public GameObject player;
 
     void Start () {
         rb2D = gameObject.GetComponent<Rigidbody2D> ();
         trans = gameObject.GetComponent<Transform> ();
         playerTrans = player.GetComponent<Transform> ();
         playerRb2D = player.GetComponent<Rigidbody2D> ();
     }
     
     // Update is called once per frame
     void Update () {
         Distancebetween = playerTrans.position.x - trans.position.x;
 
         if (Distancebetween != 0){
             rb2D.AddForce( Vector2.right * playerRb2D.velocity.x);
         }
     }
 }
 
Comment
Add comment · Show 1
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 bobisgod234 · Jul 18, 2017 at 07:39 AM 0
Share

I am not sure what the question is.

The only issue I can see is the fact that you never use Distbetween, ins$$anonymous$$d you base the enemies force off the players velocity, which won't really get the enemy moving towards the player.

Perhaps something like this will work for you?

              rb2D.AddForce( Vector2.right * $$anonymous$$athf.Sign(Distancebetween) * 100f);
 

You will have to tweak the force (100f) to get it right, if its not moving, try making this super high.

Also try changing Update to FixedUpdate, as the AddForce functions are really designed to be called from FixedUpdate().

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ZeN12 · Jul 18, 2017 at 07:37 AM

public static int movespeed = 1; public Vector3 userDirection = Vector3.right; public Start() { } public void Update() { transform.Translate(userDirection * movespeed * Time.deltaTime); }

Need to detect userDirection before moving.

Comment
Add comment · Show 2 · 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 Klasmic · Jul 18, 2017 at 08:28 AM 0
Share

@ZeN12 all it does is move right

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Deformed : $$anonymous$$onoBehaviour {
 
     public static int movespeed = 1;
     public Vector3 userDirection = Vector3.right;
 
     public void Start() {
 
     }
 
     public void Update() {
         transform.Translate(userDirection * movespeed * Time.deltaTime); 
     }
 }

avatar image ZeN12 · Jul 18, 2017 at 08:50 AM 0
Share

Yes, now in updateLoop you need to detect where it should go. Update{ if (playerTrans.postion.x > transform.position.x){ userDirection = Vector3.right; }else{ userDirection = Vector3f.left; } transform.Translate(userDirection * speed * Time.deltaTime); }

}

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

110 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

Related Questions

AI code for enemy car to follow the player car 0 Answers

AI Enemy Follow Player 2 Answers

Moving AI away or towards player not working correctly. 1 Answer

How to make the enemy move back to its waypoint after it's target killed? 2 Answers

NavMeshAgent track Player when in Range? 2 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