Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by milky_tunes · Jan 21, 2018 at 11:47 AM · 2dnpc

How to make a sprite walk on its own?

I want to make a sprite move around on it's own, like an npc, is this possible? I've tried a few scripts but they were from 2013 I realised after using them and testing them on monodevelop and to see there was 5 errors, if anybody knows if/how to do this I'd love if you were to give me the script, and is there any component I have to give to the object?

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 KittenSnipes · Jan 21, 2018 at 11:51 AM 0
Share

@milky_tunes

Why dont you post something you have tried and maybe we can help debug? This is not a community in which will always want to go through the work and make someone a script. You will be lucky to find someone kind enough for that. Ins$$anonymous$$d I can help you debug your script if you post it.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by milky_tunes · Jan 21, 2018 at 11:59 AM

Ah okay, of course I could send it, here it is:

{ { void Update () { comprobarDistancia(); if (walk) Walk (); else if(!walk && !isWaiting){ StartCoroutine(Wait()); }

  }

IEnumerator Wait(){ isWaiting = true; animation.CrossFade ("idle"); yield return new WaitForSeconds (5.0f); walk = true; isWaiting = false;

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 KittenSnipes · Jan 21, 2018 at 12:00 PM 0
Share

@milky_tunes

Can you click edit on your post and select all of the code and then click on the '101010' icon. $$anonymous$$akes the code easier to read.

avatar image KittenSnipes · Jan 21, 2018 at 12:02 PM 1
Share

Thats the full code? Seems really incomplete so that is probably where the errors come from.

avatar image
0

Answer by WinterboltGames · Jan 21, 2018 at 12:04 PM

You can do the following steps and write the following code and see if it works for you...

STEPS

  1. Create a gameObject in the scene

  2. Add a rigidbody component to it

  3. Add a 2d collider to it (example: BoxCollider2D)

  4. Create a new script call it NpcBehaviour

  5. Write the following code...

CODE using UnityEngine; public class NpcBehaviour : MonoBehaviour { public float speed; public float moveRate; public int dirX; public int dirY; private float moveCounter; private new Rigidbody2D rigidbody2D { get { return GetComponent<Rigidbody2D>() ?? default(Rigidbody2D); } } private void Update () { if (rigidbody2D) { if (moveCounter > moveRate) { ChangeDirection(); moveCounter = 0f; } Vector2 vel = new Vector2(dirX * speed, dirY * speed); rigidbody2D.velocity = Vector2.Lerp(rigidbody2D.velocity, vel, Time.deltaTime * 10f); moveCounter += Time.deltaTime; } } private void ChangeDirection () { dirX = Random.Range(-1, 1); // -1 or 0 or 1 dirY = Random.Range(-1, 1); // -1 or 0 or 1 } }

FINAL STEP 1. Attach the script to the desired game object

Hope this helps!

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 Chazmeister · Oct 08, 2020 at 09:03 PM 0
Share

how woould i make it so that the random range is only between -1 and 1 and not 0?

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

146 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

Related Questions

"Scripted" animations in 2d game 1 Answer

2D Animation does not start 1 Answer

C#, Problem with If Statements and GO Position Comparisons. 1 Answer

How to Move an NPC from anyplace to a specific place in 2D? 1 Answer

2D - Passing Through Colliders 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