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
1
Question by jimmyismike · Nov 06, 2011 at 10:32 PM · shootingshootshooter

Shoot Script Help

Recently I have been using a shoot script, but it has not been working out. Can someone tell me how to make a script where you shoot a prefab, but i want it so it shoots forward and never falls low or falls to the side. It just goes straight forward and only stops when it hits a collider of any kind.

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

1 Reply

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

Answer by Edyvargas · Nov 06, 2011 at 11:32 PM

Hi, you can use the ShootProyectile.js script attached to a non rendered gameobject placed has a child of your weapon.

Recomended:

To avoid the missing collisions of the bullet at fast speeds, use this script attached to the bullet prefab.


For the shoot, create a new Javascript object, clear its default content and copy the code above, then name it "ShootProyectile" or wathever you want, your bullet prefab must have Rigidbody, then attach the code to your not rendered gameobject (this object will be the point of shoot), placed as a child of your weapon.

If you want the bullet to go straight, just disable "Gravity" on the Rigidboy of the bullet, at the inspector:

NOTE: I think is better to use Raycast for general shooting, and just for granades and/or misils you can use the ShootProyectile script.

Also if you will use the DontGoThroughThings.js (recomended to hit colliders at fast speeds with a fisic bullet) script on your bullets, go to Edit, Physics, and disable "Raycasts Hit Triggers", to avoid the bullets collide on triggers, or if you want to keep this option able, use a specific layer only for triggers, then unset this layer on the layermask variable of the DontGoThroughThings.js script.

ShootProyectile.js Script above:

var shotsound:AudioClip;
var shellPrefab : GameObject;
var proyectilevelocity:int;
var ammo:int;
var fireRate = 1.0;
private var nextfire = 0.0;
function FixedUpdate () {
    if(Input.GetButton("Fire1") && Time.time>nextfire && ammo>0)
        ShotProyectile();
}
function ShotProyectile(){
        if(Time.time > nextfire)
        {
    var Grenade:GameObject = Instantiate(shellPrefab,transform.position,transform.rotation);
    Grenade.GetComponentInChildren(Rigidbody).velocity =transform.TransformDirection(Vector3(0,0,proyectilevelocity));
    //Physics.IgnoreCollision(shellPrefab.GetComponentInChildren(BoxCollider).collider,transform.parent.collider);
    ammo--;
    if(shotsound)
        audio.PlayOneShot(shotsound);
    Destroy(Grenade,10);
    nextfire = Time.time+fireRate;}
}

Good Luck!

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 Casper091 · Nov 12, 2012 at 04:03 AM 0
Share

I used your Idea, but what do i do about reloading? and there's only one clip, its a really good script/ tutorial.. but not what i needed

avatar image iAmAwsum · Nov 16, 2013 at 04:06 PM 0
Share

For me the bullet shots on the side of my $$anonymous$$ain Camera on the FP Controller

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

Multiple Cars not working 1 Answer

Hey can anyone help with a shooting script??? 0 Answers

How do you make a collision based fps script that deletes the object 2 seconds after it is fired? 2 Answers

Need help with shooting script 2 Answers

Problem with firing laser in direction of camera 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