Hi,
I’ve been searching and searching and searching forever now.
And i can’t for the love of God figure this out. I’m probably extremely retarded. But please i need to get this to work and i’m about to commit suicide. (not really)
Anyway, here is my problem. I’m making a 2.5D game. This means that the game is 3D, but viewed from the side like a 2D platformer.
What i want to do is have the weapon of my hero always look at a point on a plane with which every object in the gameworld is alligned. This plane acts as a raycast detecter and whenever i want to fire something the bullet should travel from the origin point of the weapon to the point that is created on the plane where my cursor is aiming on the screen.
What i have so far…
//raycast
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 999, Color.yellow);
if (Physics.Raycast(ray, out hit,1000f,mask)) {
//AimIndicator.transform.LookAt(hit.transform.position);
if (Input.GetButton("Fire1")) {
Instantiate(test, hit.transform.position, Quaternion.identity);
}
}
The problem with this code is that whenever i click to instantiate the object (test) it always instantiates it at a certain point. Regardless of where my mousepointer is.
Now, i have debugged the ray to see where it is going and as far as i can see the ray is shoting exactly where my mouse is aiming so everything seems to work perfect according to the debug.
However, this does not seem to be the case as my object does not instantiate on the hit point.
I have no idea why??!!
Can anyone please help me with this madness. I’m about to go insane i think.
I can program almost everything without much effort. But whenever there’s a raycast involved i’m having so much difficulty. Why are raycasts so extremely difficult to understand? Does anyone have like a solid good tutorial so that i can finally wrap my head around this subject?