- Home /
Question by
Gokhan_Zengin · Aug 02, 2019 at 12:33 AM ·
2dmovedrag objects
How can I move 2D object with dragging right mouse button?
I want to move the object while press right-click and mouse cursor is on it. I can do it with left-click with using OnMouseDrag function but it only works with left-click.
void OnMouseOver(){
if (Input.GetMouseButton (1)) {
transform.localPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
transform.localPosition = new Vector3 (transform.position.x, transform.position.y, 1);
}
}
I used this but when I drag fast it stops moving help please.
Comment
Answer by Gokhan_Zengin · Aug 02, 2019 at 12:58 AM
I found the solution by myself. I changed code like this:`
bool holded;
void OnMouseOver(){
if (Input.GetMouseButtonDown (1)) {
holded = true;
}
}
void Update (){
if (holded) {
transform.localPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
transform.localPosition = new Vector3 (transform.position.x, transform.position.y, 1);
if (Input.GetMouseButtonUp (1))
holded = false;
}
}
Answer by JesperGreen · Oct 23, 2020 at 07:18 AM
Does it work with 3D objects?
Would you mind sharing the full script? :)
Your answer
Follow this Question
Related Questions
Move Object With Angle In 2D 1 Answer
newbie 2d simple sprite rotation 1 Answer
2d Platform Controller Face Help 1 Answer
Switching Cameras at runtime 1 Answer