コード例 #1
0
        public override void OnInspectorGUI()
        {
            UMAPhysicsAvatar avatar = (UMAPhysicsAvatar)target;

            avatar.ragdolled = EditorGUILayout.Toggle(new GUIContent("Ragdolled", "Toggle to turn on/off the Ragdoll"), avatar.ragdolled);
            DrawDefaultInspector();
        }
コード例 #2
0
        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            UMAPhysicsAvatar avatar = (UMAPhysicsAvatar)target;

            avatar.ragdolled = EditorGUILayout.Toggle(new GUIContent("Ragdolled", "Toggle to turn on/off the Ragdoll"), avatar.ragdolled);
            //DrawDefaultInspector();
            DrawPropertiesExcluding(serializedObject, new string[] { "ragdollLayer", "playerLayer", "onRagdollStarted", "onRagdollEnded" });

            GUILayout.Space(10);
            EditorGUILayout.HelpBox("Sets layer 8 and 9 to Ragdoll and Player. If your code uses different layers do not use this defaults button", MessageType.Info);
            if (GUILayout.Button("Add Default Layers"))
            {
                AddDefaultLayers(ragdollLayer, playerLayer);
            }
            EditorGUILayout.HelpBox("The Ragdoll layer needs it's collision matrix layers set to collide with only itself. Set this in Edit->Project Settings->Physics->Layer Collision Matrix", MessageType.Info);
            ragdollLayer.intValue = EditorGUILayout.LayerField("Ragdoll Layer", ragdollLayer.intValue);
            playerLayer.intValue  = EditorGUILayout.LayerField("Player Layer", playerLayer.intValue);

            GUILayout.Space(10);
            EditorGUILayout.PropertyField(onRagdollStarted);
            EditorGUILayout.PropertyField(onRagdollEnded);

            serializedObject.ApplyModifiedProperties();
        }
コード例 #3
0
        IEnumerator TimedRagdoll(RaycastHit hit)
        {
            Transform        avatar = hit.rigidbody.transform.root;
            UMAPhysicsAvatar player = avatar.GetComponent <UMAPhysicsAvatar>();

            player.ragdolled = true;
            yield return(new WaitForSeconds(0.1f));

            player.ragdolled = false;
        }
コード例 #4
0
 public void OnSkeletonAvailable(UMAData umaData)
 {
     if (!umaData.gameObject.GetComponent <UMAPhysicsAvatar> ())
     {
         UMAPhysicsAvatar physicsAvatar = umaData.gameObject.AddComponent <UMAPhysicsAvatar>();
         physicsAvatar.simplePlayerCollider        = simplePlayerCollider;
         physicsAvatar.enableColliderTriggers      = enableColliderTriggers;
         physicsAvatar.UpdateTransformAfterRagdoll = UpdateTransformAfterRagdoll;
         physicsAvatar.ragdollLayer = ragdollLayer;
         physicsAvatar.playerLayer  = playerLayer;
         physicsAvatar.elements     = PhysicsElements;
         physicsAvatar.CreatePhysicsObjects();
     }
 }
