Hello! I just need help with my Enemies. The bullet only facing a certain direction. I made an empty game object, and added the script to it. I put the game object as a child to the enemy. No matter how much I rotate the Fire Point, the bullet won’t move. Any Help is appreciated. Here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AIBullet : MonoBehaviour {
public GameObject referenceToBulletPrefab;
public float timeToWaitBetweenShots;
void Start(){
InvokeRepeating("ShootBullet", timeToWaitBetweenShots, timeToWaitBetweenShots);
}
void ShootBullet(){
Vector3 positionToShootFrom = transform.position + transform.forward;
Instantiate(referenceToBulletPrefab, positionToShootFrom, Quaternion.Euler(Vector3.zero));
}
// Update is called once per frame
void Update () {
}
}
It’s hard to tell because your code is pasted so weird here, but… I don’t see anything here where you’re actually moving the bullet (are you doing it in your bullet script?).
You’ll want a rigidbody (not kinematic) on your bullet then apply some force to move it. Ex. set Rigidbody.velocity (not in update), or Rigidbody.AddForce to name two ways.
On my bullet on current project, I use Rigidbody.velocity… Unity - Scripting API: Rigidbody.velocity
You either set this in the Awake or Start (for example) of your bullet’s script (not the script you’re instantiating the bullet with), or you can set it right in the script where you instantiate, just after you instantiate. Instantiate function returns a reference to the gameobject’s reference, so you can return into a variable and then set the velocity on that.
ex.
bullet = Instantiate(yadayada);
bullet.GetComponent<RigidBody>().velocity = transform.forward*10.0f;
Edit: here’s a good example of using rigidbody, actually. It’s a multiplayer tutorial, but ignore that and skip to the part on firing a bullet. It short and sweet: https://unity3d.com/learn/tutorials/temas/multiplayer-networking/shooting-single-player