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 AusieJamster · Oct 22, 2013 at 04:03 AM · c#shootingshootbasic

C# Basic Shooting Script

Im making a 2d platformer with basic object throwing but when the object is shot it just falls to the ground like the AddForce isn't working on it. Plz help

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(CharacterController))]
 
 public class Movement : MonoBehaviour
 {
     public float speed = 6.0f;
     public float bulletSpeed = 1000.0f;
     public float jumpSpeed = 8.0f;
     public float gravity = 20.0f;
     public float shotInterval = 0.5f;
     public Rigidbody bulletPrefab;
     
     private float shootTime = 0.0f;
     private Vector3 moveDirection = Vector3.zero;
     private bool grounded = false;
     private Transform bulletSpawn;
     
     void Start(){
         bulletSpawn = transform.Find ("bulletSpawn");    
     }
     void FixedUpdate() 
     {
         if (grounded) 
 
         {
             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
 
             if (Input.GetButton ("Jump"))
                 moveDirection.y = jumpSpeed;
         }
         
         moveDirection.y -= gravity * Time.deltaTime;
         
         CharacterController controller = (CharacterController)GetComponent(typeof(CharacterController));
         CollisionFlags flags = controller.Move(moveDirection * Time.deltaTime);
         grounded = (flags & CollisionFlags.CollidedBelow) != 0;
         
         if (Input.GetButton ("Fire1"))
         {
             if (Time.time >= shootTime)
             { 
                 Rigidbody bullet = Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation) as Rigidbody;
                 bullet.rigidbody.AddForce(transform.forward * bulletSpeed);
                 shootTime = Time.time + shotInterval;
             }
         }
     }
 }
Comment
Add comment · Show 4
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 Benproductions1 · Oct 22, 2013 at 04:04 AM 0
Share

I don't think rigidbody.rigidbody should even compile?!?!?

avatar image AusieJamster · Oct 22, 2013 at 04:06 AM 0
Share

You mean bullet.rigidbody? so i removed the rigidbody part of it, but it does the same thing

avatar image Benproductions1 · Oct 22, 2013 at 04:15 AM 0
Share

Is there anything else that influences the bullet?

avatar image AusieJamster · Oct 22, 2013 at 04:19 AM 0
Share

Only one other but it wouldn't affect anything

 using UnityEngine;
 using System.Collections;
 
 public class Destroy : $$anonymous$$onoBehaviour {
 
     void Start () {
         Destroy(transform.gameObject, 5);
     }
 
 }
 

2 Replies

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

Answer by AusieJamster · Oct 22, 2013 at 04:40 AM

I found that it was shooting into the map and therefore appearing to just fall when it was shooting straight sorry to waste your time :(

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 SquidSquad · Oct 22, 2013 at 04:21 AM

Remove gravity from rigidbody, also if the bullet Spawn prefab has Vector3.forward as its y axis. Quaternion.identity should fix that, but if it doesn't manually create vector3 to change the euler angles(in instantiation). also since you instintiate as a prefab gravity is auto on. call buletspawn.gravity.enabled = false imediatley after instantiation. hope that helps

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 AusieJamster · Oct 22, 2013 at 04:28 AM 0
Share

I'm trying to make it more of a grenade movement (as he is throwing an object) then a bullet i called it bullet because I'm stupid. I found that it was shooting into the map and therefore appearing to just fall when it was shooting straight sorry to waste your time :(

avatar image Benproductions1 · Oct 22, 2013 at 04:32 AM 0
Share

If the question is answered you should either close it or post a answer yourself and accept that answer. If you don't know how, watch the tutorial video on the right :)

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

17 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to Sync bullet spawn with shoot animation 0 Answers

Bullet not shooting enemy center in unity 1 Answer

Why does the wrong enemy die with this raycast? C# 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