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 /
  • Help Room /
avatar image
0
Question by Younes · Sep 07, 2015 at 03:24 PM · c#scripting problem

Unsure on how to use Invoke

I'm trying to make an object follow the mouse, script worked fine until I tried to use invoke to add a delay. No console errors but nothing happens.

 using UnityEngine;
 using System.Collections;
 
 public class HandToMouse : MonoBehaviour {
     
     public float moveSpeed = 1f;
     public float moveDelay = 0.5f;
     public Vector3 Position;
 
     void Update () {
             Vector3 pos = Input.mousePosition;
             pos.z = transform.position.z - Camera.main.transform.position.z;
             Vector3 Position = Camera.main.ScreenToWorldPoint(pos);
             Invoke("MoveHand", moveDelay);
         }
     
     void MoveHand() {
         if (Input.GetButton ("Fire1")) {
             transform.position = Vector3.MoveTowards (transform.position, Position, moveSpeed);
         }
     }
 
 }
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 Clu · Sep 07, 2015 at 03:44 PM 0
Share

You should probably move your Invoke out of Update and in to the $$anonymous$$oveHand method such that the Invoke isn't being called every frame. It could be getting overwritten... Ins$$anonymous$$d, after you've moved your Invoke, call $$anonymous$$oveHand once in your Start and then, every time the $$anonymous$$oveHand function is called, it will reschedule itself. :)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Clu · Sep 07, 2015 at 04:00 PM

@Younes You might want to move your Invoke out of Update and into the actual MoveHand method such that the Invoke is repeated every time the function is called instead of every frame in Update. It could be overwriting the Invoke... In your Start, just call MoveHand() directly and then, because you've moved your Invoke into that method, it should call itself again after the delay.

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
avatar image
0

Answer by MaT227 · Sep 07, 2015 at 03:55 PM

If the aim is to add a delay to your move hand this is not the right way to do that. I would suggest you to do that but it also depends on the correct behaviour you need.

The small explanation is that you always call for Invoke and inside it you check for the fire button, this should be inverted. You might need to check for the fire button and then Invoke your function. But there is an other issue,you might need to use the MoveTowards inside the update loop, then the Invoke method is not possible so I did a simple timer.

If you need more informations, feel free to ask.

 public float moveSpeed = 1f;
 public float delay = 0.5f;
 public Vector3 targetPosition;
 public bool    move = false;
 
 private float timer = 0.0f;
 
 void Update()
 {
     if (Input.GetButton("Fire1"))
     {
         Vector3 mousePosition = Input.mousePosition;
         mousePosition.z = transform.position.z - Camera.main.transform.position.z;
         targetPosition = Camera.main.ScreenToWorldPoint(mousePosition);
         move = true;
         timer = 0.0f;    // If you reset the timer here, the move stops and wait befor targeting the new position.
     }
 
     if (move)
     {
         timer += Time.deltaTime;
         if (timer >= delay)
         {
             MoveHand();
         }
     }
 }
     
 void MoveHand()
 {
     float step = moveSpeed * Time.deltaTime;
     transform.position = Vector3.MoveTowards(transform.position, targetPosition, step);
     if (transform.position == targetPosition)
     {
         move = false;
 //        timer = 0.0f;    // If you reset the timer here, the move won't be delayed when clicking fast.
     }
 }




Comment
Add comment · Show 3 · 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 Younes · Sep 07, 2015 at 04:05 PM 0
Share

The script works but now the object doesn't follow the mouse when I hold Fire1. $$anonymous$$y aim was to let the mouse lay out a path that the object follows with a delay, figuratively speaking.

avatar image MaT227 · Sep 07, 2015 at 04:21 PM 0
Share

@Younes Comment the first timer = 0.0f; and uncomment the second one. :)

avatar image MaT227 · Sep 08, 2015 at 07:46 AM 0
Share

@Younes, Did you try this solution with the uncomment part. Is it working ?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to add a editing option to variables in a script in Inspector? 0 Answers

How to create buttons with script? 0 Answers

Side-scrolling Enemy AI not shooting in the right direction 0 Answers

How to make a random object generator that responds to simple touch? 0 Answers

Script don't work if connected to vehicle 0 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