Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
1
Question by Tomblade13 · Oct 26, 2011 at 11:08 AM · rigidbodyaddforcebulletforward

make bullet move forward.

I have this script var bulletprefab:Transform; function Update () { if(Input.GetButton("Fire1")) { var bullet = Instantiate(bulletprefab, GameObject.Find("SpawnPoint").transform.position, Quaternion.identity); bullet.rigidbody.AddForce(Transform."2000"); } }

and I want the bullet to fire forward, but as it stands all it does is give me the error, Assets/scripts/bullet.js(9,42): BCE0023: No appropriate version of 'UnityEngine.Rigidbody.AddForce' for the argument list '(int)' was found.

I am new to unity and scripting so im a little confused, an explanation would be appritiated.

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 syclamoth · Oct 26, 2011 at 10:24 AM 0
Share

Problem is, Transform."2000" isn't a thing. As in, not even slightly. Try using

 bullet.rigidbody.AddForce(transform.forward * 2000);

ins$$anonymous$$d. The rest of it looks pretty much ok, although you might want to put a few more explicit types in there (I don't know why javascript even has the other kind)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Kacer · Oct 26, 2011 at 11:20 AM

About bullets and games there's a few things you should know.

If you're making a regular gunbullet, like, lets say from a pistol, then i wouldnt use a mesh/collider to detect if you've hit anything, there's 2 reasons for this.

  1. the bullet you spawn is an uneccesary drag on your systems resources, its moving too fast for anyone to see it anyway.

  2. This one is more important and is the main reason: If your bullet is moving above a certain velocity, depending on your frames per second, the collision will not be detected, this is because objects arent really moving when they go from point a to point b, they're teleporting small distances every frame, which in the end gives the illusion of movement. So, if the speed is too fast it will simply move through the object without detecting any collisions.

You can remedy this by using a Raycast, A raycast casts a ray in a given direction and sees if you hit anything, if it does it returns the object that was hit. this can be used instead of the bullet, or you can make the raycast originate from the bullet itself.

about your question, the line of code thats giving you the error is this one:

 bullet.rigidbody.AddForce(Transform."2000");

you should write it as:

 bullet.rigidbody.AddForce(new Vector3(0,2000,0)); 

instead, thats in c# btw, there should be something equivalent in javascript, here's a link to the documentation: Clicky

Comment
Add comment · Show 4 · 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 Tomblade13 · Oct 26, 2011 at 11:38 AM 0
Share

this help so much, apart from the bullets are now firing upwards instrad of accross. the game is a topdown game and the bullets are firing up. i dont suppose you know why this is?

avatar image Kacer · Oct 26, 2011 at 11:46 AM 0
Share

thats because the "new Vector3" part of the code point up, a vector is a direction in 3D space, it looks like this: Vector3(x,y,z) so, if you put 2000 in the y spot it will push with a force of 2000 on the y-axis.

if you want it to go in another direction, try writing it like this: "new Vector3(2000,0,0)" or "new Vector3(0,0,2000)", you can use negative values as well.

avatar image Tomblade13 · Oct 26, 2011 at 11:54 AM 0
Share

I have found a major problem, the bullets fire in the same direction dispite the rotation of the character or spawnpoint.

avatar image Kacer · Oct 26, 2011 at 12:04 PM 0
Share

try using vector3.forward

here's a link to what you can do with vector3 http://unity3d.com/support/documentation/ScriptReference/Vector3.html

avatar image
0

Answer by AstroneerKing · Jul 22, 2020 at 04:07 PM

You are sort of right that it is more performant to use Raycast, but a lot of games, for example, destiny II, use real bullet objects and it adds great visual effects to the game.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How could I use AddForce() towards another Object without using transform.forward 1 Answer

bullets don't go forward 1 Answer

How to make my bullets spread when fired 3 Answers

Rigidbody.addforce then rigidbody.rotation and movement along transform.forward 2 Answers

How to use AddForce on FPC 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