Well... Heres the problem. Since my game is a rail-shooter, I want to make it such that you can throw a grenade forward, from where you click the screen.
So lets say you click the top left area of the screen, a grenade will spawn and bounce forward before exploding.
I managed to get the spawning and the explosion-ing part done quite well. But I have this strangest problem.
NO MATTER WAY, THE GRENADE WILL ALWAYS BOUNCE TOWARDS THE SAME POINT.
Like, for example, lets say the point this time is somewhere in front of me. No matter WHERE I CLICK ON THE SCREEN, the grenade's direction will ALWAYS be facing that point, so all grenades will bounce towards the same place. And I can't possibly have that.
Can someone please tell me whats wrong? Heres the code I used for the spawning:
And heres the code I used for movement:
asked Mar 05, 2011 at 06:37 PM
You're using the same rotation no matter what the position you click is
try something like this for the spawn rotation (untested, but you get the idea):
answered Mar 05, 2011 at 07:30 PM
The problem is in your Instantiate method. Your second parameter is the spawn location, which in your case (using ScreenToWorldPoint) will be whereever you click.
Your third parameter, is the rotation of your instantiated object. You use Camera.main.transform.rotation which returns the cameras rotation. This rotation will always be the same. That's why no matter where you spawn your grenade, it will always head in the same direction (the direction of the camera).
Instead, you probably want to instantiate the grenade at the same position always, but instead have the direction change.
In the above code, the grenade will always be spawned at the cameras location, but the rotation will be different: It will be rotated towards where you clicked on the screen, instead of where the camera is looking. The Z value is set to 1000 in this case. In your specific case you might want to increase or decrease the Z value, depending on the scale of your scene.
answered Mar 05, 2011 at 07:34 PM