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 Sillan · Mar 17, 2013 at 09:09 PM · raycastprefabshoot

Shoot Script

Does anyone know how to write a script that will let you shoot? I already have one but am looking for one that doesn't transform the prefab "forward" as it won't work if the player looks up. Thankyou so much in advance.

Comment
Add comment · Show 7
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 serenefox · Mar 17, 2013 at 09:26 PM 0
Share

What does your script look like? Im not really that great with scripting but when I use "Instantiate" I Instantiate a projectile at a position from a Fire Point and then on the projectile mesh I apply a "constant force" and type in a number in the relative force parameter. That way the projectile will always "fly" in the relative direction form where it was fired.

avatar image Sillan · Mar 17, 2013 at 09:38 PM 0
Share

Hi serenefox, thanks for replying. This is my current script (it's for a mobile platform) - var prefabBullet:Transform; var shootForce:float;

 function Update()
 {
     if (Input.touchCount == 1)
         {
         var touch: Touch = Input.touches[0]; 
  
         if(touch.phase == TouchPhase.Began && guiTexture.HitTest(touch.position))
         {
             var instanceBullet = Instantiate(prefabBullet, GameObject.Find("shootspawn").transform.position, GameObject.Find("$$anonymous$$ain Camera").transform.rotation);
             instanceBullet.rigidbody.AddForce(GameObject.Find("Player").transform.forward * shootForce);
                      }
    }
 }

When i shoot by pressing the button it only does it forward according to the player and when i rotate the camera up it doesn't shoot up. I'm not that good at coding either :D. Thanks again, Johnny

avatar image serenefox · Mar 17, 2013 at 10:16 PM 0
Share

Ok I haven't really done anything mobile yet but it should be the same concept I believe(and anyone can correct me if Im wrong). here is a script I did for something I am working on:

 var projectilePrefab : GameObject;
 var fp1 : GameObject; 
 var fireRate : float = 1;
 var nextFire : float = 1;
 
 function Update ()
  {
 
         if( Input.GetButton("Fire1") && Time.time > nextFire)
         {
             nextFire = Time.time + fireRate;
             Instantiate(projectilePrefab, fp1.transform.position, fp1.transform.rotation);
             
         }
 }

This would go on my characters gun and in the scene I would create an empty gameobject that would be a child under the gun. The empty gameObject would be placed at the end of my gun mesh(so where the bullet would come out). In the Inspector on the gun I would assign all the correct parts to the variable slots(for example the projectile mesh to the "projectilePrefab" and so on). Then on the projectile mesh I would go to Component>Phsyics>Rigidbody to add a rigidbody(if you want to you can mess with the rigidbody's parameters) because you need a rigidBody to use a constant force. Then go to Component>Phsyics>ConstantForce (then mess with the "relative force" parameter until when you fire your projectile it goes straight out from the gun). The empty gameobject's (the firepoint's) axis is what direction the projectile will go off of so look at the way it is facing for the right axis to change in the constant force.

But that that extent of my knowledge on this. Hopefully you will be able to look at my script example and be able to change yours so it works properly.

avatar image serenefox · Mar 17, 2013 at 10:18 PM 0
Share

The Fire Rate and Next fire parts of my script are there to space out the shots so its not rapid fire constantly when you press fire.

avatar image Sillan · Mar 17, 2013 at 10:38 PM 0
Share

Thankyou so so much. It was exactly what I needed. I really appreciate you Help :D

Show more comments

1 Reply

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

Answer by serenefox · Mar 17, 2013 at 10:40 PM

 var projectilePrefab : GameObject;
 var fp1 : GameObject; 
 var fireRate : float = 1;
 var nextFire : float = 1;
 
 function Update ()
  {
 
        if( Input.GetButton("Fire1") && Time.time > nextFire)
        {
          nextFire = Time.time + fireRate;
          Instantiate(projectilePrefab, fp1.transform.position, fp1.transform.rotation);
 
        }
 }


This would go on my characters gun and in the scene I would create an empty gameobject that would be a child under the gun. The empty gameObject would be placed at the end of my gun mesh(so where the bullet would come out). In the Inspector on the gun I would assign all the correct parts to the variable slots(for example the projectile mesh to the "projectilePrefab" and so on). Then on the projectile mesh I would go to Component>Phsyics>Rigidbody to add a rigidbody(if you want to you can mess with the rigidbody's parameters) because you need a rigidBody to use a constant force. Then go to Component>Phsyics>ConstantForce (then mess with the "relative force" parameter until when you fire your projectile it goes straight out from the gun). The empty gameobject's (the firepoint's) axis is what direction the projectile will go off of so look at the way it is facing for the right axis to change in the constant force.

But that that extent of my knowledge on this. Hopefully you will be able to look at my script example and be able to change yours so it works properly.

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

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

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

How to make mobile controls shoot prefab bullets 0 Answers

Problems with Raycast 0 Answers

How do I instantiate and shoot a ball along a ray cast? [ANSWERED] 1 Answer

Custom Collision Detection 4 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