• 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 Norbert1234 · Jan 04, 2013 at 12:30 AM · gamefirst-person-controller

Can somebody help me and tell me what is wrong with my script on a first person shooter game ?

Hi! I just started to use Unity three months ago and right now I am doing a first person shooter game. I have a major problem with the script which shoots the bullets from the sniper. My script is the following so far :

var Bullet : Rigidbody;

var cannon : Transform;

var initialSpeed = 100.0;

var reloadTime = 0.5;

var ammoCount = 20;

private var lastShot = -10.0;

function Fire () {

 if (Time.time > reloadTime + lastShot && ammoCount > 0) {

     var instantiatedBullet : Rigidbody = Instantiate(Bullet,cannon.transform.position, cannon.transform.rotation );

 instantiatedBullet.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));

     // Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
     
     lastShot = Time.time;
     ammoCount--;
 }

}

  • Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider); is commented out because I want collision between the bullets and the characters.

My problem is that I can not shoot straight forward... my bullets are going 90 degrees to left, so right now I am shooting to my left, not forward, also my bullets are not faced forward , when I shoot them, there are exactly standing, there are vertical, not horizontal, I rotated the prefab, but nothing good happened.

Somebody could help me how to code the bullets for shooting them forward, not to the left, also how to but them in a horizontal position with codes, because rotating or changing the angles of the prefab could not resolve the problem.

Comment
Add comment · Show 3
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 Lovrenc · Jan 04, 2013 at 12:35 AM 0
Share

I guess your characters axes are messed up. Is the Z axis of your FPS character looking left?

Never the less. We usualy dont actially make bullets in fps games. They are 2 fast to collide anyways. You should use raycast and just display animations/effects on hit.

avatar image Norbert1234 · Jan 04, 2013 at 12:54 AM 0
Share

Hi ! Z-axis is looking forward.

I never used raycast. How I could use that ?

avatar image FL · Jan 06, 2013 at 06:26 PM 0
Share

I guess that you imported sniper are facing left in 0 degrees position.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Hybris · Jan 06, 2013 at 09:23 PM

The problem probably is the local axis of your object which has the script attached. Although this seems weird, I would try to use transform.forward instead of Vector3.forward. So:

 instantiatedBullet.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));

this would become:

 instantiatedBullet.velocity = transform.TransformDirection(transform.forward * initialSpeed));

please note that:

 instantiatedBullet.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));

is the same as:

 instantiatedBullet.velocity = transform.TransformDirection(Vector3.forward * initialSpeed);
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.

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

11 People are following this question.

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

Related Questions

getting udp package info inside unity (GlovePIE) 0 Answers

Some suggestions First Game 2 Answers

How to spawn a player in multiplayer on Android with Photon? 1 Answer

Game is not using First Person Controller? 1 Answer

FPS teleport flicker one frame- but works fine with main camera. 2 Answers


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