i’ve got a problem relating to enemy AI- I want my enemies to fire a sphere at the player once they have detected them (I have script already to look at the player once they are in range, and not hidden behind walls etc- raycast)
however i’m finding it difficult to implement a shoot script, here’s what i have so far:
this brings error code: ArgumentException: get_time can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
EnemyAttack02…ctor () (at Assets/Scripts/EnemyAttack02.js:33)
var LookAtTarget : Transform;
var range = 100.0;
var damp = 5.0;
var bullet : Transform;
var Spawnpoint:GameObject;
var savedTime = 0;
function Update()
{
if(InAttackRange())
{
var targetRotation = Quaternion.LookRotation (LookAtTarget.position - transform.position, Vector3.up);
transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, Time.deltaTime * damp);
}
}
function InAttackRange()
{
if(Vector3.Distance (transform.position, LookAtTarget.position) > range)
{
return false;
}
var hit : RaycastHit;
if (Physics.Linecast (transform.position, LookAtTarget.position, hit))
{
if (hit.collider.gameObject.tag != "Player")
{
return false;
}
}
return true;
}
var seconds : int = Time.time;
var oddeven = (seconds % 2);
if(oddeven)
{
Shoot(seconds);
}
function Shoot(seconds)
{
if(seconds!=savedTime)
{
var bullet = Instantiate(bullet ,transform.Find("BulletSpawn").transform.position ,Quaternion.identity);
bullet.rigidbody.AddForce(transform.forward * 10);
savedTime=seconds;
}
}