- Home /
Controlling object movement with mouse
Hi,
I am making a script that allows users to control the position of an object with the mouse. I don't want the object to actually be rendered at the position of the mouse, but I want its movement to be relative to that of the mouse.
I am using this code to try to achieve this:
var delta : Vector3 = Vector3.zero; var lastPos : Vector3 = Vector3.zero; var cam : Camera;
function Update() { delta = cam.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, 125)) - lastPos; transform.position += delta; lastPos = cam.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, 125)); }
If this worked right, the position of the object would be calculated on the difference between the last mouse position and the current mouse position, thus making the object's movement relative to the cursor, but not actually be placed at the position of the cursor.
BUT, for some reason all it's doing is being placed at the cursor's position!
Why?!... And how can I fix this?
Thanks in advance! :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613082323im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Calculating change in mouse position 2 Answers
Camera.main.ScreenToWorldPoint not working 0 Answers
Converting Input.mousePosition to world coordinates 1 Answer
World point for MousePosition 1 Answer
Creating Projectile - MoveTo/Lerp 0 Answers