コード例 #5
0
        /// <summary>
        /// Applies the data to a Unity mesh.
        /// </summary>
        /// <param name="renderer">Target renderer.</param>
        /// <param name="skeleton">Skeleton.</param>
        public void ApplyDataToUnityMesh(SkinnedMeshRenderer renderer, UMASkeleton skeleton)
        {
            if (renderer == null)
            {
                if (Debug.isDebugBuild)
                {
                    Debug.LogError("Renderer is null!");
                }
                return;
            }

            CreateTransforms(skeleton);

            Mesh mesh = renderer.sharedMesh;

#if UNITY_EDITOR
#if UNITY_2018_3_OR_NEWER
            if (UnityEditor.PrefabUtility.IsAddedComponentOverride(renderer))
            {
                if (Debug.isDebugBuild)
                {
                    Debug.LogError("Cannot apply changes to prefab!");
                }
            }
#else
            if (UnityEditor.PrefabUtility.IsComponentAddedToPrefabInstance(renderer))
            {
                if (Debug.isDebugBuild)
                {
                    Debug.LogError("Cannot apply changes to prefab!");
                }
            }
#endif
            if (UnityEditor.AssetDatabase.IsSubAsset(mesh))
            {
                if (Debug.isDebugBuild)
                {
                    Debug.LogError("Cannot apply changes to asset mesh!");
                }
            }
#endif
            mesh.subMeshCount = 1;
            mesh.triangles    = new int[0];

            if (OwnSharedBuffers())
            {
                ApplySharedBuffers(mesh);
            }
            else
            {
                mesh.vertices    = vertices;
                mesh.boneWeights = unityBoneWeights != null ? unityBoneWeights : UMABoneWeight.Convert(boneWeights);
                mesh.normals     = normals;
                mesh.tangents    = tangents;
                mesh.uv          = uv;
                mesh.uv2         = uv2;
                mesh.uv3         = uv3;
                mesh.uv4         = uv4;
                mesh.colors32    = colors32;
            }
            mesh.bindposes = bindPoses;

            var subMeshCount = submeshes.Length;
            mesh.subMeshCount = subMeshCount;
            for (int i = 0; i < subMeshCount; i++)
            {
                bool sharedBuffer = false;
                for (int j = 0; j < gSubmeshTris.Length; j++)
                {
                    if (gSubmeshTriIndices[j] == i)
                    {
                        sharedBuffer = true;
                        mesh.SetTriangles(gSubmeshTris[j], i);
                        gSubmeshTriIndices[j] = UNUSED_SUBMESH;
                        break;
                    }
                }

                if (!sharedBuffer)
                {
                    mesh.SetTriangles(submeshes[i].triangles, i);
                }
            }

            //Apply the blendshape data from the slot asset back to the combined UMA unity mesh.
            #region Blendshape
            mesh.ClearBlendShapes();
            if (blendShapes != null && blendShapes.Length > 0)
            {
                for (int shapeIndex = 0; shapeIndex < blendShapes.Length; shapeIndex++)
                {
                    if (blendShapes [shapeIndex] == null)
                    {
                        //Debug.LogError ("blendShapes [shapeIndex] == null!");
                        //No longer an error, this will be null if the blendshape got baked.
                        break;
                    }

                    for (int frameIndex = 0; frameIndex < blendShapes[shapeIndex].frames.Length; frameIndex++)
                    {
                        //There might be an extreme edge case where someone has the same named blendshapes on different meshes that end up on different renderers.
                        string name = blendShapes[shapeIndex].shapeName;

                        float     frameWeight   = blendShapes[shapeIndex].frames[frameIndex].frameWeight;
                        Vector3[] deltaVertices = blendShapes[shapeIndex].frames[frameIndex].deltaVertices;
                        Vector3[] deltaNormals  = blendShapes[shapeIndex].frames[frameIndex].deltaNormals;
                        Vector3[] deltaTangents = blendShapes[shapeIndex].frames[frameIndex].deltaTangents;

                        if (UMABlendFrame.isAllZero(deltaNormals))
                        {
                            deltaNormals = null;
                        }

                        if (UMABlendFrame.isAllZero(deltaTangents))
                        {
                            deltaTangents = null;
                        }

                        mesh.AddBlendShapeFrame(name, frameWeight, deltaVertices, deltaNormals, deltaTangents);
                    }
                }
            }
            #endregion

            mesh.RecalculateBounds();
            renderer.bones      = bones != null ? bones : skeleton.HashesToTransforms(boneNameHashes);
            renderer.sharedMesh = mesh;
            renderer.rootBone   = rootBone;

            if (clothSkinning != null && clothSkinning.Length > 0)
            {
                Cloth cloth = renderer.GetComponent <Cloth>();
                if (cloth != null)
                {
                    GameObject.DestroyImmediate(cloth);
                    cloth = null;
                }

                cloth = renderer.gameObject.AddComponent <Cloth>();
                UMAPhysicsAvatar physicsAvatar = renderer.gameObject.GetComponentInParent <UMAPhysicsAvatar>();
                if (physicsAvatar != null)
                {
                    cloth.sphereColliders  = physicsAvatar.SphereColliders.ToArray();
                    cloth.capsuleColliders = physicsAvatar.CapsuleColliders.ToArray();
                }

                cloth.coefficients = clothSkinning;
            }
        }
