- Home /
Problem with raycasting and ragdoll!
Hi I'm trying to make a script where you can kill an enemy by using aiming at him and pressing the RMB. I have come pretty far but it doesn't work. What is supposed to happen is the characters animator should be disabled, The " IsKinematic" options should turn to false and the text "Dead" should appear in the console. when I give the script to a simple cube the text text appears. I would appreciate the help ALOT! thanks :)
Raycast script:
var Cursor : GameObject;
function Update () {
var hit : RaycastHit;
Cursor.guiTexture.color = Color.white;
if (Physics.Raycast(transform.position, transform.forward, hit))
{
if (hit.collider.gameObject.tag == "Enemy"){
Cursor.guiTexture.color = Color.red;
}
if (Input.GetButton("Fire1"))
{
hit.transform.SendMessage("kill",500,SendMessageOptions.DontRequireReceiver);
}
}
}
HP script(health for the enemy):
var HP : int = 100;
var EnemyRigid : Component [];
var animator : Animator;
var Damage : int = 500;
animator = gameObject.GetComponent(Animator);
function Start ()
{
EnemyRigid = GetComponentsInChildren(Rigidbody);
for (var i = 0; i <EnemyRigid.Length; i++){
EnemyRigid[i].rigidbody.isKinematic = true;
}
}
function kill (Damage: int) {
HP -=Damage ;
}
function Update () {
if (HP < 0){
Debug.Log ("Dead");
for (var i = 0; i <EnemyRigid.Length; i++)
EnemyRigid[i].rigidbody.isKinematic = false;
animator.enabled = false;
}
}
You might want to format this question. Also, is there code missing?
The question has been edited. What do you mean format the question? Sorry for being newbie, this is my first project.
By formatting he was referring to your quellcode.. look at my answer, that's formatted properly, except the first if.. the bracket should be in the same line as the if-statement for consiency, as the other if's also follow that way.
Answer by avi9111 · Jun 26, 2017 at 10:01 PM
Are there any other encounter on this problem? it was fine when I used a simple gameobject such as box or cart but my colleague created a ragdoll, you konw, a gameobject with multi charater joint. the same raycast return null for the ragdoll, any ideas?
Answer by WhoRainZone1 · Jul 10, 2014 at 09:40 AM
Normally I'm using C#, but this should work. It works at least in my projects. First you need to use your mouseposition as the ray, and then you also want to check the hit's name with the name of that gameObject.
function Update () {
var hit : RaycastHit;
var ray : Ray;
Cursor.guiTexture.color = Color.white;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit))
{
if (hit.collider.name == gameObject.name){
Cursor.guiTexture.color = Color.red;
}
if (Input.GetButton("Fire1")){
hit.transform.SendMessage("kill",500,SendMessageOptions.DontRequireReceiver);
}
}
}
That should be your new Update() function.
Cheers
It doesn't fix the problem. only generates a new one. I want to use the raycast cursor not the actual mouse.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Physics.Raycast not checking layermask properly? 1 Answer
Collision position - Improving on the 'grounded' mechanic 2 Answers
changing guiTexture color with raycast 1 Answer
RAYCAST KNOWLEDGE HELP!!! 1 Answer