- Home /
Mouse Movement
Im trying to make an object move using mouse. Slide to be more specific. I was able to make it slide forward and right, by sliding my mouse in those direction but for some reason "backward" and "down" are not recognized by Unity. Also is it possible to have an angular movement, so when I move my mouse in certain angle, my object will move in same direction.? thanks for any help?
here is the code I used:
function Start () {
} private var v3Pos : Vector3; private var threshold = 9;
function OnMouseDown() { v3Pos = Input.mousePosition; }
function OnMouseDrag() { var v3 = Input.mousePosition - v3Pos; v3.Normalize(); var f = Vector3.Dot(v3, Vector3.up); if (Vector3.Distance(v3Pos, Input.mousePosition) < threshold) { Debug.Log("movement"); return; }
if (f >= 0.5) {
Debug.Log("forward");
rigidbody.AddForce(-transform.forward *500);
rigidbody.useGravity = true;
}
else if (f <= -0.5) {
Debug.Log("backward");
rigidbody.AddForce(-transform.backward *500);
rigidbody.useGravity = false;
}
else {
f = Vector3.Dot(v3, Vector3.right);
if (f >= 0.5) {
Debug.Log("Right");
rigidbody.AddForce(-transform.right *500);
rigidbody.useGravity = true;
}
else {
Debug.Log("down");
rigidbody.AddForce(-transform.right *500);
rigidbody.useGravity = false;
}
}
}
Answer by Canazza · Aug 20, 2013 at 11:19 AM
Transforms only have Forwards, Up and Right, you can get 'backwards' , 'down' and 'left' and by multiplying their respective opposites by -1
In your case, you can just remove the minus sign
Debug.Log("backward");
rigidbody.AddForce(transform.forward* 500);
rigidbody.useGravity = false;
Answer by dekilana · Aug 20, 2013 at 11:38 AM
that worked like a charm, thank you very very much. Is there a way of making it move on angle instead of only up/down and left/right. thanks for taking your time to help me
Your answer
Follow this Question
Related Questions
Ball Doesn't Quite Roll Right 1 Answer
Sliding and gravity. 1 Answer
Sider Joint 2D in 3D 0 Answers
How to make a gameobject be exactly where the mouse is. 2 Answers
Is there a function for OnCollision, that detects drag? 0 Answers