Hi guys, for me (being a noob) this is the hardest nut to crack I have encountered so far, but I really hope maybe this would seem ridiculously easy for someone of you.
I am trying to calculate a position for an object to appear based on my mouse clicks. In this picture Imgur: The magic of the Internet “C” is where I click mouse and “D” is where I release the button. I want an object to appear on the same line as these points at the top of the screen (Point “A”).
I have laid down the equation, that I would need to calculate the “x” value of the point, but I can not figure out how to do it in script. Can mousePosition.x be used as a float and then back as a position for object or are there some another way?
I tried this kind of solution:
void Update () {
Vector3 C = Input.mousePosition;
Vector3 D = Input.mousePosition;
float Width = Camera.main.pixelWidth;
float BF = Camera.main.pixelHeight;
Vector3 Cam = new Vector3(Width,BF,0);
float BC;
float CE;
float ED;
Vector3 A;
float AB;
BC = BF - C.y;
CE = C.y - D.y;
ED = D.x - C.x;
AB = BC / CE * ED;
A = new Vector3((C.x - AB),BF,0);
transform.Translate((C.x - AB),BF,0);
}
Now I am getting “transform.position assign attempt for ‘Cube’ is not valid. Input position is { NaN, NaN, NaN }.” I guess it is because of what @syclamoth said - I have not converted 2d coords to 3d world? Would just adding “Camera.main.ScreenToWorldPoint” for certain Vectors be a reasonable solutions? ( I have never done this, so I do not really understand what is happening)