var currentTarget : Transform = null;
var weaponRange : float = 20.0;
function Update()
{
if (Input.GetMouseButton(0))
{
if (currentTarget == null)
{
var ray = Camera.ViewportPointToRay(Vector3(0.5,0.5,0));
var hit : RaycastHit;
if (Physics.Raycast(ray,hit,weaponRange))
{
if (hit.transform.tag == "Player")
{
currentTarget = hit.transform;
}
else
{
// maybe do normal damage to other objects you can't lock on.
// hit.transform refers to the object you hit
}
}
}
else
{
// We are locked on a target
var vectorTotarget = currentTarget.position - transform.position;
if (vectorTotarget.sqrMagnitude > weaponRange*weaponRange) // is the target out of range?
{
currentTarget = null; // clear target
}
` `else
{
// Do damage to the target
}
}
}
else if(Input.GetMouseButtonUp(0))
{
// when you release the fire button clear the target
currentTarget = null;
}
}
This script will shoot like every normal weapon, straight forward. If it hits a gmaeObject that is tagged "Player" it will lock to this target until you release the fire button.
It's just the concept. You have to change/extend it to your needs...
↧