i group the bullet, but the bullet clone are outside the group when they play, how can i fiz that? for the bullet clone appears inside the group not outside.
You need to make the transform parent of the clone to whatever object you want it to be “inside” of.
using UnityEngine;
using System.Collections;
public class EnemyAAi : MonoBehaviour {
public Transform target;
private float speed = 20;
public float normalSpeed = 20;
public float maxSpeed = 40;
public float rotateSpeed = 3;
public float lookAtDistance = 80;
public float attackRange = 40;
private Transform myTransform;
private bool moveForward = false;
public Rigidbody bullet;
public float bulletSpeed = 70;
public AudioClip laserSound;
public Transform muzzle;
public float attackTimer = 1;
private float timer = 0;
// Use this for initialization
void Start ()
{
myTransform = transform;
}
// Update is called once per frame
void Update ()
{
//40 to attack
//80 to look
float distance = Vector3.Distance(target.position, myTransform.position);
if(distance < lookAtDistance)
{
LookAtTarget();
speed = maxSpeed;
moveForward = true;
if(distance <= attackRange)
{
moveForward = false;
//attack code
TimeStuff();
}
}
else
{
moveForward = false;
}
//this is for making shure the ship moves and stops
if(moveForward)
{
MoveToTarget();
}
else
{
return;
}
}
//move towards player
private void MoveToTarget()
{
rigidbody.AddForce(myTransform.forward * speed);
}
//look at player
private void LookAtTarget()
{
//myTransform.LookAt(target);
Quaternion rotation = Quaternion.LookRotation(target.position - myTransform.position);
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, rotation, rotateSpeed * Time.deltaTime);
}
private void ShootBullet()
{
Rigidbody clone;
clone = Instantiate(bullet, muzzle.position,muzzle.rotation) as Rigidbody;
clone.velocity = transform.forward * 100;
timer = attackTimer;
}
private void TimeStuff()
{
//making shure timer is doing what is supposed to do
if(timer <= 0)
{
timer = 0;
}
if(timer > 0)
{
timer -= Time.deltaTime;
}
if(timer == 0)
{
ShootBullet();
}
}
public float health=100; //the health variable.
public float damage=10; //damage amount.
public void hit()
{
health-= damage;
if (health < 0)
{
}
}
}