コード例 #6
0
        // Update is called once per frame
        void Update()
        {
            //if left mouse button clicked
            if (Input.GetMouseButtonDown(0))
            {
                //Get a ray going from the camera through the mouse cursor
                Ray ray = currentCamera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));

                //check if the ray hits a physic collider
                RaycastHit hit;                 //a local variable that will receive the hit info from the Raycast call below
                if (Physics.Raycast(ray, out hit, 100f, layers))
                {
                    //check if the raycast target has a rigid body (belongs to the ragdoll)
                    if (hit.rigidbody != null)
                    {
                        Transform avatar = hit.rigidbody.transform.root;                         // this need to search more intelligently, only works
                        //find the RagdollHelper component and activate ragdolling
                        UMAPhysicsAvatar player = avatar.GetComponent <UMAPhysicsAvatar>();
                        //if(player == null)
                        //	player = avatar.GetComponentInChildren<RagdollPlayer>();
                        if (player)
                        {
                            player.ragdolled = true;
                        }

                        //set the impact target to whatever the ray hit
                        impactTarget = hit.rigidbody;

                        //impact direction also according to the ray
                        impact = ray.direction * 2.0f;

                        //impactTarget.AddForce(impact,ForceMode.VelocityChange);

                        //the impact will be reapplied for the next 100ms
                        //to make the connected objects follow even though the simulated body joints
                        //might stretch
                        impactEndTime = Time.time + 0.1f;
                    }
                }
            }

            if (Input.GetMouseButtonDown(1))
            {
                //Get a ray going from the camera through the mouse cursor
                Ray ray = currentCamera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));

                //check if the ray hits a physic collider
                RaycastHit hit;                 //a local variable that will receive the hit info from the Raycast call below
                if (Physics.Raycast(ray, out hit, 100f, layers))
                {
                    //check if the raycast target has a rigid body (belongs to the ragdoll)
                    if (hit.rigidbody != null)
                    {
                        Transform avatar = hit.rigidbody.transform.root;                         // this need to search more intelligently, only works
                        //find the RagdollHelper component and activate ragdolling
                        UMAPhysicsAvatar player = avatar.GetComponent <UMAPhysicsAvatar>();
                        if (player == null)
                        {
                            player = avatar.GetComponentInChildren <UMAPhysicsAvatar>();
                        }

                        if (player)
                        {
                            player.ragdolled = false;
                        }
                    }
                }
            }

            if (Input.GetMouseButtonDown(2))
            {
                //Get a ray going from the camera through the mouse cursor
                Ray ray = currentCamera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));

                //check if the ray hits a physic collider
                RaycastHit hit;                 //a local variable that will receive the hit info from the Raycast call below
                if (Physics.Raycast(ray, out hit, 100f, layers))
                {
                    //check if the raycast target has a rigid body (belongs to the ragdoll)
                    if (hit.rigidbody != null)
                    {
                        Transform avatar = hit.rigidbody.transform.root;                         // this need to search more intelligently, only works
                        //find the RagdollHelper component and activate ragdolling
                        UMAPhysicsAvatar player = avatar.GetComponent <UMAPhysicsAvatar>();
                        if (player == null)
                        {
                            player = avatar.GetComponentInChildren <UMAPhysicsAvatar>();
                        }
                        if (player)
                        {
                            StartCoroutine(TimedRagdoll(hit));
                        }

                        //set the impact target to whatever the ray hit
                        impactTarget = hit.rigidbody;

                        //impact direction also according to the ray
                        impact = ray.direction * 1.0f;

                        //impactTarget.AddForce(impact,ForceMode.VelocityChange);

                        //the impact will be reapplied for the next 100ms
                        //to make the connected objects follow even though the simulated body joints
                        //might stretch
                        impactEndTime = Time.time + 0.1f;
                    }
                }
            }

            //Check if we need to apply an impact
            if (Time.time < impactEndTime)
            {
                impactTarget.AddForce(impact, ForceMode.VelocityChange);
            }
        }
