Exemple #1
0
 public MotionState GetMotion(string name)
 {
     for (int i = 0; i < this.m_Motions.Count; i++)
     {
         MotionState motion = this.m_Motions[i];
         if (motion.FriendlyName == name)
         {
             return(motion);
         }
     }
     return(null);
 }
Exemple #2
0
        public void SetMotionEnabled(string name, bool state)
        {
            MotionState motion = GetMotion(name);

            if (motion != null)
            {
                motion.enabled = state;
                if (!state)
                {
                    motion.StopMotion(true);
                }
            }
        }
        private void RemoveMotion(ReorderableList list)
        {
            MotionState motion = m_Motions.GetArrayElementAtIndex(list.index).objectReferenceValue as MotionState;

            m_Motions.GetArrayElementAtIndex(list.index).objectReferenceValue = null;
            m_Motions.DeleteArrayElementAtIndex(list.index);
            UnityEngine.Object.DestroyImmediate(motion, true);
            list.index = list.index - 1;
            if (list.index == -1 && m_Motions.arraySize > 0)
            {
                list.index = 0;
            }
            EditorUtility.SetDirty(this.m_Controller);
        }
        private void OnEnable()
        {
            this.m_Controller = target as ThirdPersonController;
            this.m_GameObject = this.m_Controller.gameObject;
            this.m_Script     = serializedObject.FindProperty("m_Script");
            this.m_Motions    = serializedObject.FindProperty("m_Motions");

            this.m_MotionList = new ReorderableList(serializedObject, this.m_Motions, true, true, true, true)
            {
                drawHeaderCallback            = new ReorderableList.HeaderCallbackDelegate(DrawMotionHeader),
                drawElementCallback           = new ReorderableList.ElementCallbackDelegate(DrawMotion),
                onAddDropdownCallback         = new ReorderableList.AddDropdownCallbackDelegate(AddMotion),
                onRemoveCallback              = new ReorderableList.RemoveCallbackDelegate(RemoveMotion),
                onSelectCallback              = new ReorderableList.SelectCallbackDelegate(SelectMotion),
                drawElementBackgroundCallback = new ReorderableList.ElementCallbackDelegate(DrawMotionBackground)
            };
            int motionIndex = EditorPrefs.GetInt("MotionIndex" + target.GetInstanceID().ToString(), -1);

            if (this.m_MotionList.count > motionIndex)
            {
                this.m_MotionList.index = motionIndex;
                SelectMotion(this.m_MotionList);
            }
            MotionState[] states = this.m_Controller.GetComponents <MotionState> ();
            for (int i = 0; i < states.Length; i++)
            {
                states [i].hideFlags = HideFlags.HideInInspector;
            }


            for (int i = 0; i < this.m_Controller.Motions.Count; i++)
            {
                if (this.m_Controller.Motions[i] != null)
                {
                    if (this.m_Controller.Motions[i].gameObject != this.m_GameObject)
                    {
                        if (ComponentUtility.CopyComponent(this.m_Controller.Motions[i]))
                        {
                            MotionState component = this.m_GameObject.AddComponent(this.m_Controller.Motions[i].GetType()) as MotionState;
                            ComponentUtility.PasteComponentValues(component);
                            this.m_Controller.Motions[i] = component;
                        }
                    }
                }
            }
            EditorApplication.playModeStateChanged += PlayModeState;
        }
Exemple #5
0
        private void OnAnimatorIK(int layer)
        {
            for (int i = 0; i < this.m_Controller.Motions.Count; i++)
            {
                MotionState motion = this.m_Controller.Motions[i];
                if (motion.IsActive && !motion.UpdateAnimatorIK(layer))
                {
                    return;
                }
            }

            if (layer == 0)
            {
                this.m_Animator.SetLookAtPosition(this.m_AimPosition);
                this.m_Animator.SetLookAtWeight(this.m_Weight, this.m_BodyWeight, this.m_HeadWeight, this.m_EyesWeight, this.m_ClampWeight);
            }
        }
