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);
}
}
}
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. :)
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.
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.
}
}
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.
@Younes Comment the first timer = 0.0f; and uncomment the second one. :)
@Younes, Did you try this solution with the uncomment part. Is it working ?
Your answer
![](https://koobas.hobune.stream/wayback/20220612032755im_/https://answers.unity.com/themes/thub/images/avi.jpg)