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:
var theVector = Input.mousePosition;
theVector.z = 1;
var thrownItem = Instantiate(fullWeaponsList[currentWeaponNum].thrownObject, Camera.main.ScreenToWorldPoint(theVector),Camera.main.transform.rotation);
And heres the code I used for movement:
Answer by Mike 3
Mar 05, 2011 at 07:30 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):
YES! IT WORKS! Thanks Mike!
Answer by MortenK84
Mar 05, 2011 at 07:34 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.
var theVector = Input.mousePosition;
theVector.z = 1000;
var thrown = Instantiate(prefab, Camera.main.transform.position,Quaternion.LookRotation(Camera.main.ScreenToWorldPoint(theVector)));
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.
THANKS! Its almost there! The grenades can now land on different spots i believe... Although... Now the issue is that it shoots kinda weirdly... Hard to describe, but basically when I shoot from the complete left side of the screen, it will head towards the complete right, and if I fire from the complete right, it'll head towards the centre...
I used the code that you provided... Is there anything that I could have forgotten to do?
That's odd. I've tested the code and it works fine enough for me. Can you try and make either a screencast (using jingproject.com, camtasia studio or similar) or take some screenshots? Also, what is your main cameras rotation?
Strange... Looks like Mike's method works though... Thanks for all the help though MortenK! Much appreciated!
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
No one has followed this question yet.
How should I control a 'rake' that pushes objects around a table?
Roll forward mouse
How can I manipulate an object with the mouse?
Tray and cup. How to?
Having more than one collider in a GameObject?