コード例 #7
0
    void Update()
    {
        if (connected && lastMood != mood)
        {
            lastMood = mood;
            switch (mood)
            {
            case Mood.Neutral:
                expression.leftMouthSmile_Frown  = 0;
                expression.rightMouthSmile_Frown = 0;
                expression.leftEyeOpen_Close     = 0;
                expression.rightEyeOpen_Close    = 0;
                expression.midBrowUp_Down        = 0;
                expression.leftBrowUp_Down       = 0;
                expression.rightBrowUp_Down      = 0;
                expression.rightUpperLipUp_Down  = 0;
                expression.leftUpperLipUp_Down   = 0;
                expression.rightLowerLipUp_Down  = 0;
                expression.leftLowerLipUp_Down   = 0;
                expression.mouthNarrow_Pucker    = 0;
                expression.jawOpen_Close         = 0;
                expression.noseSneer             = 0;

                break;

            case Mood.Happy:
                expression.leftMouthSmile_Frown  = 0.7f;
                expression.rightMouthSmile_Frown = 0.7f;
                expression.leftEyeOpen_Close     = 0.3f;
                expression.rightEyeOpen_Close    = 0.3f;
                expression.midBrowUp_Down        = 0.4f;
                break;

            case Mood.Smiley:
                expression.leftMouthSmile_Frown  = 0.7f;
                expression.rightMouthSmile_Frown = 0.7f;
                expression.leftEyeOpen_Close     = 0.3f;
                expression.rightEyeOpen_Close    = 0.3f;
                expression.midBrowUp_Down        = 0.4f;
                expression.leftLowerLipUp_Down   = -0.6f;
                expression.rightLowerLipUp_Down  = -0.6f;
                expression.leftUpperLipUp_Down   = -0.6f;
                expression.rightUpperLipUp_Down  = -0.6f;
                break;

            case Mood.Sad:
                expression.leftMouthSmile_Frown  = -0.7f;
                expression.rightMouthSmile_Frown = -0.7f;
                expression.leftEyeOpen_Close     = -0.3f;
                expression.rightEyeOpen_Close    = -0.3f;
                expression.midBrowUp_Down        = -0.4f;
                break;

            case Mood.Surprised:
                expression.leftMouthSmile_Frown  = 0f;
                expression.rightMouthSmile_Frown = 0f;
                expression.midBrowUp_Down        = 1f;
                expression.leftBrowUp_Down       = 1f;
                expression.rightBrowUp_Down      = 1f;
                expression.rightUpperLipUp_Down  = 0;
                expression.leftUpperLipUp_Down   = 0;
                expression.rightLowerLipUp_Down  = 0;
                expression.leftLowerLipUp_Down   = 0;
                expression.mouthNarrow_Pucker    = -1f;
                expression.jawOpen_Close         = 0.8f;
                expression.noseSneer             = -0.3f;
                expression.leftEyeOpen_Close     = 1f;
                expression.rightEyeOpen_Close    = 1f;
                break;

            case Mood.Angry:
                expression.leftMouthSmile_Frown  = -0.3f;
                expression.rightMouthSmile_Frown = -0.3f;
                expression.midBrowUp_Down        = -1f;
                expression.leftBrowUp_Down       = 1f;
                expression.rightBrowUp_Down      = 1f;
                expression.rightUpperLipUp_Down  = 0.7f;
                expression.leftUpperLipUp_Down   = 0.7f;
                expression.rightLowerLipUp_Down  = -0.7f;
                expression.leftLowerLipUp_Down   = -0.7f;
                expression.mouthNarrow_Pucker    = 0.7f;
                expression.jawOpen_Close         = -0.3f;
                expression.noseSneer             = 0.3f;
                expression.leftEyeOpen_Close     = -0.2f;
                expression.rightEyeOpen_Close    = -0.2f;
                break;
            }
        }

        //if(!enabled && !ready && Time.time < 30){
        //	readyTimer += Time.deltaTime;
        //	if(readyTimer >=5){
        //		ready = true;
        //	}
        //	}

        if (connected && !enabled)
        {
            //  Debug.Log("NPC is Looking  to connect");
            if (wayPoints == null || wayPoints.Count <= 0)
            {
                wayPoints = new List <Transform>();
                //	    Debug.Log("NPC is Looking for waypoints");
                foreach (Transform wp in transform.parent.GetChild(0))
                {
                    wayPoints.Add(wp);
                }
            }

            if (randomPosition)
            {
                int     randomCount = Random.Range(0, wayPoints.Count);
                Vector3 newPos      = wayPoints[randomCount].position;
                //	Debug.Log("Npc spawed at "+ newPos);
                newPos.x += transform.localPosition.x;
                newPos.z += transform.localPosition.z;
                gameObject.transform.position = newPos;

                Debug.Log(gameObject + " spawned at" + wayPoints[randomCount].gameObject);
                Random.seed = System.DateTime.Now.Millisecond;
            }

            if (agent != null)
            {
                agent = GetComponent <UnityEngine.AI.NavMeshAgent>();
            }
            if (agent == null)
            {
                agent = gameObject.AddComponent <UnityEngine.AI.NavMeshAgent>();
            }
            agent.transform.position = gameObject.transform.position;
            agent.speed       = 0f;
            agent.autoBraking = false;
            //agent.angularSpeed = 100f;
            // agent.updatePosition = false;
            //    agent.updateRotation = true;
            agent.stoppingDistance = 1;

            anim = GetComponent <Animator>();
            //anim.applyRootMotion =true;

            avatar   = GetComponent <DynamicCharacterAvatar>();
            p_Avatar = GetComponent <UMAPhysicsAvatar>();
            switch (avatar.umaRecipe.name)
            {
            case "HumanFemale Base Recipe":
                bool female = true;
                anim.SetBool("Female", true);
                break;

            case "HumanMale Base Recipe":
                bool male = true;
                break;
            }

            armature = gameObject;

            // Disabling auto-braking allows for continuous movement
            // between points (ie, the agent doesn't slow down as it
            // approaches a destination point).

            enabled = true;

            GotoNextPoint();
        }

        if (isIdle)
        {
            animTimer += Time.deltaTime;
        }
        if (enabled)
        {
            if (agent.remainingDistance < 6f && !agent.pathPending)
            {
                // Agent has reached a waypoint Choose the next destination point when the agent gets
                // close to the current one.
                //!agent.pathPending && agent.remainingDistance < 6f

                if (animRandom == 1)
                {
                    animRandom = Random.Range(3, 20);
                    StateSwitch("idle");
                    Debug.Log(this.gameObject + " reached destination.");
                    agent.Stop();
                    //agent.isStopped = true;
                }

                if (animRandom != 1 && animTimer > animRandom)
                {
                    animTimer  = 0;
                    animRandom = 1;
                    GotoNextPoint();
                }
            }

            if (!isMoving && !agent.pathPending && agent.hasPath)
            {
                StateSwitch("moving");
                isMoving = true;
                Debug.Log(this.gameObject + " is going to " + wayPoints[destPoint]);
            }
        }
    }
