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 drodrii · Mar 10, 2014 at 05:16 PM · transformvector3mouseyieldienumerator

Object Not Moving - Yield - IEnumerator

Hope everyone's doing amazing.

I want to move an object to the position of the mouse when I click.

Here's what happens, I've attached this script to the point of origin(player) using that point to create the object and calls for a function called getDestination.

 void Update () 
     {
         
 
 
         if(Input.GetMouseButtonDown(1))
         {
             playerPosition = player.transform.position;
 
             //Get users input position(where they shoot);
             
             userInputPosition = (new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
             userInputPosition = Camera.main.ScreenToWorldPoint(userInputPosition);
             
 
             Debug.Log ("User clicked here:" + userInputPosition);
             Debug.Log ("Here's The player:" + playerPosition);
 
         projectile = ObjectPoolManager.CreatePooled( object2Spawn, transform.position, player.transform.rotation ).GetComponent<bulletActions>();
 
             //Test to move the projectile.
             projectile.GetComponent<bulletActions>().getDestination(userInputPosition);
 
         }
         
     }

Get destination gets the point where the user clicked and the idea is that the object created moves smoothly towards that point, once the point is reached. Destroy the object.

 void OnTriggerEnter (Collider other) {
 
         Debug.Log ("OnTrigger");
         Debug.Log (collider.gameObject.name);
         EliminateThisObject ();
         Destroy (other.gameObject);
 
 
 
 
         }
 
 
 
     public void getDestination (Vector3 destination) 
     {
 
         
         Debug.Log ("Destination is :" + destination);
         Debug.Log ("Position is" + transform.position);
 
         projectileDestination = destination;
         gotDestination = true;
         moveToDestination ();
         
 
     }
     private IEnumerator moveToDestination ()
     {
         while (gotDestination == true) 
         {
             transform.position = Vector3.Lerp(transform.position, projectileDestination, moveSpeed * Time.deltaTime);
 
             Debug.Log ("mtd Destination is :" + projectileDestination);
             Debug.Log ("mtd Position is" + transform.position);
             if(transform.position == projectileDestination)
             {
                 EliminateThisObject();
                 gotDestination = false;
                 Debug.Log ("Update, position is the same as destination");
             }
             yield return null;
         }
     }

I've added a couple of Debugs with mtd on it to test if the method is happening "(IEnumerator moveToDestination)" but its not appearing. Am I doing the best way to accomplish this situation?

Thanks for your help and time!

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

Answer by supernat · Mar 10, 2014 at 09:59 PM

A couple of things you'll want to do. Use StartCoroutine() to start the moveToDestination, and place an if statement around the call to make sure it doesn't happen multiple times, like this:

 if (!getDestination)
 {
   getDestination = true;
   StartCoroutine(moveToDestination());
 }

Let me know if that doesn't do what you're looking to do. Basically, the StartCoroutine will run your moveToDestination method in small time slices (each yield call), but without it, I'm not sure what behavior to expect.

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 drodrii · Mar 10, 2014 at 10:23 PM 0
Share

Thanks, I looked it up and just like you said!! Thank you man!

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

21 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

Related Questions

Mouse-following script freezes Unity. 1 Answer

Make an Object Animate Up In The Update Function 2 Answers

Input.mousePosition returns coordinates as integers. 1 Answer

Move camera when mouse is near the edges of the screen 1 Answer

Move Transform to Target in X seconds 3 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