- Home /
raycast and detecting if it did hi
Hello im curently making an FPS shooter. And my bullets are done by using raycast.
Now i want to make computer controlled bots, and i need them to be able to take damage from the user who shoots the raycast. Therefore i would like to know if it is possible to check if a give gameobject (bots, but lets call it a cube for testing purposes) is hit by a raycast.
have been searching google and the docs but cant seem to find anything, so would be nice if someone would help me :)
thanks in advance.
Answer by MaGuSware™ · Jan 05, 2013 at 07:24 PM
I don't think it's possible to check if something is hit by a raycast but you can check to see if the raycast has hit something.
When you are doing your raycast, include a RaycastHit variable which will contain the hit information of the raycast. Then you act upon that accordingly.
Here is an extract from my gun script:
In my "shoot" function
RaycastHit hit;
if(Physics.Raycast(rayOrigin, rayDirection, out hit, m_fMaxRange))
{
ProcessRayHit(hit);
}
And my ProcessRayHit function (stripped down)...
void ProcessRayHit( RaycastHit hit )
{
hit.collider.gameObject.SendMessage("DoDamage", m_iDamage, SendMessageOptions.DontRequireReceiver);
}
To send damage to my targets I use send message with "DoDamage" as the function name.
Some helpful links:
http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit.html
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.SendMessage.html
i have the following code:
if (distance > 0) {
var hit : RaycastHit;
if (Physics.Raycast(oldPos, direction, hit, distance)) {
// adjust new position
newPos = hit.point;
// notify hit
hasHit = true;
var rotation : Quaternion = Quaternion.FromToRotation(Vector3.up, hit.normal);
//Apply force if we hit rigidbody
if (hit.rigidbody){
hit.rigidbody.AddForce( transform.forward * impactForce, Force$$anonymous$$ode.Impulse );
}
//HIT SURFACES
if(impactHoles){
//var impact : GameObject;
for(var i : int = 0; i<impactObjects.Count; i++){
if(hit.transform.tag == impactObjects[i].name){
Instantiate(impactObjects[i], hit.point, rotation);
}
}
}
//We cant hit ourselfs
if(hit.transform.tag != "Player" && doDamage){
hit.transform.Send$$anonymous$$essageUpwards("ApplyDamage", damage, Send$$anonymous$$essageOptions.DontRequireReceiver);
}
Destroy (gameObject, 1);
}
}
and my dmg is handled by var damage : int = 20;
so how would i implement it?
well, you need a receiver script on what it is that you want to damage which will have a function like so
function ApplyDamage( damage : int )
{
//do the damage stuff here.
}
$$anonymous$$ine is like this
public class Health : $$anonymous$$onoBehaviour
{
public int m_i$$anonymous$$axHealth = 100;
int m_iCurHealth;
bool m_bAlive = true;
void Start()
{
m_iCurHealth = m_i$$anonymous$$axHealth;
}
public void DoDamage( int damage )
{
if (m_bAlive)
m_iCurHealth -= damage;
if (m_iCurHealth <= 0)
{
//Do $$anonymous$$ill
}
}
}
}
ok i now have the following: in my bullets script:
if(hit.collider){ hit.collider.gameObject.Send$$anonymous$$essage("DoDamage", damage, Send$$anonymous$$essageOptions.DontRequireReceiver); }
and my hp script: public int hp = 100;
public int currentHp;
bool alive = true;
void Start(){
currentHp = hp;
}
public void DoDamage(int damage){
if(alive){
// then we are alive
currentHp -= damage;
if(currentHp <= 0){
// then we are dead
alive = false;
}
}
}
still does not seem to work?
did you keep it as a C# class, or make it into a JS? Because if you made it into a JS that syntax is wrong.
JS should be
var currentHp : int = 100;
private var alive : bool = true;
function DoDamage(damage : int)
{
if (alive)
{
currentHp -= damage;
if (currentHp <= 0)
{
alive = false;
}
}
}
Your answer
Follow this Question
Related Questions
Bullet collision for space shooter - colliders, rays, etc? 2 Answers
Physics.Raycast Interval problem in the FixedUpdate(). 0 Answers
What is this variable type? 2 Answers
Use transform.LookAt with Raycast with a delayed bullet? 1 Answer
Ray curve for bullet gravity effect, or any way to make it 1 Answer