well when my player turn my enemy shoot horizontal not vertical it only happen when i circle around the enemy it going in the right rotation i think the bullet is like a space shooter bullet projectile using a laser image.
here my enemy attack script and bullet movement script and image of the laser shot.
using UnityEngine;
using System.Collections;
public class EnemyAttack : MonoBehaviour {
public float timeBetweenAttacks = 0.5f;
public int attackDamage = 10;
bool playerInRange;
float timer;
GameObject player;
PlayerHealth playerHealth;
EnemyHealth enemyHealth;
public Transform shotSpawn;
public GameObject enemyShot;
void Start ()
{
player = GameObject.FindGameObjectWithTag("Player");
playerHealth = player.GetComponent<PlayerHealth> ();
enemyHealth = GetComponent<EnemyHealth> ();
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject == player)
{
playerInRange = true;
}
}
void OnTriggerExit (Collider other)
{
if (other.gameObject == player) {
playerInRange = false;
}
}
void Update ()
{
timer += Time.deltaTime;
if (timer >= timeBetweenAttacks && playerInRange && enemyHealth.currentHealth > 0)
{
Attack ();
}
}
void Attack ()
{
timer = 0f;
//Instantiate (enemyShot, shotSpawn.position, Quaternion.identity);
if (playerHealth.currentHealth > 0)
{
//playerHealth.TakeDamage (attackDamage);
Instantiate (enemyShot, transform.position, Quaternion.identity);
}
}
}
using UnityEngine;
using System.Collections;
public class Mover : MonoBehaviour {
public float speed;
Transform player;
void Start () {
player = player = GameObject.FindGameObjectWithTag ("Player").transform;
GetComponent<Rigidbody> ().velocity = (player.transform.position - transform.position).normalized * speed;
//transform.forward * speed;
}
}
for something as simple as “go forward” it’s a little bit of a waste to add a script to the shot, you can just access the component on the instantiated instance. Have the attack script point the shot at the target and then give it a shove
using UnityEngine;
using System.Collections;
public class Shoot: MonoBehaviour {
public float speed = 40;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
transform.Translate(Vector3.forward * Time.deltaTime * speed);
}
}
then within your enemy script something like
using UnityEngine;
using System.Collections;
public class EnemyAttack : MonoBehaviour
{
public float timeBetweenAttacks = 0.5f;
public int attackDamage = 10;
bool playerInRange;
float timer;
GameObject player;
PlayerHealth playerHealth;
EnemyHealth enemyHealth;
public Transform shotSpawn;
public GameObject enemyShot;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
playerHealth = player.GetComponent<PlayerHealth>();
enemyHealth = GetComponent<EnemyHealth>();
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject == player)
{
playerInRange = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject == player)
{
playerInRange = false;
}
}
void Update()
{
timer += Time.deltaTime;
if (timer >= timeBetweenAttacks && playerInRange && enemyHealth.currentHealth > 0)
{
Attack();
}
}
void Attack()
{
timer = 0f;
//Instantiate (enemyShot, shotSpawn.position, Quaternion.identity);
if (playerHealth.currentHealth > 0)
{
//playerHealth.TakeDamage (attackDamage);
GameObject obj = (GameObject)Instantiate(enemyShot);
obj.transform.position = transform.position;
obj.transform.LookAt(player.transform.position);
}
}
}
Vector3.foward will send the bullet forward
Transform.lookat(player) will get it to rotate towards the player and fire
To be clear, there are two fundamentally different ways to make something move in Unity. @LeftyRighty is assuming that you’re using physics, i.e., your shot has a Rigidbody. So all you have to do is set its velocity, and if all the other physics parameters are right (zero drag, etc.), then it’ll go — no script required. @Rob21894 is showing the alternative, which is to not use physics (no Rigidbody on your shot), and add a little script to make it move.
Personally, I usually favor the second (no physics) approach unless physics is really necessary for some other reason.
okay thanks i am going to try that cause the problem is the problem is the bullet shoot horizontal when turning, i want it to shoot vertical even if the player turn or not
Oh, so the direction of the bullet should have nothing to do with the rotation of the player?
I think the bullet rotating because that’s what you told it to do when you created it:
GameObject shot = Instantiate(enemyShot, transform.position, rotation) as GameObject;
That third parameter is the rotation. If you don’t want to do that, then you can just leave that third parameter out, or pass in some constant like Vector3D.up.
it working great bro but the funny thing why my enemy lookat rotation turn my enemy to the wrong rotation like spin it around and some error when get close to the player see in the image above.
using UnityEngine;
using System.Collections;
public class EnemyMovement : MonoBehaviour {
Transform player;
PlayerHealth playerHealth;
EnemyHealth enemyHealth;
public float damping;
public float Speed = 10;
void Start () {
player = GameObject.FindGameObjectWithTag ("Player").transform;
playerHealth = player.GetComponent<PlayerHealth> ();
enemyHealth = GetComponent<EnemyHealth> ();
}
// Update is called once per frame
void Update ()
{
if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
{
lookAtPlayer ();
Movement ();
}
}
void lookAtPlayer ()
{
Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * damping);
}
void Movement ()
{
transform.position = Vector3.MoveTowards (transform.position, player.position, Speed * Time.deltaTime);
}
}