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 Endercry365 · Apr 17, 2019 at 08:05 AM · ainpc

Making an AI for an NPC

I'm making a 2D RPG style game and was running into issues with making an NPC "companion" fallow behind the player. the idea i had was that the NPC would fallow a way point (that being the player) and animate itself as it fallows. The problem I have is that after the player animations stop, the companions animations stop. I need help making it so that the animations continues until it reaches the player. I used similar controls for the companion that i did for the player. Help would be much appreciated.

My Code:

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

public class WayPointFallow : MonoBehaviour {

 private Animator anim;

 private bool companionMoving;
 private Vector2 lastMove;

 public GameObject wayPoint;
 //This is how often your waypoint's position will update to the player's position
 private float timer = 0.5f;

 public GameObject target;
 public List<Vector3> positions;
 public int distance_permitted;
 public float speed;
 private Vector3 lastLeaderPosition;

 void Start()
 {
     anim = GetComponent<Animator>();

     positions.Add(target.transform.position);
 }

 void Update()
 {

     companionMoving = false;

     if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
     {

         companionMoving = true;
         lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
     }

     if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
     {

         companionMoving = true;
         lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
     }
     anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
     anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
     anim.SetBool("CompanionMoving", companionMoving);
     anim.SetFloat("LastMoveX", lastMove.x);
     anim.SetFloat("LastMoveY", lastMove.y);

     if (timer > 0)
     {
         timer -= Time.deltaTime;
     }
     if (timer <= 0)
     {
         //The position of the waypoint will update to the player's position
         UpdatePosition();
         timer = 0.5f;
     }

     if (lastLeaderPosition != positions[positions.Count - 1])
     {
         positions.Add(target.transform.position);
     }

     if (positions.Count >= distance_permitted)
     {
         if (gameObject.transform.position != positions[0])
         {

             transform.position = Vector3.MoveTowards(transform.position, positions[0], Time.deltaTime * speed);

         }
         else
         {
             positions.Remove(positions[0]);
             gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
             transform.position = Vector3.MoveTowards(transform.position, positions[0], Time.deltaTime * speed);
         }
     }
     lastLeaderPosition = target.transform.position;

 }

 void UpdatePosition()
 {
     //The wayPoint's position will now be the player's current position.
     wayPoint.transform.position = transform.position;
 }

}

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
0

Answer by Rati · Apr 17, 2019 at 08:06 PM

Here, your "companionMoving" is only true when you are geting axis data (so, when you are moving the joystick)

you should probably set it to false when he got to his destination, then setting it to tru again once his target has moved.

Comment
Add comment · Show 1 · 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 Endercry365 · May 01, 2019 at 05:52 PM 0
Share

void Update() { companion$$anonymous$$oving = false; //So this if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f) { companion$$anonymous$$oving = true; //And this last$$anonymous$$ove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f); } if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f) { companion$$anonymous$$oving = true; //And this last$$anonymous$$ove = new Vector2(0f, Input.GetAxisRaw("Vertical"));

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

171 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

A set of actions 1 Answer

Help needed for a dialogue/speech System 1 Answer

Making AI that can navigate through a city? 3 Answers

Moving from one position to another 3 Answers

3d text backwards when facing camera 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