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 /
This question was closed Jan 31, 2017 at 10:22 AM by NovacainE2 for the following reason:

Duplicate Question

avatar image
0
Question by NovacainE2 · Nov 09, 2016 at 06:38 PM · animationmovementrandom

How to combine anim states with movement in a random movement game.

Ok so I am sorry because I don't know how to express my problem in one sentence but here it is:

Im trying to create a 2D top down game. The game will be about a zoo and animals. My main goal here is to create a prefab of an animal and scatter a few of them inside a fence. So what I want to do is for this prefab to wander around the cage doing random movement. The prefab has two states. One idle and one walking. My problem is that while I have found a way(through the community) to create a random way to choose between states and displaying them correctly, my 2D sprite won't move. enter code here` void Start() {

     InvokeRepeating("ExecuteAfterTime", 2, 4);

     //yield return StartCoroutine(ExecuteAfterTime(2.0F));
     animator = GetComponent<Animator>();
 }
 void ExecuteAfterTime()
 {
     {
         randvalue = Random.value;
         if (Random.value >= 0.5)
         {
             Idle = true;
         }
         else
             Idle = false;

         // change to random direction at random intervals
         if (Time.time >= tChange)
         {
             randomX = Random.Range(-2.0f, 2.0f); // with float parameters, a random float
             randomY = Random.Range(-2.0f, 2.0f); //  between -2.0f and 2.0f is returned
                                                  // set a random interval between 0.5f and 1.5f
             tChange = Time.time + Random.Range(0.5f, 1.5f);
         }

         if (Idle != true)
         {

             isCoroutineExecuting = true;

                 transform.Translate(new Vector3(randomX, randomY, 0) * moveSpeed * Time.deltaTime);

                 animator.SetBool("DinoIdle", false); // DInoIdle is my bool which changes the anim states.
             


         }
         else

             animator.SetBool("DinoIdle", true);

         // if object reached any border, revert the appropriate direction
         if (transform.position.x >= maxX || transform.position.x <= minX)
         {
             randomX = -randomX;

         }
         if (transform.position.y >= maxY || transform.position.y <= minY)
         {
             randomY = -randomY;
         }
         // make sure the position is inside the borders
         Vector3 temp = transform.position;
         temp.x = Mathf.Clamp(transform.position.x, minX, maxX);
         temp.y = Mathf.Clamp(transform.position.y, minY, maxY);
         moveSpeed = Random.Range(0.5f, 2.0f);

         moveSpeed = Random.Range(0.5f, 2.0f);

         isCoroutineExecuting = false;
         
     }

 }

 void Update()
 {

       if (isCoroutineExecuting)
         {

         transform.Translate(new Vector3(randomX, randomY, 0) * moveSpeed * Time.deltaTime);
         }
     


 }`


I imagine this is happening since the movement takes place inside the coroutine and not inside the update. But if I put the code of the couroutine inside the update function, then the prefab changes states like crazy( which makes sense ) and ruins what I am trying to achieve. Is there any way I can do this? Am I using the wrong approach? I figured I should use Navmesh but I want to be able to click on an animal and force it to go a predefined route (a single target, not wherever the player wants). My programming skills are not the best but I'm trying. Thank you for reading this, any help will be great.

Comment
Add comment · Show 2
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 Filhanteraren · Nov 09, 2016 at 11:53 PM 0
Share

I think you should go with Nav$$anonymous$$esh and a NavAgent, then you could make it wanter randomly and also point where is should go if you would like.

avatar image Filhanteraren · Nov 10, 2016 at 12:30 AM 0
Share

Quick code to have them walk random with Nav$$anonymous$$esh:

 using System.Collections;
 using UnityEngine;
 
 [RequireComponent(typeof(Nav$$anonymous$$eshAgent))]
 public class AnimalControl : $$anonymous$$onoBehaviour
 {
     private Nav$$anonymous$$eshAgent _nav$$anonymous$$eshAgent;
 
     public float WalkingRaduis;
 
     private void Start()
     {
         _nav$$anonymous$$eshAgent = GetComponent<Nav$$anonymous$$eshAgent>(); // Get reference to you Nav$$anonymous$$eshAgent
         WanderRandom();
     }
 
     private void Update()
     {
         if (_nav$$anonymous$$eshAgent.remainingDistance < 0.1f || _nav$$anonymous$$eshAgent.desiredVelocity.magnitude < 0.1f)
         {
             WanderRandom();
         }
     }
 
     private void WanderRandom()
     {
         _nav$$anonymous$$eshAgent.SetDestination(FindRandomPoint());
     }
 
     private Vector3 FindRandomPoint()
     {
         var direction = transform.forward;
         return direction + Random.insideUnitSphere * WalkingRaduis;
     }
 }

1 Reply

  • Sort: 
avatar image
0

Answer by NovacainE2 · Nov 10, 2016 at 02:52 PM

Hey @Filhanteraren ! Thank you so much for getting back to me and thanks for sharing the script. I'll do some research now and let you know about the results and also marking this thread as answered once I work on it. However do you have in mind what would it take to to change the animation states in a more discreet way? On my script I use a boolean variable to change between states but the problem that it doesn't work right because the animation must go along the movement part right? If I try to change states once an animal is moving (which happens in update) then the animation states would change every frame. The reason for that is that I am trying to achieve Idle and walking behaviors alike. So my goal is to make an animal wander around, stop, and wander around more in any random order with proper switching between the anim states. Anyway, thanks again, wish you all the best!

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

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Creating an animation with variables? 2 Answers

Handling Animation and movement speed 0 Answers

Tank Wheels and Treads 1 Answer

scripting for animation 0 Answers

Stopping movement when animation is playing 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