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 permanentrecords · Apr 29, 2015 at 11:29 PM · raycastanimatorragdoll

Using a raycast to disable a component (animator)

I'm making a game that so far uses raycasts to tell where the bullets go. The bad guys all die in one hit, and I would like to have the raycast disable their animators so that they go ragdoll.

The ragdolls accept hits from the raycast and can be pushed all over. But I can't get the raycast to disable the animator.

 using UnityEngine;
 using System.Collections;public class Shooter : MonoBehaviour {
     public float gunPower;
     void Update() {
         if (Input.GetMouseButtonDown(0)) {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit))
                 if (hit.rigidbody != null)
                     hit.rigidbody.AddForceAtPosition(ray.direction * gunPower, hit.point);
                 
             
         }
     }
 }
 
Comment
Add comment · Show 3
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 tanoshimi · Apr 29, 2015 at 11:50 PM 0
Share

Ok... and how do you expect anyone to help you when you haven't posyted your script?

avatar image permanentrecords · Apr 29, 2015 at 11:56 PM 0
Share

The script works fine. The question is how to disable a component using a raycast. I didn't think it was important.

avatar image zeppike · Apr 30, 2015 at 08:36 AM 0
Share

I am a bit uncertain what you want with it, but I think this will disable the animator component on the object hit by the raycast.

 hit.transform.GetComponent<Animator> ().enabled = false;

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by timohausmann · Apr 30, 2015 at 06:44 PM

Hey. You first want to get the GameObject hit by your Raycast. Then get the Animator Component from that object, and finally disable it.

Untested code, but it should work like this:

 if (Physics.Raycast(ray, out hit)) {
 
   if (hit.rigidbody != null) {
 
     //get the hit gameObject
     GameObject hitObj = hit.transform.gameObject;
 
     //get the Animator Component
     Animator anim = hitObj.GetComponent<Animator>();
 
     //disable the Animator Component
     anim.enabled = false;
   }
 
 }
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

Answer by Runemark · Apr 30, 2015 at 08:49 AM

Check out this blog post about the transitions between mecanim and ragdoll. http://perttuh.blogspot.fi/2013/10/unity-mecanim-and-ragdolls.html

With raycast you can do something like this (pseudo like code)

 if (Physics.Raycast(ray, out hit))
 {
    // Check if the target is valid
    if(hit.transform.GetComponent<MecanimToRagdollScript>() != null)
    {
       if(<target is mecanim>)
          // call mecanim to ragdoll methos
       else // if it's already a ragdoll
         hit.rigidbody.AddForceAtPosition(ray.direction * gunPower, hit.point);
    }
 }                   
              

Hope it helped.

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

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast occasionally won't hit colliders on Mecanim animated characters (but hits perfectly if Animator is disabled) 1 Answer

Animation on Raycast Not Playing 1 Answer

Adding force to ragdoll after isKinematic is disabled? 1 Answer

Cant get Raycast to activate animations on different game objects with an if statement 0 Answers

Help with raycast from a ragdoll 0 Answers


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