Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by GamezAtWork · Mar 05, 2011 at 06:37 PM · physicsmousegrenade

SHOOT CORRECTLY DAMN GRENADE THING

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:

function Awake(){
rigidbody.AddRelativeForce(Vector3(0,0,1000));

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Quaternion.LookRotation(Camera.main.ScreenPointToRay(theVector).direction)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GamezAtWork · Mar 07, 2011 at 02:57 AM 0
Share

YES! IT WOR$$anonymous$$S! Thanks $$anonymous$$ike!

avatar image
0

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.

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GamezAtWork · Mar 06, 2011 at 05:28 PM 0
Share

THAN$$anonymous$$S! 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?

avatar image MortenK84 · Mar 06, 2011 at 07:16 PM 0
Share

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?

avatar image GamezAtWork · Mar 07, 2011 at 02:57 AM 0
Share

Strange... Looks like $$anonymous$$ike's method works though... Thanks for all the help though $$anonymous$$orten$$anonymous$$! $$anonymous$$uch appreciated!

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Making an object fly at selected item. 0 Answers

Having more than one collider in a GameObject? 8 Answers

How to create normal physics reactions when collision is caused by a GameObject controlled by mouse position 0 Answers

How can my mouse-controlled object interact with Rigidbodies? 0 Answers

Is it possible to create a balance board that follows the mouse? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges