- Home /
Getting (Input) values before running rest of script.
Hi! I am trying to create something like a "drag the direction to shoot". I have this to get Input coords:
void Update ()
{
Vector3 Point1;
Vector3 Point2;
Vector3 Point3;
if (Input.GetKeyDown("space"))
{
Point1 = Camera.main.ScreenToWorldPoint( new Vector3(Input.mousePosition.x, Input.mousePosition.y,10));
}
if (Input.GetKeyUp("space"))
{
Point2 = Camera.main.ScreenToWorldPoint( new Vector3(Input.mousePosition.x, Input.mousePosition.y,10));
}
if (Point1.z ==10 || Point2.z ==10)
{
Point3.y = 20;
if (Point1.x == Point2.x)
{
Point3.x = Point2.x;
}
else
{
float mouse_grad =(Point1.y-Point2.y)/(Point1.x-Point2.x);
Point3.x=((Point3.y-Point2.y)+(mouse_grad*Point2.x))/mouse_grad;
}
transform.position = new Vector3(Point3.x, Point3.y, 10);
}
}
Although logically this SHOULD work, Unity won`t let me use it because of use of possibly unassigned values (After the third if). Maybe anyone of you knows how to bypass this with a different loop?
I know this is actually a logic problem, not scripting, but I have been thinking about it since the last evening, and I can not seem to figure out a solution for it to work. (Meanwhile I am a a noob, and do not have a long experience with scripting, so if the solution is ridiculousl easy please at least point me in the direction guys :) )
Have a great day!
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613062955im_/https://answers.unity.com/themes/thub/images/avi.jpg)