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.
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);
}
}
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.
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.
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).
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.