Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 jdzach2000 · May 17, 2016 at 03:37 PM · unity 5javascriptjavascripting

Gun shooting help

I am creating a game where you are the starting as the unity jet object. There will be other planes that are enemies that are going to follow the player and shoot him. Right now I am trying to make the player shoot. Right now it instantiaites a yellow cube (tracer fire) and then the cube just falls. It goes forward but not nearly as fast as i want it to be. Also I don't want it to drop, heres my code var Bullet : GameObject; function Update () { %|-829671820_1|% %|52666472_2|% %|1809783453_3|% %|-1054893234_4|% %|1058131029_5|% %|-490803135_6|% // object's Z axis %|-112405093_8|% %|-1590855618_9|% }

Comment
Add comment · Show 2
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 JoshDangIt · May 17, 2016 at 03:45 PM 0
Share

Can you repost your code so I can better read it?

Also, you can get an object's z axis a lot more easily with transform.forward if that's what you're trying to do.

avatar image jdzach2000 JoshDangIt · May 17, 2016 at 09:23 PM 0
Share
  var Bullet : GameObject;
  function Update () {
      // Ctrl was pressed, launch a projectile
      if (Input.GetButton("Fire2")) {
          // Instantiate the projectile at the position and rotation of this transform
          var clone : GameObject;
          clone = Instantiate(Bullet, transform.position, transform.rotation);
  
          // Give the cloned object an initial velocity along the current 
          // object's Z axis
          clone.velocity = transform.TransformDirection (Vector3.forward);
      }
  }
 

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by phxvyper · May 17, 2016 at 09:29 PM

Ensure that the Bullet gameobject prefab doesn't have any gravity-applying components such as a Rigidbody. If it does, then disable gravity on that prefab in the inspector:

alt text

Also, ensure that you're using the correct forward component:

Change

 clone.velocity = transform.TransformDirection (Vector3.forward);

to

 clone.velocity = transform.TransformDirection (transform.forward);

and that SHOULD fix the issue.


ildl2uz.png (31.3 kB)
Comment
Add comment · Show 1 · 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 jdzach2000 · May 18, 2016 at 03:41 AM 0
Share

i dont have any rigid bodies, and yet they still fall. also it seems like the speed at which they go forward is VERY slow. i would like to have an editable speed variable

avatar image
1

Answer by daleth90 · May 18, 2016 at 05:31 AM

First, as @phxvyper said, add a RigidBody to your bullet and cancel UseGravity. Your script should have encountered error since you can only control the velocity through RigidBody.

Second, you should check what the value of

 transform.TransformDirection (Vector3.forward)

is. Actually, it's (0, 0, 1). So small, right? This is why your bullet move slowly.

OK, so here's the script that should work.

 [SerializeField]
 private Rigidbody cube;
 [SerializeField]
 private float speed = 100f;
 
 private void Update () {
     if ( Input.GetKeyDown( KeyCode.Space ) ) {
         Rigidbody clone = Instantiate( cube );
 
         cube.transform.localPosition = Vector3.zero;
         cube.velocity = cube.transform.forward * speed;
     }
 }


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 jdzach2000 · May 19, 2016 at 05:56 PM 0
Share

7 errors from this code

  [SerializeField]
  private Rigidbody cube;
  [SerializeField]
  private float speed = 100f;
  
  private void Update () {
      if ( Input.Get$$anonymous$$eyDown( $$anonymous$$eyCode.Space ) ) {
          Rigidbody clone = Instantiate( cube );
  
          cube.transform.localPosition = Vector3.zero;
          cube.velocity = cube.transform.forward * speed;
      }
  }

Assets/Scripts/GunFire.js(1,18): UCE0001: ';' expected. Insert a semicolon at the end. Assets/Scripts/GunFire.js(2,10): BCE0043: Unexpected token: Rigidbody Assets/Scripts/GunFire.js(3,18): UCE0001: ';' expected. Insert a semicolon at the end. Assets/Scripts/GunFire.js(4,10): BCE0043: Unexpected token: float. Assets/Scripts/GunFire.js(6,10): BCE0043: Unexpected token: void. Assets/Scripts/GunFire.js(6,24): UCE0001: ';' expected. Insert a semicolon at the end. Assets/Scripts/GunFire.js(8,19): UCE0001: ';' expected. Insert a semicolon at the end.

avatar image daleth90 jdzach2000 · May 20, 2016 at 02:04 AM 0
Share

Well, that's because you just "copy and paste".

The main reason is the language I use is C#, not JavaScript, since I don't know anything about JavaScript. :(

You need to try to realize my code and rewrite into JavaScript.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Cannot remove sprite using destroy 1 Answer

Can Any Body Help me how to setup jenkins with unity3d 5.3.4 for automatic builds???? 0 Answers

Script sets color to multiple objects from instead of only one object with Random.range() 0 Answers

Animation depending on the Int value 0 Answers

Unable to enable or disable NavMeshAgent through script? 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