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 Adam Bruns · Feb 28, 2010 at 06:56 PM · movementpositionloopjavascript-specific

Move a Person to a point / Repeat a function called from another script?

So I have 2 scripts set up, one on the monster and one on the player, when the player right clicks on the monster it tells the monster to send the player to start moving towards it. (if you need to know more about those scripts to call the function, see my last question http://answers.unity3d.com/questions/4845/attack-a-target-using-mouse-over/4850#4850)

now the problem is when you right click the monster, it only moves a little bit, and you have to actually spam right click on it to move anywhere. My function to move is:

var speed = 4.0; function AttackMonster(Position) { var targetRotation = Quaternion.LookRotation(Position - transform.position);

     transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);

     // Move the object forward.

     transform.position += transform.forward * speed * Time.deltaTime;

}

is there a way to set it to loop inside that function, so it only loops when its called and until it is a certain distance away from the targets position (that way it doesnt end on top of it)?

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
1
Best Answer

Answer by StephanK · Feb 28, 2010 at 11:58 PM

Yes, you can use a Coroutine for this. Add a while-loop around your movement code. At the end of the loop insert:

yield WaitForFixedUpdate();

As breaking condition for the while-loop check if the distance to target is below a certain value. The function will execute until the yield statement and and continue the loop on the next fixedUpdate.

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 Adam Bruns · Mar 01, 2010 at 05:21 AM 0
Share

thank you very much i got it to work :D

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can't transform position of an object after a certain point. Help? 0 Answers

Translate Craig Reynolds's steering behaviors 0 Answers

Getting an Objects position once 2 Answers

Why is Input.GetAxisRaw() not returning whole numbers when using a joystick? 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