- Home /
Set bullet direction according to turret direction
Hey guys, Ive got my bullets to spawn through my BulletSpawnPoint through Instantiation.
using UnityEngine;
using System.Collections;
public class Bullet : MonoBehaviour
{
public float speed;
public float maxFlightTime;
public float currentFlightTime;
public string firedBy;
// Use this for initialization
void Start ()
{}
// Update is called once per frame
void Update ()
{
transform.Translate(Vector3.up* speed * Time.deltaTime);
currentFlightTime += Time.deltaTime;
}
public void setFiredBy(string shooter)
{
firedBy = shooter;
// GameObject shooterObj = GameObject.Find(shooter);
// Physics.IgnoreCollision(collider, shooterObj.collider);
}
}
this is my bullet script, i understand that my bullets keeps firing upwards, but how do i make them fire according to the direction of my turret
The solution will be in your Spawn code. It will need to rotate the bullet to face the direction to be fired. Note this would be much easier if you constructed your bullet so that the front was forward rather than up. That is the front of the bullet faced positive 'z'. Also note that by using 'Transform.Translate()', you may have trouble detecting collisions.
this is my spawn code
GameObject bullet = (GameObject)Instantiate(bulletPrefab, transform.Find("TurretPivotControl/Turret/BulletSpawnPoint").transform.position,transform.rotation);@robertbu
It should take the rotation from the turret if your spawn code is from the turret. So really you just need to use Vector3.forward ins$$anonymous$$d of Vector3.up... if your bullet is rotated correctly when imported into Unity.
While we could talk you through some solution given the current rotation of your bullet, the best solution is to fix the mesh. You can do that by:
Change it in your modeling program before importing.
$$anonymous$$ake the visible game object a child of an empty game object. The empty game object should be located at the pivot point of the visible object. When the empty game object has a rotation of (0,0,0), the front of the visible game object should be facing positive 'z'.
You can use the Rotate$$anonymous$$esh editor script. This script creates a clone of the original with the new rotation. You will find the script in my answer here.
Your answer
![](https://koobas.hobune.stream/wayback/20220613141414im_/https://answers.unity.com/themes/thub/images/avi.jpg)