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.


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:

function Awake(){


more ▼

asked Mar 05, 2011 at 06:37 PM

avatar image

263 83 62 69

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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):

more ▼

answered Mar 05, 2011 at 07:30 PM

avatar image

Mike 3
34.6k 28 156 319

YES! IT WORKS! Thanks Mike!

Mar 07, 2011 at 02:57 AM GamezAtWork
(comments are locked)
10|3000 characters needed characters left

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.

Try this:

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.

more ▼

answered Mar 05, 2011 at 07:34 PM

avatar image

63 4 3 10

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?

Mar 06, 2011 at 05:28 PM GamezAtWork

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?

Mar 06, 2011 at 07:16 PM MortenK84

Strange... Looks like Mike's method works though... Thanks for all the help though MortenK! Much appreciated!

Mar 07, 2011 at 02:57 AM GamezAtWork
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Mar 05, 2011 at 06:37 PM

Seen: 1282 times

Last Updated: Mar 05, 2011 at 06:37 PM