- Home /
Question by
mrblinder · Feb 24, 2014 at 09:36 PM ·
2d-gameplaygetmousebuttondown
Input.mousePosition changing to (0,0) after GetMouseButtonUp
I'm trying to make my Player be able to throw a bomb with a vector which is directly determined by the mouse positions at GetMouseButtonDown and GetMouseButtonUp. The code is as follows (all in 2D, btw):
#pragma strict
public var newBomb:Rigidbody2D;
public var ThrowFactor:float = 1;
public var destructTime:float = 3;
function Start () {
}
function Update ()
{
if(Input.GetMouseButtonDown(0))
{
var start_pos:Vector2 = Input.mousePosition;
Debug.Log("start1 = "+start_pos);
}
if (Input.GetMouseButtonUp(0))
{
var end_pos:Vector2 = Input.mousePosition;
var throwDirection:Vector2 = ThrowFactor*(end_pos-start_pos);
Debug.Log("start2 = "+start_pos);
Debug.Log("end = "+end_pos);
Debug.Log("direction = "+throwDirection);
var new_pos : Vector3 = transform.position+throwDirection.normalized;
var new_rot : Quaternion = Quaternion(0,0,0,0);
var newBombInstance : Rigidbody2D;
newBombInstance = Instantiate(newBomb, new_pos, new_rot);
newBombInstance.gravityScale =10;
newBombInstance.velocity = throwDirection;
}
}
For some reason, the debug.log always shows (start2 = 0.0,0.0) even when start1 is not. Any ideas?
Comment
Best Answer
Answer by highpockets · Feb 24, 2014 at 09:53 PM
Because it is a local variable inside the mouse down conditional. Declare it outside of your functions and it should be fine
Your answer
![](https://koobas.hobune.stream/wayback/20220613134617im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
physics for 2D game 1 Answer
NavMesh and 2D top-down games 0 Answers
How can i move a 2D ball ? 0 Answers
How to realize FreeRiderHD-like game mechanic? 0 Answers
Minimap with mutlple scenes 0 Answers