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 /
  • Help Room /
avatar image
0
Question by JP.Games · Feb 15, 2016 at 03:56 AM · c#unity 5rigidbodyvelocityaddforce

Sphere with AddForce or Velocity not moving

I have been following this tutorial: https://www.youtube.com/watch?v=fMUpJNndzcY and this tutorial https://www.youtube.com/watch?v=fMUpJNndzcY to make a bullet. The first tutorial didn't help enought because he uses a model instead a shpere, so the transform didn't move after spawn, but I fixed it with the second tutorial, but now I have another problem, and the sphere doesn't move forward, either with .AddForce or with .Velocity.

If I press one time the SpaceBar (to Shoot), the ball spawns and falls directly instead of go forward and fall in the way, and if I hold the SpaceBar, they will be in a Row, one above the other, but always in the same coordinates.

This is part of my code:

 void FixedUpdate()
 {
     //...
     if(CrossPlatformInputManager.GetButton("FireButton") || isFire)
     {
         StopCoroutine(Shoot());
         StartCoroutine(Shoot());
     }
 }
 //...
 public Transform bulletPrefab;
 public float bulletspeed = 1000;

 public IEnumerator Shoot()
 {
     Rigidbody clone = Instantiate(bulletPrefab, transform.position, transform.rotation) as Rigidbody;
      
     clone.AddForce(new Vector3(bulletspeed , 0, 0), ForceMode.VelocityChange);
     //or
     clone.velocity = transform.TransformDirection(new Vector3(bulletspeed, 0, 0));

     yield return new WaitForSeconds(1);
 }

Another thing, and I think that is related with this issue, is that any code that is below of the .AddForce or .velocity line will be always omitted, so that WaitForSeconds or a Destroy line will not run and cause a lot of problem in my game.

Note that I've used different types of arguments with .addforce, like .AddForce(transform.forward, 100), and always I have the same problem.

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

Answer by Salmjak · Feb 15, 2016 at 11:31 AM

I'm pretty sure a RigidBody must be part of a transform, and should also have a collider. Don't instatiate it as Rigidbody, not sure how that would work. Use something like:

  bool isShooting = false;
 
 void FixedUpdate()
  {
      //...
      if(CrossPlatformInputManager.GetButtonDown("FireButton"))
      {
         isShooting = true;
          StartCoroutine(Shoot());
      }
    if(CrossPlatformInputManager.GetButtonUp("FireButton")){
      isShooting = false;
 }
  }
  //...
  public GameObject bulletPrefab;
  public float bulletspeed = 100f;
  public IEnumerator Shoot()
  {
 while(isShooting){
      GameObject clone = Instantiate(bulletPrefab, transform.position, transform.rotation) as GameObject;
 Rigidbody rigBody = clone.GetComponent<Rigidbody>();
       
      rigBody.AddForce(new Vector3(bulletspeed , 0, 0), ForceMode.VelocityChange);
      yield return new WaitForSeconds(1f);
 }
  }


Comment
Add comment · Show 7 · 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 JP.Games · Feb 15, 2016 at 02:02 PM 0
Share

Tried that code, but I get the "Cannot convert a Gameobject into a Transform" error, so I tried this code:

 GameObject clone = Instantiate(bulletPrefab).gameObject as GameObject;
 Rigidbody rigidBody = clone.GetComponent<Rigidbody>();
 rigidBody.AddForce(new Vector3(0, 0, bulletspeed), Force$$anonymous$$ode.VelocityChange);

 yield return new WaitForSeconds(1);

 Destroy(clone.gameObject, 5);

At least now everyline runs fine, but the bullet stills doing the same, and also it's spawns in the original spawn place ins$$anonymous$$d the actual player place.

EDit: sorry for the runtime error, I forget to change public Transform bulletPrefab; to public GameObject bulletPrefab;, but after change that I still having the same issue.

avatar image Salmjak JP.Games · Feb 15, 2016 at 02:13 PM 0
Share

You got the error because you're sloppy with copy-paste ;P You forgot the first line: public GameObject bulletPrefab; which says that the prefab is a gameobject, not a transform (as in your code).

$$anonymous$$y code was also more of a pseudo-code, you still have to put in the position in the Instantiate()-function as you did in your code :) Instantiate(bulletPrefab, transform.position, transform.rotation) as GameObject;

I think you also have to add "f" to bulletspeed, as in float bulletspeed = 100f; Floats can be weird like that, they don't always behave if you forget the "f"-suffix.

avatar image JP.Games Salmjak · Feb 15, 2016 at 02:34 PM 0
Share

Yes, never use a copy-paste haha... Anyway, I did everything again and now the bullets moves, but I have one more problem, and is that the bullets stills appearing from the original spawn place ins$$anonymous$$d the player place, that could be because I use this

 GameObject clone = Instantiate(bulletPrefab).gameObject as GameObject;

but if use this:

 GameObject clone = Instantiate(bulletPrefab, transform.position, transform.rotation) as GameObject;

the bullets don't move correctly (it's like the original problem).

Show more comments
avatar image Salmjak JP.Games · Feb 15, 2016 at 02:30 PM 0
Share

Btw, you do realise that if you hold down the fire-button you will just stop and start the coroutine. That will interrupt it which may be the cause of your problems. Remove "StopCoroutine()" and see if it works.

You shouldn't even use a coroutine. I dont see any reason for it since you're not looping anything.

@Jp.Games

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

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

Related Questions

Keep gravity while adding velocity to a Rigidbody? 1 Answer

How to set rigidbody velocity and angularVelocity to Vector3.zero over time? 1 Answer

How to move forward (towards z), ignoring any rotations? 2 Answers

Inconsistent AddForce results 0 Answers

struggling with Playing card game in unity 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