Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Sandr0G · Jul 23, 2012 at 08:13 PM · shootinggunshoot

Shooting script not working properly

I'm using this script to shoot:

 var bulletPrefab:Transform;
 
 function Update () 
 {
 
  if(Input.GetButtonDown("Fire1"))
 
    {
      
      //Create bullet
      var bullet = Instantiate(bulletPrefab, GameObject.Find("spawnPoint").transform.position, Quaternion.identity);
   
   //Direction of bullet and add force
   bullet.rigidbody.AddForce(transform.forward * 5000);   
 
    }
 }

But it does not shoot properly. When I turn right it does not shoot, it just creates the bullet in front of the gun. Also when I shoot at certain angles it does not shoot to where I am aiming it shoots to the left. Besides this it works fine. What am I doing wrong?

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

3 Replies

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

Answer by Seth-Bergman · Jul 23, 2012 at 08:19 PM

You need to change "Quaternion.identity", which aligns the rotation to the world axis, to "transform.rotation" or "spawnPoint.transform.rotation"

and maybe change "transform.forward" to "bullet.transform.forward"

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 Sandr0G · Jul 23, 2012 at 08:24 PM 0
Share

Thank you, it worked!

avatar image
0

Answer by Ingen · Jul 23, 2012 at 08:20 PM

hi!

the spawnPoint is a bit forward the gun and is a child of maincamera?

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 Hassam1957234 · Jan 19, 2013 at 05:23 PM

Hi i was using this script var bulletPrefab:Transform;

function Update () {

if(Input.GetButtonDown("Fire1"))

{

  //Create bullet
  var bullet = Instantiate(bulletPrefab, GameObject.Find("spawnPoint").transform.position, transform.rotation);

//Direction of bullet and add force bullet.rigidbody.AddForce(bullet.transform.forward * 5000);

} } first of all i cant drag a bullet prefab to inspector of my rifle and it wont shoot

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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Shooting & Animation Problem 1 Answer

not losing ammo when shooting 1 Answer

Problems with a shooting script 1 Answer

How to make a gun in unity? 1 Answer

1 script doesn't react to input 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