Question by
Thedallames · Sep 13, 2021 at 06:44 PM ·
3dprojectile
Projectiles Fly in Seemingly Random Pattern
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);
}
}
Comment
Your answer

Follow this Question
Related Questions
Top Down Projectiles 1 Answer
Why are my projectiles spawning in batches? 1 Answer
Shooting projectiles with FPS controller 2 Answers
Transform rotation with an ease 0 Answers
[HELP] Folding view from 2 3D cameras. 0 Answers