コード例 #8
0
        // Update is called once per frame
        void Update()
        {
            //if left mouse button clicked
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                UMAPhysicsAvatar[] components = GameObject.FindObjectsOfType <UMAPhysicsAvatar>();
                foreach (var player in components)
                {
                    if (player.ragdolled)
                    {
                        player.ragdolled = false;
                    }
                }
            }
            if (Input.GetMouseButtonDown(0))
            {
                AudioSource src = gameObject.GetComponent <AudioSource>();
                if (src != null)
                {
                    src.PlayOneShot(Bang);
                }
                //Get a ray going from the camera through the mouse cursor
                Ray ray = currentCamera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));


                //check if the ray hits a physic collider
                RaycastHit hit;                 //a local variable that will receive the hit info from the Raycast call below
                if (Physics.Raycast(ray, out hit, 100f, layers))
                {
                    //check if the raycast target has a rigid body (belongs to the ragdoll)
                    if (hit.rigidbody != null)
                    {
                        Transform avatar = hit.rigidbody.transform.root;                         // this need to search more intelligently, only works
                        //find the RagdollHelper component and activate ragdolling
                        UMAPhysicsAvatar player = avatar.GetComponent <UMAPhysicsAvatar>();
                        //if(player == null)
                        //	player = avatar.GetComponentInChildren<RagdollPlayer>();
                        if (player)
                        {
                            if (Blood != null)
                            {
                                GameObject bloodEmitter = GameObject.Instantiate(Blood, hit.point, Quaternion.identity);
                            }
                            if (!player.ragdolled)
                            {
                                hits++;
                                if (hits == 5)
                                {
                                    StartCoroutine(PlayHit(KillingSpree));
                                }
                                else
                                {
                                    AnnounceHit(hit);
                                }
                            }
                            player.ragdolled = true;
                        }

                        //set the impact target to whatever the ray hit
                        impactTarget = hit.rigidbody;

                        //impact direction also according to the ray
                        impact = ray.direction * 2.0f;

                        //impactTarget.AddForce(impact,ForceMode.VelocityChange);

                        //the impact will be reapplied for the next 100ms
                        //to make the connected objects follow even though the simulated body joints
                        //might stretch
                        impactEndTime = Time.time + 0.1f;
                    }
                }
            }

            if (Input.GetMouseButtonDown(1))
            {
                //Get a ray going from the camera through the mouse cursor
                Ray ray = currentCamera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));

                //check if the ray hits a physic collider
                RaycastHit hit;                 //a local variable that will receive the hit info from the Raycast call below
                if (Physics.Raycast(ray, out hit, 100f, layers))
                {
                    //check if the raycast target has a rigid body (belongs to the ragdoll)
                    if (hit.rigidbody != null)
                    {
                        Transform avatar = hit.rigidbody.transform.root;                         // this need to search more intelligently, only works
                        //find the RagdollHelper component and activate ragdolling
                        UMAPhysicsAvatar player = avatar.GetComponent <UMAPhysicsAvatar>();
                        if (player == null)
                        {
                            player = avatar.GetComponentInChildren <UMAPhysicsAvatar>();
                        }

                        if (player)
                        {
                            player.ragdolled = false;
                        }
                    }
                }
            }

            if (Input.GetMouseButtonDown(2))
            {
                //Get a ray going from the camera through the mouse cursor
                Ray ray = currentCamera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));

                //check if the ray hits a physic collider
                RaycastHit hit;                 //a local variable that will receive the hit info from the Raycast call below
                if (Physics.Raycast(ray, out hit, 100f, layers))
                {
                    //check if the raycast target has a rigid body (belongs to the ragdoll)
                    if (hit.rigidbody != null)
                    {
                        Transform avatar = hit.rigidbody.transform.root;                         // this need to search more intelligently, only works
                        //find the RagdollHelper component and activate ragdolling
                        UMAPhysicsAvatar player = avatar.GetComponent <UMAPhysicsAvatar>();
                        if (player == null)
                        {
                            player = avatar.GetComponentInChildren <UMAPhysicsAvatar>();
                        }
                        if (player)
                        {
                            StartCoroutine(TimedRagdoll(hit));
                        }

                        //set the impact target to whatever the ray hit
                        impactTarget = hit.rigidbody;

                        //impact direction also according to the ray
                        impact = ray.direction * 1.0f;

                        //impactTarget.AddForce(impact,ForceMode.VelocityChange);

                        //the impact will be reapplied for the next 100ms
                        //to make the connected objects follow even though the simulated body joints
                        //might stretch
                        impactEndTime = Time.time + 0.1f;
                    }
                }
            }

            //Check if we need to apply an impact
            if (Time.time < impactEndTime)
            {
                impactTarget.AddForce(impact, ForceMode.VelocityChange);
            }
        }