Projectiles Fly About in Random Patterns,Projectiles Fly in Random Patterns
I have a top down 3D RPG that I am working on, but when I spawn a projectile it flies around in a seemingly random pattern until disappearing. I have a Projectile prefab, and some code for the staff the character is wielding. Here is my code: Staff Code: public class IceStaff : MonoBehaviour { [SerializeField] private GameObject projectilePrefab; private GameObject _projectile; public GameObject _staff;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0)){
Shoot();
StartCoroutine(ProjectileDestroy());
}
}
void Shoot(){
Vector3 startPos = _staff.transform.position;
Vector3 endPos = _staff.transform.forward * 10;
Debug.DrawRay(startPos, endPos);
RaycastHit hit;
if (Physics.Raycast(startPos, endPos, out hit)){
Debug.Log(hit.transform.name + " was hit");
}
_projectile = Instantiate(projectilePrefab) as GameObject;
_projectile.transform.position = transform.TransformPoint(Vector3.forward * 1.5f);
_projectile.transform.rotation = transform.rotation;
}
private IEnumerator ProjectileDestroy(){
yield return new WaitForSeconds(2);
Destroy(this._projectile);
}
}
Projectile Code: public class Projectile : MonoBehaviour { public float speed = 15.0f; public int damage = 1;
// Start is called once in the beginning
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(0,0, speed * Time.deltaTime);
}
void OnTriggerEnter(Collider other){
Destroy(this.gameObject);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613043001im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Bullet goes sideways when the player gets hit 1 Answer
projectile travels parallel to ground and not local space 1 Answer
How to spawn projectiles on the server from a client. 0 Answers
enemy projectiles 0 Answers
2.5D Projectile Help 1 Answer