I'm trying to make the mouse cursor automatically move from a random place to a specific place on my unity game screen.
I'm using Mouse.current.WarpCursorPosition, and Vector3.MoveTowards for what i want to make.
But it doesn't work at all.
Here's my code.
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.InputSystem;
public class TestMousemove : MonoBehaviour
{
public GameObject G;
public float Speed;
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
Mouse.current.WarpCursorPosition(transform.position =
Vector3.MoveTowards(transform.position, G.transform.position, Speed * Time.deltaTime));
}
}
}
and this is how it works.
I want to make it move smoothly like the cursor follows the location.
Is there anyone knows about the question, please answer here.
Thank you
CodePudding user response:
To me, it's quite strange to provide an argument and assign a value at the same time, but maybe I don't know about something. Nevertheless, it seems your expression transform.position = Vector3.MoveTowards(transform.position, G.transform.position, Speed * Time.deltaTime)
always produce the same result, hence, your mouse always warps to the same position.
When I changed it to:
transform.position =
Vector3.MoveTowards(transform.position, G.transform.position, Speed * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.A)) Mouse.current.WarpCursorPosition(
Vector3.MoveTowards(transform.position, G.transform.position, Speed * Time.deltaTime));
it seems to work, but when I move the mouse, it instantly warps to the position of transform.position Speed * Time.delta time.
My solution is to replace transform.position with your current mouse position, so the current
argument of MoveTowards
method will be the actual mouse position, not position of the object this script is attached to:
Mouse.current.WarpCursorPosition(
Vector2.MoveTowards(Mouse.current.position.ReadValue(), G.transform.position, Speed * Time.deltaTime));
CodePudding user response:
There are a couple problems here:
- You are not fully committed to the new input system, which the editor should warn you about. You're still using the Input class.
- The Mouse position is in screenspace while you attempt to use transforms from worldspace. Unless you did a ton of experimenting and fine tuning this will result in odd behavior.
- Your "Mouse movement code" is only active for one frame, namely the one where you press the
A
button. It's never going to be a smooth animation with only one frame.
Here's some code that should do what you want:
[SerializeField] Vector2 target;
[SerializeField] float speed = 100;
bool moving;
Vector2 position;
void Update()
{
if (Mouse.current.rightButton.isPressed)
{
position = Mouse.current.position.ReadValue();
moving = true;
}
if (moving)
{
position = Vector2.MoveTowards(position, target, Time.deltaTime * speed);
Mouse.current.WarpCursorPosition(position);
moving = Vector2.Distance(position, target) > 0.1f;
}
}
The mouse will start to move when you press the right mouse button. It will move at a speed of 100 pixels per second until it reaches the specified target notated in pixel units.
Note It might glitch in the editor, but will work guaranteed in the build. If you execute in windowed mode and the coordinate is off screen the mouse will get stuck at the edge of your physical screen. Also keep in mind that the user is able to fight the adjustment.