Question by
WindicaGames · Nov 22, 2020 at 06:25 PM ·
transformbullettransform.translate
Bullet go random directions
Hello Everyone ! I have a little problem in my game with distance attacks. I wrote a code where my character can shot bullets, everything looks okay for me, but after spawn them, they do random stuff. One time they go forward(like they should go - it sometimes happens when i stay in same place for a while), next time they flying around without any sense etc.
i added rigidbody to object(sphere).
There is my code in character script : Quaternion rot = transform.rotation; rot.x = 0f; rot.z = 0f;
if (bullets > 0)
{
Object[] bulletsTab = new Object[nrOfBullets];
for (int i = 0; i < bullets; i++)
{
bulletsTab[i] = Instantiate(Resources.Load("Bullet"), new Vector3(transform.position.x, transform.position.y + bulletYpossition, transform.position.z), rot);
Destroy(bulletsTab[i], 3f);
impacted = true;
}
}
and there is code for bullet script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public float speed;
public int damage;
void Start()
{
}
void Update()
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Enemy")
{
other.GetComponent<Mob>().getHit(damage);
Destroy(gameObject);
}
}
}
Comment
Answer by WindicaGames · Nov 23, 2020 at 03:10 PM
Finally ! I find the answer. I forgot to put "is Trigger" to Object Bullet to true statment :)