- Home /
Question by
convictcartel · Aug 21, 2015 at 08:51 AM ·
collisiongameobjectraycastmessage
Send Message On Collision
Ive been messing around with this part of code to make this work with a collision of my weapon, instead of raycast because my game is thirdperson, any ideas how I can change this script? Thank you!
#pragma strict
var rayLength = 10;
private var treeScript : TreeController;
function Update()
{
var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position, fwd, hit, rayLength))
{
if(hit.collider.gameObject.tag == "Tree")
{
treeScript = GameObject.Find(hit.collider.gameObject.name).GetComponent(TreeController);
if(Input.GetButtonDown("Fire1") && playerAnim.canSwing == true)
{
treeScript.treeHealth -= 1;
}
}
}
}
Comment
Answer by Youri1er · Aug 21, 2015 at 01:50 PM
You must have rigidbody and collider/trigger on your tree and a collider with your weapon.
You keep your animation of weapon when the player Fire1. But weapon in these script will have a function :
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Tree")
treeScript.treeHealth -=1;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612000839im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Raycast from inside an object 2 Answers
Grabbing MonoBehavior object from collision 1 Answer
Layered Collisions 1 Answer
Select a group of gameobjects that are contiguous 1 Answer
Raycast Collision - Getting Custom Values from GameObject 1 Answer