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 uberokeer · Mar 15, 2013 at 03:25 PM · shooting

Shooting at ground

For some reason I can't shoot normally I just shoot at the ground. And I don't know why.

Here's my shooting script. I just attach it as a component to my character : Mainplayer

Code:

 var ammo : int = 220;
 var rocketPrefab: Transform; // your rocket prefab
 var rocketSpeed: float = 20; // rocket speed
 var spawnPoint: Transform; // empty object which defines the spawn point
 var rateFire : float = 0.5;
 var access : boolean = true;
 
 private var canShoot : boolean;
 
 function Start(){
 canShoot = true;
 }
 function Update () {
 ToFire();
 if (ammo < 1){
 canShoot = false;
 }
 }
 function OnGUI(){
 GUI.Label(Rect(15,15,200,100),ammo.ToString());
 }
 function ToFire(){
 if (Input.GetButton("Fire1")){
    if (access){
       access = false;
       //here all the events to fire the gun //START NOW!
       if (canShoot){
   if (Input.GetButton("Fire1")){
   animation.Play("fps_attack_1");
   ammo -= 1;
     var ray: Ray = Camera.main.ViewportPointToRay(Vector3(0.5,0.5,0));
     var hit: RaycastHit;
     var dir: Vector3;
     if (Physics.Raycast(ray, hit)){
       dir = (hit.point - spawnPoint.position).normalized;}
     else{
       dir = ray.direction;}
     var rot = Quaternion.FromToRotation(rocketPrefab.forward, dir);
     var rocket = Instantiate(rocketPrefab, spawnPoint.position, rot);
     rocket.rigidbody.velocity = dir * rocketSpeed * 5;
     WaitForSeconds(1);
 
  }}
       //END NOW!
       yield WaitForSeconds(rateFire);
       access = true;
    }
    }
    }
    function OnCollisionEnter(col : Collision){
    if(col.gameObject.tag == "AmmoPack"){
    ammo +=15;
    Destroy (GameObject.Find("AmmoPack"));
    }
    }


Thanks in advance!

EDIT:

I don't get any errors either.

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
2
Best Answer

Answer by Lockstep · Mar 15, 2013 at 04:00 PM

You calculate the rotation of your rocket in a pretty strange way. It is way better to make sure your spawnPoint points in the direction you want to shoot and then set your rocket accordingly:

 var rocket = Instantiate(rocketPrefab, spawnPoint.position, spawnPoint.rotation);
 rocket.rigidbody.velocity = spawnPoint.forward * rocketSpeed; // what is the 5 for??

Also you check for Input.GetButton("Fire1") twice which is redundant. You can remove the second statement to keep your code clean. Furthermore your OnCollisionEnter is wrong too. You use GameObject.Find to destroy your ammopack. But GameObject.Find will return the first gameObject with name AmmoPack in your scene, which is not neccessarily the one you picked up. Use

 Destroy(col.gameObject);

instead. This will destroy exactly the ammoPack you picked up.

Comment
Add comment · Show 3 · 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 uberokeer · Mar 15, 2013 at 04:12 PM 0
Share

:/ it's still doing it... Thanks though for the script.

avatar image Lockstep · Mar 15, 2013 at 05:15 PM 1
Share

I just copied your script and it worked just fine. I only commented the animation out since I don't have it.

Is the shotpoint a child of your character? Naturally you want the shotpoint to move with your character.

Have you messed with the gravity? If the gravity is too high you won't notice the velocity you give your rocket in your script and the rocket will just drop. Test this by unchecking the 'use gravity' checkbox of the rocket prefab.

avatar image uberokeer · Mar 15, 2013 at 06:10 PM 0
Share

Oh wow.... I feel incredibly stupid now... I just realized that i put the spawnpoint of the missle inside of my character collider XD...Wow. hahah thanks for Helping though! :D

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

Multiple Cars not working 1 Answer

Photon Networking Prefab help 0 Answers

C# scriping help 0 Answers

Can you help me find the script error please? 1 Answer

Not working gui buttons 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