- Home /
Move the bullet from the BulletEmitter in the direction player is facing 3D.
Hello, I want to make the bullet go from BulletEmitter which is already done but I want it to go IN THE DIRECTION MY PLAYER IS FACING. And this part is important I don't want to make it go forward forever:
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime); //WRONG CODE, IT GOES ALWAYS FORWARD ALONG THE Z AXIS.
I tried literally everything. With Rigidbody the bullets are weirdly rotating though I instantiate them with Quaternion.Identity. Also I tried launching the bullet this way: transform.Translate(BulletEmitter.forward * moveSpeed * Time.deltaTime); //Same effect as the previous code.
I have no idea what to do. Please help me. I tried using MoveTowards() and move it towards my crosshair but still does not work:Vector3.MoveTowards(transform.position, Crosshair.transform.position, maxDistance * moveSpeed * Time.deltaTime); //The bullet just stands still in the air...
If it's possible I would rather use Vector3 or transform to move the bullet instead of Rigidbody but Rigidbody is easier or better then okay. :/
Answer by Aspect13 · Apr 17, 2019 at 08:03 PM
FIXED IT! I just had to spawn the bullet with the same rotation as my Player was!
Instantiate(BulletPrefab, BulletEmitter.position, PlayerPrefab.transform.rotation);
The Update() with transform.Translate was fine:
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
Answer by NikitaDemidov · Apr 17, 2019 at 08:14 PM
Try this: If you want the bullet to follow the Player:
class ClassInBullet
{
bool StopMove;
public GameObject Player;
void Start()
{
StartCoroutine(move());
}
IEnumerator move()
{
while(!StopMove){
transform.position += (transform.position - Player.transform.position) / 10f;
yield return new WaitForSeconds(0.1f);
}
}
}
+This will let the tiles slower when they are nearer to the Player.
I tried it and had to change the code to this
void Start()
{
StartCoroutine(move());
Destroy(gameObject,3f);
}
IEnumerator move()
{
while (!Stop$$anonymous$$ove)
{
transform.position -= (transform.position - Player.transform.position) / 15f;
yield return new WaitForSeconds(0.08f);
}
}
It woked great. Here's a screenshot:I can't uplode it!? It doesn't stop to load.
If you want I can give you all the scripts.
It doesn't work the bullets still go like if I wrote Vector3.forward...
Your answer
![](https://koobas.hobune.stream/wayback/20220612195553im_/https://answers.unity.com/themes/thub/images/avi.jpg)