Exemple #6
0
        private void FixedUpdate()
        {
            if (!this.m_ControllerActive)
            {
                return;
            }
            for (int i = 0; i < this.m_Motions.Count; i++)
            {
                MotionState motion = this.m_Motions [i];
                if (!motion.isActiveAndEnabled)
                {
                    continue;
                }
                if (!motion.IsActive && (motion.StartType == StartType.Automatic || this.m_ToggleState [motion]))
                {
                    this.TryStartMotion(motion);
                }
                if (motion.IsActive)
                {
                    if (motion.StopType == StopType.Automatic && motion.CanStop())
                    {
                        this.TryStopMotion(motion);
                    }
                }
            }
            this.m_LookRotation = Quaternion.Euler(this.m_Transform.eulerAngles.x, this.m_CameraTransform.eulerAngles.y, this.m_Transform.eulerAngles.z);

            this.m_Velocity = this.m_Rigidbody.velocity;
            if (this.IsGrounded)
            {
                this.m_Velocity.x = this.m_RootMotionForce.x;
                this.m_Velocity.z = this.m_RootMotionForce.z;
                float force = this.m_Animator.GetFloat("Force");
                this.m_Velocity += this.m_Transform.TransformDirection(this.RelativeInput * force);
            }
            this.CheckGround();
            this.CheckStep();
            this.UpdateVelocity();
            this.UpdateFrictionMaterial();
            this.UpdateRotation();
            this.UpdateAnimator();
            this.m_Rigidbody.velocity = this.m_Velocity;
        }
        private void AddMotion(Rect rect, ReorderableList list)
        {
            Type[]      motionTypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(assembly => assembly.GetTypes()).Where(type => typeof(MotionState).IsAssignableFrom(type) && !type.IsAbstract).ToArray();
            GenericMenu menu        = new GenericMenu();

            for (int i = 0; i < motionTypes.Length; i++)
            {
                Type motionType = motionTypes [i];
                menu.AddItem(new GUIContent(motionType.Name), false, delegate() {
                    serializedObject.Update();
                    MotionState motion = m_Controller.gameObject.AddComponent(motionType) as MotionState;
                    motion.hideFlags   = HideFlags.HideInInspector;
                    m_Motions.arraySize++;
                    m_Motions.GetArrayElementAtIndex(m_Motions.arraySize - 1).objectReferenceValue = motion;
                    list.index = m_Motions.arraySize - 1;
                    serializedObject.ApplyModifiedProperties();
                });
            }
            menu.ShowAsContext();
        }
Exemple #8
0
        private void UpdateAnimator()
        {
            for (int i = 0; i < this.m_Motions.Count; i++)
            {
                MotionState motion = this.m_Motions [i];
                if (motion.IsActive && !motion.UpdateAnimator())
                {
                    return;
                }
            }

            this.m_Animator.SetFloat("Forward Input", RelativeInput.z * this.m_SpeedMultiplier, this.m_ForwardDampTime, Time.deltaTime);
            this.m_Animator.SetFloat("Horizontal Input", RelativeInput.x * this.m_SpeedMultiplier, this.m_HorizontalDampTime, Time.deltaTime);
            this.IsMoving = RelativeInput.sqrMagnitude > 0.01f;
            this.m_Animator.SetBool("Moving", this.m_IsMoving);
            if (this.m_IsAiming)
            {
                this.m_Animator.SetFloat("Yaw Input", Normalize(GetSignedAngle(this.m_Transform.rotation, this.m_LookRotation, Vector3.up) * this.m_AimRotation, -180, 180), 0.15f, Time.deltaTime);
            }
        }
Exemple #9
0
        private void UpdateRotation()
        {
            for (int i = 0; i < this.m_Motions.Count; i++)
            {
                MotionState motion = this.m_Motions [i];
                if (motion.IsActive && !motion.UpdateRotation())
                {
                    return;
                }
            }

            Quaternion rotation = this.m_Transform.rotation;

            if (IsAiming)
            {
                rotation = this.m_LookRotation;
            }
            else if (this.m_RawInput.sqrMagnitude > 0.01f)
            {
                rotation = Quaternion.LookRotation(this.m_LookRotation * this.m_RawInput);
            }
            this.m_Transform.rotation = Quaternion.Slerp(this.m_Transform.rotation, rotation, (IsAiming ? this.m_AimRotation : this.m_RotationSpeed) * Time.fixedDeltaTime);
        }
