- Home /
[I REALLY NEED HELP FAST]Help with enemy Shooting
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 () {
}
}
Which thing are you changing? The positionToShootFrom? The bullet spawn is entirely based on that, so if you don't change that, it will always emerge from in front.
Also, what's your bullet movement script? Or are you not focusing on that right now?
Answer by cstooch · Jun 23, 2017 at 09:01 PM
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... https://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html
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
Agreed, this is the only problem I see. Sloth's "forward" instantiation idea works fine.
@$$anonymous$$ingSloth You could also have a simple translation script transform.position += transform.forward * speed * Time.deltaTime;
ins$$anonymous$$d of dealing with rigidbodies. Up to you.
That will work, but I think the only issue with that is then you're moving a static collider around, which apparently is less efficient, according to this: https://unity3d.com/learn/tutorials/topics/physics/physics-best-practices?playlist=17120
(jump to the Rigidbody section)
And moving a rigidbody without physics I believe can cause issues with the collider.
Actually I Googled this up, and I guess in Unity 5+ it isn't an issue to move rigidbody without physics anymore by the sounds of it. edit: O$$anonymous$$.. maybe.. conflicting reports out there. LOL. I just do it the way I'm familiar with. Rigidbody + physics for bullets.
Yeah, honestly, don't quote me on any of that. lol I'm still learning, myself. And it doesn't help that some of the info you find on the net is dated!
$$anonymous$$oving an object without a rigidbody by simply moving it's position is most likely more efficient than moving a rigidbody with physics. But I thought I'd read somewhere that moving an object with a collider without a rigidbody on it could cause issues with collision detection. i very well could be wrong, or that could be dated info.
If it works, it works though, and I would just ignore my ramblings on this! :)
bullet.GetComponent().velocity = bullet.transform.forward * 10.0f;
You're rusty :P
ah it's just a kink in the forum's dealing with <>
. You were right, never $$anonymous$$d
Yeah, I edited it.. stupid thing :) Has to go in code tags.