Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by RevolutionNow · Jul 08, 2014 at 12:52 PM · physicsraycastprogramming

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;
     }
 }

 

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Graham-Dunnett ♦♦ · Jul 08, 2014 at 12:53 PM 0
Share

You might want to format this question. Also, is there code missing?

avatar image RevolutionNow · Jul 10, 2014 at 09:24 AM 0
Share

The question has been edited. What do you mean format the question? Sorry for being newbie, this is my first project.

avatar image WhoRainZone1 · Jul 10, 2014 at 09:42 AM 0
Share

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.

avatar image RevolutionNow · Jul 10, 2014 at 10:58 AM 0
Share

Sorry for the extreme confusion!

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0
Wiki

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RevolutionNow · Jul 10, 2014 at 10:33 AM 0
Share

It doesn't fix the problem. only generates a new one. I want to use the raycast cursor not the actual mouse.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges