Hey guys,
Fairly new to unity, and scripting in general. Working on my first project and having one issue i cant find and answer for.
Im making a top down shooter, i have most of the basic mechanics set up, the issue im having is that the projectiles the player shoots, while colliding correctly closer to the player, stop being detected if the enemy is at a distance from the player.
The distance is around 7 units, and only while the target is moving (collision appears to work normal when i make them stand still). I am also using the dontgothroughthings.js on the projectile.
The code is pretty rough
//shoot.js
var projectile : Rigidbody;
var speed = 20;
var reloadTime = 0.5;
var targ : Transform;
var force : int;
private var nextFireTime = 0.0;
function Start()
{
var go = GameObject.FindGameObjectWithTag("Player");
targ = go.transform;
}
function FixedUpdate ()
{
if (Input.GetButton ("Fire1")&& Time.time > nextFireTime)
{
nextFireTime = Time.time + reloadTime;
targ.transform.Translate(-Vector3.forward * Time.deltaTime*force);
clone = Instantiate(projectile,transform.position, transform.rotation);
clone.velocity=transform.TransformDirection(Vector3(0,0,speed));
}
}
//bullet.js
var dropOff : int = 4;
function Start ()
{
Destroy(gameObject,dropOff);
}
function OnCollisionEnter(collision : Collision)
{
Debug.Log("i hit thing!");
if(collision.gameObject.tag == "Enemy")
{
Debug.Log("i die here right?");
Destroy(gameObject);
}
}
//enemymovement.js
var waypoint : Transform[];
var speed : float=20;
private var currentWaypoint : int;
var loop : boolean = true;
var player : Transform;
var sight:int=10;
function Start(){
var go = GameObject.FindGameObjectWithTag("Player");
player = go.transform;
}
function Update () {
if(currentWaypoint < waypoint.length){
var target:Vector3 = waypoint[currentWaypoint].position;
var moveDirection:Vector3 = target - transform.position;
var distFromPlayer:Vector3 = player.position - transform.position;
var velocity = rigidbody.velocity;
if(moveDirection.magnitude<1){
currentWaypoint++;
}
else if(distFromPlayer.magnitude<1000){
target=player.position;
velocity = moveDirection.normalized*speed;
velocity= (player.position-transform.position).normalized*speed;
}
else{
velocity = moveDirection.normalized*speed;
}
}
else{
if(distFromPlayer.magnitude<1000){
velocity=Vector3.zero;
target=player.position;
velocity = moveDirection.normalized*speed;
}
if(loop){
currentWaypoint = 0;
}
else{
velocity = Vector3.zero;
}
}
rigidbody.velocity=velocity;
transform.LookAt(target);
}