Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Makiavel · Apr 10, 2012 at 07:58 AM · 2dmovementrandomdirectioncycle

Random movement direction cycle.

I wanted to create a script, that moves an object in one direction and changes the direction of the movement every three seconds. But it returns error. Could you please see what is wrong here?

     var speed = Vector3 (0, 0, 0);
 
 
 function Start() 
 {
 var randomDirection : Vector3 = new Vector3(Random.Range(-359, 359),0,Random.Range(-359, 359));
 StartCoroutine(ChangeDirection(3.0));
 }
 
 
 
 
 function Update()
 {
 rigidbody.MovePosition(rigidbody.position + speed * Time.deltaTime);
 
 
 
 }
 
 
 
 
 function ChangeDirection (waitTime : float) 
 {
 yield WaitForSeconds (waitTime);
 var randomDirection : Vector3 = new Vector3(Random.Range(-359, 359),0,Random.Range(-359, 359));
 StartCoroutine(WaitAndStart(1.0));
 }
 
 function WaitAndStart (waitTime : float) 
 {
 yield WaitForSeconds (waitTime);
 var randomDirection : Vector3 = new Vector3(Random.Range(-359, 359),0,Random.Range(-359, 359));
 StartCoroutine(ChangeDirection(3.0));
 }





Error is:

1)Assets/Blind_Scripts/Random_2D_Movement.js(35,16): BCE0070: Definition of 'Random_2D_Movement.WaitAndStart' depends on 'Random_2D_Movement.ChangeDirection' whose type could not be resolved because of a cycle. Explicitly declare the type of either one to break the cycle.

2)Assets/Blind_Scripts/Random_2D_Movement.js(35,15): BCE0023: No appropriate version of 'UnityEngine.MonoBehaviour.StartCoroutine' for the argument list '(unknown)' was found.

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 fafase · Apr 10, 2012 at 08:02 AM 0
Share

When you post, highlight the code part and press 101010 to make it easier to read. Also, you should post the error so that it can be fixed faster if we already know where it is wrong.

avatar image fafase · Apr 10, 2012 at 09:50 AM 0
Share

Your problem is that one function relies on the other while the other has not yet given any result. the second error is telling you that you call a function that does not exist.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by fafase · Apr 10, 2012 at 08:17 AM

Ok, I reviewed your script just by head so try and report any error so that we can fix it.

     var speed : Vector3; 
     var timing :float;
     
     function Start() { 
        // I reckon you are in 2D so z is 0
        speed = Vector3(Random.Range(-359, 359),Random.Range(-359, 359),0); 
        timing = 0;
     }
     
     function FixedUpdate() { 
        timing+=Time.deltaTime;
        rigidbody.MovePosition(rigidbody.position + speed * Time.deltaTime); 
         if(timing > 3){
            ChangeDirection();
            timing = 0;
         }
     }
            
     function ChangeDirection () { 
         speed = new Vector3(Random.Range(-359, 359),0,Random.Range(-359, 359),0);  }
Comment
Add comment · Show 6 · 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 Makiavel · Apr 10, 2012 at 10:48 AM 0
Share

Thank you so much, I see now what a mess my script was) This one works perfectly!

avatar image Makiavel · Apr 11, 2012 at 11:23 AM 0
Share

I'm sorry, but I've got another question. Is there a way to point an object of script in the direction of movement? I can not find a way to use a current Vector3 as a looking direction, not via LookAt, not via Rotate functions...

avatar image fafase · Apr 11, 2012 at 01:17 PM 0
Share

Do you mean you can figure out LookAt()?

target:Transform; // drag your target object in the inspector

function Update(){ transform.LookAt(target); }

Or explain a little more I did not quite get what you meant.

avatar image Makiavel · Apr 11, 2012 at 02:49 PM 0
Share

I mean that I am trying to make the object, that I attach this script to, face in the direction of the movement. And each time the direction of movement changes, so does the facing direction. And so on...

avatar image fafase · Apr 11, 2012 at 03:32 PM 0
Share

"I wanted to create a script, that moves an object in one direction and changes the direction of the movement every three seconds." I am lost, that is what you wanted but now you don'ty want it?

Show more comments
avatar image
0

Answer by fafase · Apr 11, 2012 at 05:42 PM

 var moveSpeed:float = 10;
 var timing :float;
 var target:Vector3;

 function Start() { 
    timing = 0;
 }

 function FixedUpdate() { 
    timing+=Time.deltaTime;
    transform.position += transform.forward*moveSpeed*Time.deltaTime
     if(timing > 3){
        ChangeDirection();
        timing = 0;
     }
 }

 function ChangeDirection () { 
     target.position = Vector3(Random.Range(-359, 359),Random.Range(-359, 359),0);  }


Well , I tried to review with the information I understood, now you get a random position on the map to where the guy is heading. It changes every 3 seconds.

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to create random movement in 2d 2 Answers

Random Movement : 2d 1 Answer

How to create random movement in 2D 2 Answers

Unity 2D Random Movement 1 Answer

Need player to walk in random direction 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