Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Omega_V · Jun 11, 2021 at 10:16 AM · rigidbodyraycasthierarchyragdollrigging

Ragdoll won't recieve damage from raycast gun, or allow move code, or deactivate

I have experienced many issues when trying to apply Ragdoll physics to an enemy in my game. When I applied these ragdoll physics to the enemy, it wouldn't follow its script to follow the player, it wouldn't recieve damage from the raycast gun i made, and I also couldn't disable the ragdoll in the enemy's script.

Help or fixes to the script would be greatly appreciated.

the enemy script:

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class NewZombieScript : MonoBehaviour
 {
     public float ZombieSpeed = 4;
     public float ZombieRotationSpeed = 3;
     public float MaxDistzombie = 22.1f; // if larger than this, don't chase, otherwise, chase if lower than this and higher than MinDistzombie
     public float MinDistzombie = 3; // if larger than this and smaller than MaxDistzombie, chase player
     public Transform target;
     public float PlayerDistance;
     private bool chasing;
     public bool Alive;
 
     public int MaxZombieHP = 40;
     public int CurrentZombieHP;
     public HealthBar ZombieHealthBar;
 
     public Animator ZombieAnimator;
     List<Rigidbody> rigidBodies;
 
     private float attackTime; //time since last attack
     public const float attackRate = 1f; // attack cooldown
 
     private void Awake()
     {
         chasing = false;
         Alive = true;      
     }
 
     public void Damage(int ZombieDamage)
     {
         CurrentZombieHP -= ZombieDamage;
         ZombieHealthBar.SetHealth(CurrentZombieHP);
     }
 
     internal static void Damage()
     {
         throw new NotImplementedException();
     }
 
 
 
 
 
     // Start is called before the first frame update
     void start()
     {
         rigidBodies = new List<Rigidbody>(transform.GetComponentsInChildren<Rigidbody>());
         Alive = true;
         CurrentZombieHP = MaxZombieHP;
         rigidBodies.Remove(GetComponent<Rigidbody>());
         PlayerDistance = Vector3.Distance(transform.position, target.position);
     }
 
     // Update is called once per frame
     void Update()
     {
         PlayerDistance = Vector3.Distance(transform.position, target.position);
 
         if (Alive)
         {
             DeActivateRagDoll();
 
             if (PlayerDistance <= MaxDistzombie)
             {
                 GoToTarget();
             }
             if (PlayerDistance < MinDistzombie)
             {
                 StopZombie();
                 ZombieAttack();
             }
         }
 
         if(Input.GetKeyDown(KeyCode.M))
         {
             ActivateRagdoll();
         }
 
 
 
 
         if (attackTime > 0)
         {
             attackTime -= Time.deltaTime;
         }
 
 
 
 
         void GoToTarget()
         {
             if (PlayerDistance > MinDistzombie)
             {
                 ZombieSpeed = 4f;
                 chasing = true;
                 Debug.Log("Chasing");
             }
         }
 
         if (CurrentZombieHP <= 0)
         {
             Die();
         }
 
 
 
 
         if (chasing)
         {
             //rotate to look at the player
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), ZombieRotationSpeed * Time.deltaTime);
 
 
             //move towards the player
             transform.position += transform.forward * ZombieSpeed * Time.deltaTime;
             Debug.Log("MovingToPlayer");
 
             if (PlayerDistance > MaxDistzombie)
                 chasing = false;
 
             //give up when out of range
 
         }
 
 
 
         else
         {
             //not currently chasing
 
             //start chasing when player is close enough
 
         }
 
 
         void StopZombie()
         {
             chasing = false;
             ZombieSpeed = 1;
             Debug.Log("StoppedMoving");
         }
 
         void Die()
         {
             Alive = false;
             ActivateRagdoll();
         }
 
         void ZombieAttack()
         {
             if (attackTime <= 0)
             {
                 attackTime = attackRate;
                 target.GetComponent<PlayerHealth>().TakeDamage(8);
                 Debug.Log("AttackingPlayer");
             }
 
         }
 
 
 
         void DeActivateRagDoll()
         {
             ZombieAnimator.enabled = true;
             for (int i = 0; i < rigidBodies.Count; i++)
             {
                 rigidBodies[i].useGravity = false;
                 rigidBodies[i].isKinematic = true;
             }
         }
 
         void ActivateRagdoll()
         {
             ZombieAnimator.enabled = false;
             for (int i = 0; i < rigidBodies.Count; i++)
             {
                 rigidBodies[i].useGravity = true;
                 rigidBodies[i].isKinematic = false;
             }
 
             foreach (Rigidbody rb in rigidBodies)
             {
                 rb.isKinematic = false;
             }
 
 
 
 
         }
 
 
     }
 }
 

the gun script:

 using UnityEngine;
 
 public class Gun : MonoBehaviour {
 
 public float PistolDamage = 10f;
 public float PistolRange = 100f;
 public float impactForce = 15f;
 public float fireRate = 3f;
 
     public Camera fpsCam;
     public ParticleSystem muzzleflash;
     public GameObject impactEffect;
     
 
     private float nextTimeToFire = 0f;
 
     // Update is called once per frame
     void Update(){
 
     if (Input.GetButtonDown("Fire1") && Time.time >= nextTimeToFire)
        {
             nextTimeToFire = Time.time + 1f / fireRate;
           Shoot();
        }
 
 }
 
 void Shoot ()
 {
         muzzleflash.Play();
 
     RaycastHit hit;
 
         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, PistolRange))
     //what's below only happens when a target is hit by the pistol's raycast
     {
       
         Debug.Log(hit.transform.name);
 
             NewZombieScript target = hit.transform.GetComponent<NewZombieScript>();
             if(target != null)
             {
                 NewZombieScript.Damage();
             }
 
             if (hit.rigidbody != null)
             {
                 hit.rigidbody.AddForce(-hit.normal * impactForce);
             }
 
             GameObject ImpactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
             Destroy(ImpactGO, 2f);
            }
           }
          }


Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

248 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 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 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 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 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 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 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 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 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 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 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 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

Bugging ragdoll 1 Answer

how to prevent a kinematic rigidbody from going through static colliders 0 Answers

How to make a custom ragdolls with different bones 1 Answer

Rigidbody blocking raycasts 2D 0 Answers

Problem. Pick up and Grab object script, except all objects in scene are picked up instead of one. 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