- Home /
Playing an objects animation that has been hit by a RayCast
Problem: I want to play an objects animation that has been hit by the raycast
.
So basically I'm setting up a a desert arena where you have to hit the targets as fast as you can with select guns. Anyways, my current problem is where the player hits the target and it plays its animation to go down.
||I USE CSHARP MOST OF THE TIME, SO PLEASE NO JAVASCRIPT|| Thanks!
Here is what I have tried - SEND MESSAGE WITH RECEIVER.
I do have a theory that you could use a "spring" physics component on the object, so whenever there is a force acting upon it, it moves. But I decided to ask you all anyways, just for opinions.
Please help, ask for coding that is needed. But I believe this is all you need for now: Thank you all in advance!!
void RayShoot (){
if(!isReloading) {
GameObject.Find("AK-47 FBX With Arms").GetComponent().Play("shoot");
RaycastHit hit;
//Vector3 DirectionRay = transform.TransformDirection(Vector3.forward);
Vector3 direction = transform.forward;
direction.x += Random.Range(-spreadFactor, spreadFactor);
direction.y += Random.Range(-spreadFactor, spreadFactor);
direction.z += Random.Range(-spreadFactor, spreadFactor);
Debug.DrawRay (transform.position, direction * Range, Color.blue);
if(Physics.Raycast(transform.position, direction , out hit, Range)){
SendMessage ("YourHit", true);
if(hit.rigidbody){
if(hitParticles){
hitParticles.transform.position = hit.point;
hitParticles.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, hit.normal);
hitParticles.Emit();
hit.rigidbody.AddForceAtPosition(direction * Force, hit.point);
hit.collider.SendMessageUpwards("ApplyDamage", Damage , SendMessageOptions.DontRequireReceiver);
}
}
}
BulletsLeft--;
if( BulletsLeft < 0){
BulletsLeft = 0;
}
if(BulletsLeft == 0){
Reload();
}
}
}
I am a bit unsure what your problem is. Is it if you are doing it in a 'good way'? Or is something not working(like raycast never hits anything)?
$$anonymous$$y raycast hits and works perfectly fine, i just want to play an objects animation that has been hit by the raycast
Answer by Ingen · Jul 30, 2012 at 05:03 PM
Hallo. that's is what I use for what U want
sorry if is in JS I don't chew C# but is enough easy and short
if( /*Input.GetButtonDown ("Fire1")*/ Input.GetKeyDown(KeyCode.R)) //input key
{
hit.collider.SendMessageUpwards("Active", SendMessageOptions.DontRequireReceiver);
"Active" is the message send, and function in second script need match and is case sensitive
and in the object who received the message
var animatedObj1 : GameObject; // Dooor
var animatedObj2 : GameObject; // Button
function Active(){
animatedObj1.animation.Play();
animatedObj2. animation.Play();
}
hope can help