• 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 Manny · Jan 31, 2010 at 10:12 PM · 2d

How to shoot in different directions with different keys?

Well i made a Mario-esque game, where i can run, jump and shoot. Well the problem here is the shooting part. When i shoot, i either shoot forward, or out into 3D space. For the shooting script i got:

var Char_Bullet : Transform; var power = 2000.0;

function Update () { if(Input.GetKey("k")) { var bullet = Instantiate(Char_Bullet, GameObject.Find("Gun_Spawn").transform.position, Quaternion.identity); bullet.rigidbody.AddForce(transform.forward * power); } }

What i want to do is, have 'J' and 'L' be the direction i fire in, while 'K' is the fire button. This script only shoots with 'K' and that is, like i said, into 3D space. Any help would be appreciated.

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 Jaap Kreijkamp · Jan 31, 2010 at 11:08 PM

It depends a bit on how your world is oriented along the axis, but basically change the transform.forward to transform.right (and for other way negate power) and you probably are shooting in the right direction.

Okay, without doing anything smart the code would end up something like:

var Char_Bullet : Transform; var power = 2000.0;

function Update () { if(Input.GetKey("j")) { var bullet = Instantiate(Char_Bullet, GameObject.Find("Gun_Spawn").transform.position, Quaternion.identity); bullet.rigidbody.AddForce(transform.right -power); } else if(Input.GetKey("l")) { var bullet = Instantiate(Char_Bullet, GameObject.Find("Gun_Spawn").transform.position, Quaternion.identity); bullet.rigidbody.AddForce(transform.right power); } }

Comment
Add comment · 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
0

Answer by xToxicInferno · Jan 31, 2010 at 11:20 PM

That helps to shoot to the right. This still leaves shooting to the left not working. Could you please explain how to negate the power? Because simply making it less or even a negative, it just randomly sprays the bullets.

Comment
Add comment · Show 2 · 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 Jaap Kreijkamp · Jan 31, 2010 at 11:38 PM 0
Share

add a $$anonymous$$us sign before the word power :-)

avatar image xToxicInferno · Feb 01, 2010 at 12:05 AM 0
Share

The negative sign made it so they sprayed the right way, but the problem was my character was in between the spawn point, and where they wanted to go...so i simply made a 'Gun_Spawn1' on the opposite side. Thanks!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Equivelant of GUI.DrawSprite() ? 0 Answers

How to tell if two objects are right next to each other in a 2d game? 2 Answers

Making the enemy always face the player 1 Answer

No movement when animation plays (2d) 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