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 paulson_101 · Dec 12, 2018 at 07:33 AM · 2d sprites2d animation

Random 2D NPC within defined area

I really need help. I need to be able to define an area, or use collision. And have an NPC with an idle and walk cycle using a blend tree. I just want the character to idle then walk for a bit to a random spot, idle... And so on forever. I have looked everywhere for this! Please help.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by kaosermail · Dec 12, 2018 at 11:27 AM

You can use Navigation for this: https://docs.unity3d.com/Manual/Navigation.html

First define an area to be walkable. Then asign a nav agent to the NPC. Also add to the nav agent a script where you change his destination every random seconds.

 // MoveTo.cs
 using UnityEngine;
 using System.Collections;
 
 public class MoveTo : MonoBehaviour {
    
    public Transform goal;
    float time;
    float randomSeconds;
    
    void Start () {
       NavMeshAgent agent = GetComponent<NavMeshAgent>();
       agent.destination = goal.position; 
       randomSeconds = Random.Range(3, 10));
       time = Time.time;
    }

     void Update () {
       if (time - Time.time > randomSeconds)
       {
           agent.destination = new Vector2 ( Random.Range(-1000, 1000), Random.Range(-1000, 1000));
           randomSeconds = Random.Range(3, 10));
           time = Time.time;
       }          
    }
 }

This way, he will stop when he reaches the end of the navigation walkable area.

Comment
Add comment · 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
0

Answer by paulson_101 · Dec 31, 2018 at 11:59 PM

I am still struggling with this basic concept. So I have a 2d NPC Sprite with an animation control. I also have a 2d Room, with collision on the walls. I jsut want the character to randomly walk around, using collision, and stopping on occassion. Why is this so hard for me to figure out! haha @kaosermail

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 kaosermail · Jan 01, 2019 at 02:26 AM 0
Share

The hard part of using Navigation is that you would need to use it as it was 3D (placing the plane horizontally....). If you think that it is too dificult I would suggest you to use the move function and stop every x seconds or when onTriggerEnter is activated and then change direction/ wait x seconds.

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

100 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

Related Questions

How can I change player sprite based on mouse position relative to the player? 2 Answers

How to Change a sprite mesh at a certain frame in an animation clip without script? 1 Answer

Making a 2d sprite play its animation at given waypoint. 1 Answer

Are sprite sheets treated like sparse textures in texture streaming, what size can I use? 0 Answers

How to flip hitboxes if my 2D Sprite is Flipped in AnimationsEditor ? [Unity2D] 1 Answer


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