Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by LABoy · Aug 21, 2015 at 07:02 PM · rigidbodyinstantiatevector3velocityshooting

Cant Get a Bullet to Shoot (C#)

So I'm making a third person shooter and right now I'm working on a simple enemy ai. So far the enemies look at you and shoot, but the bullets don't go anywhere. They spawn and drop to the ground. I'm using a piece of code I used when making the player shooting script, and I have all the right variables in the enemy script, but it's not working like it is in the player shooting script. The bullets just drop like I said. Here's the script: using UnityEngine; using System.Collections;

 public class EnemyScript : MonoBehaviour {
 
     public bool agroed;
     public Transform player;
     public Rigidbody bullet;
     public float force;
     public Transform bulletSpawn;
     public float rateOfFire;
     public float waitShootTime;
 
     // Use this for initialization
     void Start () {
         agroed = false;
     }
     
     // Update is called once per frame
     void Update () {
 
         waitShootTime -= Time.deltaTime;
         if(waitShootTime <= 0)
             waitShootTime = 0;
         if(Input.GetButtonDown("Play"))
         {
             agroed = true;
         }
     
         if(agroed == true)
         {
             transform.LookAt(player);
             if(waitShootTime <= 0)
             {
 
                 waitShootTime = rateOfFire;
                 Shoot();
 
             }
         }
     }
 
     void Shoot()
     {
         Rigidbody clone;
         clone = Instantiate(bullet, bulletSpawn.transform.position, bulletSpawn.transform.rotation) as Rigidbody;
         clone.velocity = bulletSpawn.transform.TransformDirection(Vector3.forward * force);
     }
 }
 

can any of you find out why the bullet is ignoring velocity? All variables are assigned. Thanks!

Comment
Add comment · Show 1
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 j0ffe · Aug 21, 2015 at 11:20 PM 0
Share

$$anonymous$$ight sound like dump question, have you check so unity doesn't set force to 0?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by DiegoSLTS · Aug 30, 2015 at 05:19 PM

In this line:

 clone.velocity = bulletSpawn.transform.TransformDirection(Vector3.forward * force);

Are you sure you want "Vector3.forward" there? Vector3.forward is a global direction, it's (0,0,1) in world space. The bullet will start with a velocity up in the Z axis and gravity will start pulling it down. Maybe you want to use transform.forward, which is the local forward direction of the enemy, the direction it's facing.

EDIT: Looking closer, maybe you want this:

 clone.velocity = transform.forward * force;

or:

 clone.velocity = bulletSpawn.transform.forward * force;
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 Seneral · Aug 21, 2015 at 07:57 PM

Use clone.AddForce instead off setting the velocity:)

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 LABoy · Aug 21, 2015 at 11:00 PM 0
Share

Thanks for the suggestion. Sadly it didn't work. I appreciate you taking the time to help though!

avatar image Seneral · Aug 30, 2015 at 01:09 PM 0
Share

@LABoy Just revisiting, actual problem is you are instantiating a Rigidbody which is a component, not a gameObject. Add it to a new GameObject prefab with a mesh and renderer + rigidbody.

avatar image DiegoSLTS · Aug 30, 2015 at 05:11 PM 0
Share

@Seneral, what he's doing is O$$anonymous$$, Instantiate will clone the game object that has the Rigidbody component. That's why there's a version of Instatiate that receives an Object and needs a cast later, and a generics version that let's you specify the object type in code. You can even see in the docs examples using a Transform and a Rigidbody.

http://docs.unity3d.com/ScriptReference/Object.Instantiate.html

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Set the Velocity to reach a position at a specific Timestamp 0 Answers

Check/Change velocity of an object 0 Answers

Rigidbody.velocity Movement code produce wildly different gravity interactions 0 Answers

Slow down a character while they are midair while keeping their original velocity 0 Answers

Please help me figure this out 0 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