How does one continuously move an object forward without using velocity?
It seems that everyone uses rigidbodies and velocity to send bullets flying forward. I absolutely despise rigidbodies. Using a rigidbody is going to make the bullet move inconsistently and bounce off surfaces I don't want it to.
So, instead, what I'm trying to do is just move the bullet at a certain interval in a direction.
void fire(GameObject f){
GameObject bullet = Instantiate (f, transform.position + (transform.forward*2),transform.rotation);
StartCoroutine(goforward(bulletspeed,bullet));
}
IEnumerator goforward(float speed, GameObject go){
Vector3 forward = go.transform.forward;
go.transform.position += forward * Time.deltaTime;
StartCoroutine(goforward(speed,go));
return null;
}
I've got two problems with this current method I'm using.
The bullet is instantiating inside the player, yet it would seem it should instantiate a little bit forward based on how I called Instantiate in the fire method. (Less important)
The bullet isn't moving at all!
Answer by Hellium · Oct 29, 2018 at 08:01 AM
I would advise you to use a Transform to place the "spawn point" of the bullet. Thanks to this, you will be able to place it precisely where you want.
Then, I would advise you to extract the responsability of moving the bullet outside the Player class. The bullet should have its own script.
[SerializeField]
private Bullet bulletPrefab; // Drag & drop the prefab of the bullet in the inspector
void Fire( Bullet bulletPrefab )
{
Bullet bullet = Instantiate (bulletPrefab , barrelTransform.position,barrelTransform.rotation);
bullet.Shooter = this ; // If you want to know who shot the bullet to add points for instance
// Do nothing more, let the bullet do its job
}
// In the Bullet script
[SerializeField]
private float speed = 1;
public Player Shooter { get ; set ; }
void Update()
{
if( /* condition to check whether the bullet did not hit something */ )
transform.Translate( Vector3.forward * speed * Time.deltaTime ) ;
}
Your answer
Follow this Question
Related Questions
Instantiate spawn problem 1 Answer
Instantiate position for shooting projectile is way off. Could I get some help? 0 Answers
Instantiate GameObject with Velocity 1 Answer
i have a list of transforms and i want to spawn a prefab once for all the transforms in the list. 1 Answer
How to make an area inacessible? 1 Answer