Exemple #10
0
        public void CopyProperties(ThirdPersonController other)
        {
            this.m_DontDestroyOnLoad  = other.m_DontDestroyOnLoad;
            this.m_ForwardInput       = other.m_ForwardInput;
            this.m_HorizontalInput    = other.m_HorizontalInput;
            this.m_SpeedMultiplier    = other.m_SpeedMultiplier;
            this.m_AimType            = other.m_AimType;
            this.m_AimInput           = other.m_AimInput;
            this.m_AimRotation        = other.m_AimRotation;
            this.m_RotationSpeed      = other.m_RotationSpeed;
            this.m_AirSpeed           = other.m_AirSpeed;
            this.m_AirDampening       = other.m_AirDampening;
            this.m_GroundDampening    = other.m_GroundDampening;
            this.m_StepOffset         = other.m_StepOffset;
            this.m_SlopeLimit         = other.m_SlopeLimit;
            this.m_GroundLayer        = other.m_GroundLayer;
            this.m_SkinWidth          = other.m_SkinWidth;
            this.m_IdleFriction       = other.m_IdleFriction;
            this.m_MovementFriction   = other.m_MovementFriction;
            this.m_StepFriction       = other.m_StepFriction;
            this.m_AirFriction        = other.m_AirFriction;
            this.m_ForwardDampTime    = other.m_ForwardDampTime;
            this.m_HorizontalDampTime = other.m_HorizontalDampTime;

            /*this.m_LookOffset = other.m_LookOffset;
             * this.m_BodyWeight = other.m_BodyWeight;
             * this.m_HeadWeight = other.m_HeadWeight;
             * this.m_EyesWeight = other.m_EyesWeight;
             * this.m_ClampWeight = other.m_ClampWeight;*/

            this.m_Motions = new List <MotionState> ();
            for (int i = 0; i < other.Motions.Count; i++)
            {
                MotionState motion = CopyComponent <MotionState> (other.Motions [i], gameObject);
                this.m_Motions.Add(motion);
            }
        }
Exemple #11
0
        private void Update()
        {
            if (!this.m_ControllerActive)
            {
                return;
            }

            if ((Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1)) && EventSystem.current != null && UnityTools.IsPointerOverUI())
            {
                this.m_GUIClick = true;
            }

            if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1))
            {
                this.m_GUIClick = false;
            }

            this.m_RawInput = new Vector3(Input.GetAxis(this.m_HorizontalInput), 0, Input.GetAxis(this.m_ForwardInput));

            switch (this.m_AimType)
            {
            case AimType.Button:
                IsAiming = Input.GetButton(this.m_AimInput) && !this.m_GUIClick;
                break;

            case AimType.Axis:
                float aim = Input.GetAxis(this.m_AimInput);
                if (Mathf.Abs(aim) > 0.01f)
                {
                    IsAiming          = true;
                    this.m_RawInput.x = aim;
                }
                else
                {
                    IsAiming = false;
                }
                break;

            case AimType.Toggle:
                if (Input.GetButtonDown(this.m_AimInput))
                {
                    IsAiming = !IsAiming;
                }
                break;
            }


            for (int j = 0; j < this.m_Motions.Count; j++)
            {
                MotionState motion = this.m_Motions [j];
                if (!motion.isActiveAndEnabled || motion.ConsumeInputOverUI && this.m_GUIClick)
                {
                    continue;
                }
                if (motion.StartType != StartType.Down && motion.StopType != StopType.Toggle || !Input.GetButtonDown(motion.InputName))
                {
                    if (motion.StopType == StopType.Up && Input.GetButtonUp(motion.InputName))
                    {
                        this.TryStopMotion(motion);
                        this.m_ToggleState [motion] = motion.StopType == StopType.Up && motion.IsActive;
                    }
                }
                else if (!motion.IsActive && motion.StartType == StartType.Down)
                {
                    this.TryStartMotion(motion);
                    this.m_ToggleState [motion] = (motion.StopType == StopType.Toggle || motion.StopType == StopType.Up) && motion.IsActive;
                }
                else if (motion.StopType == StopType.Toggle)
                {
                    this.TryStopMotion(motion);
                    this.m_ToggleState [motion] = motion.StopType == StopType.Toggle && motion.IsActive;
                    break;
                }
                if (motion.StartType == StartType.Press && Input.GetButton(motion.InputName))
                {
                    this.TryStartMotion(motion);
                }
            }
        }