- Home /
RayCast to send message to the object it hits?
I have a zombie and its got simple AI on it and I have this gun script that should send a message to it. But it keeps saying "Object reference not set to an instance of an object". anyway here are the scripts.
GUN SCRIPT
var hit : RaycastHit;
var AmoInClip = 0;
var Clips = 2;
var AmoInClips = 30;
var AmoLeft = 0;
var BulletDammage = 100;
var Bullet : Transform;
var Spawn : GameObject;
function Update()
{
var hit : RaycastHit;
if(Input.GetMouseButtonDown(0))
{
Fire();
}
if(Input.GetKeyUp(KeyCode.R))
{
Reload();
}
if(AmoInClip <= 0)
{
}
AmoLeft = AmoInClips * Clips + AmoInClip;
}
function Fire()
{
if(hit.gameObject.WithTag("zombie"))
{
hit.collider.SendMessageUpwards ("ApplyDammage", BulletDammage, SendMessageOptions.DontRequireReceiver);
}
}
function Reload()
{
yield WaitForSeconds(2);
AmoInClip = 0;
Clips -= 1;
AmoInClip = AmoInClips;
}
function OnGUI()
{
GUI.Label (Rect (0,80,80,20), AmoLeft.ToString(), "box");
}
ZOMBIE SCRIPT
var Distance;
var Target : Transform;
var lookAtDistance = 25.0;
var chaseRange = 15.0;
var attackRange = 1.5;
var moveSpeed = 5.0;
var Damping = 6.0;
var attackRepeatTime = 1;
var trigger : boolean = false;
var TheDammage = 40;
var DamageToMe = 0;
private var attackTime : float;
var controller : CharacterController;
var gravity : float = 20.0;
private var MoveDirection : Vector3 = Vector3.zero;
function Start ()
{
DamageToMe += Random.Range(1,600);
moveSpeed = Random.Range(2,5);
Target = GameObject.FindWithTag("Player").transform;
attackTime = Time.time;
}
function Update ()
{
Distance = Vector3.Distance(Target.position, transform.position);
if (Distance < lookAtDistance)
{
lookAt();
}
if (Distance < attackRange)
{
attack();
}
else if (Distance < chaseRange)
{
chase ();
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(Target.position - transform.position);
//transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
var dir = Target.position - transform.position;
dir.y = 0; // kill height differences
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(dir), moveSpeed * Time.deltaTime);
}
function chase ()
{
if(trigger == true)
{
transform.localPosition.y += 0.05;
}
moveDirection = transform.forward;
moveDirection *= moveSpeed;
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
function attack ()
{
if (Time.time > attackTime)
{
Target.SendMessage("hurt");
attackTime = Time.time + attackRepeatTime;
}
}
function ApplyDammage ()
{
chaseRange += 30;
moveSpeed += 2;
lookAtDistance += 40;
}
function OnTriggerEnter()
{
trigger = true;
yield WaitForSeconds(1);
trigger = false;
}
function Hurt()
{
DamageToMe += 100;
if(DamageToMe <= 0)
{
Destroy(gameObject);
}
}
Answer by aldonaletto · Oct 10, 2013 at 11:26 PM
The function Fire is wrong: you should do a Raycast in order to fill the RaycastHit structure - something like this:
function Fire()
{
// do a raycast in the gun forward direction:
if (Physics.Raycast(transform.position, transform.forward, hit){
// if something is hit, check whether it's tagged "zombie":
if(hit.gameObject.CompareTag("zombie"))
{ // if so, apply damage to it
hit.collider.SendMessageUpwards ("ApplyDammage", BulletDammage, SendMessageOptions.DontRequireReceiver);
}
}
}
But be aware that the zombie's ApplyDammage function isn't applying any damage at all - apparently, the damage is applied by the Hurt function; either use SendMessage("Hurt",...) or change the ApplyDamage code to call Hurt.
thanks! but im getting a error with it
$$anonymous$$issingFieldException: UnityEngine.RaycastHit.gameObject
Change line 6 to:
if (hit.collider.tag == "zombie")
or:
if (hit.collider.gameObject.CompareTag("zombie"))
Your answer
Follow this Question
Related Questions
Optimize script or Other way get material name (js) 2 Answers
Raycast with SendMessage can send Parameter, but how to send a variable ? 2 Answers
Detect Object that are hit by a ray 3 Answers
Findout if an object that was hit by raycast is still being hit (30 object that are instantiated) 0 Answers
Player wont be found on start 1 Answer