private float DifferenceBetweenGivenFacingAndCurrentFacing(Vector2 xzVector)
        {
            float targetTheta   = Mathf.Atan2(xzVector.y, xzVector.x);
            var   currentVector = transform.forward.JustXZ().normalized;
            float currentTheta  = Mathf.Atan2(currentVector.y, currentVector.x);

            return(MeatPuppetToolKit.AngleBetweenThetas(targetTheta, currentTheta));
        }
        private void UpdateNavAgent()
        {
            if (!HasMoveTarget() || ReachedTarget())
            {
                navAgent.enabled = false;
            }
            //else if (!IsGrounded()) {
            //	navAgent.enabled = false;
            //}
            else
            if (moveInputType == InputType.Direction)
            {
                navAgent.enabled = false;
            }
            else
            {
                // Unity's NavAgent has a few issues:
                // 1. the navagent has its own position and sometimes it is not the same as the transform
                // 2. if the puppet is off the navmesh, the navagent will teleport to the closest navmesh point

                // for issue #1 -> check that the two positions are within some threshold
                if (!MeatPuppetToolKit.PointAndPointWithinDistanceOfEachOther(navAgent.nextPosition, transform.position,
                                                                              parentPuppet.configuration.navAgentAllowedDistance))
                {
                    //if ((navAgent.nextPosition - transform.position).sqrMagnitude >
                    //		parentPuppet.configuration.navAgentAllowedDistance * parentPuppet.configuration.navAgentAllowedDistance) {

                    // if they are not -> teleport the navagent to our position
                    navAgent.enabled      = false;
                    navAgent.nextPosition = transform.position;
                }

                navAgent.enabled          = true;
                navAgent.destination      = GetMoveTargetPoint();
                navAgent.stoppingDistance = parentPuppet.configuration.stoppingDistance;

                // Force the simulated navAgent to maintain the same speed as the actual body
                navAgent.velocity = parentPuppet.Rigidbody.velocity;

                navAgent.speed        = GetTopSpeed();
                navAgent.acceleration = parentPuppet.configuration.moveAcceleration * Time.fixedDeltaTime;

                // for issue #2 -> check the two positions AGAIN, and if they are not the same at this point
                //    we can assume that the puppet is stuck off the navmesh
                if (!MeatPuppetToolKit.PointAndPointWithinDistanceOfEachOther(navAgent.nextPosition, transform.position,
                                                                              parentPuppet.configuration.navAgentAllowedDistance))
                {
                    //if ((navAgent.nextPosition - transform.position).sqrMagnitude >
                    //		parentPuppet.configuration.navAgentAllowedDistance * parentPuppet.configuration.navAgentAllowedDistance) {

                    // TODO: if they are not -> teleport the navagent to our position
                }
            }
        }
Example #3
0
        private void ConfigureRigidBody()
        {
            Rigidbody = GetComponent <Rigidbody>();
            if (Rigidbody == null)
            {
                Rigidbody = gameObject.AddComponent <Rigidbody>();
            }
            // assign some properties:
            Rigidbody.isKinematic = false;
            Rigidbody.angularDrag = 1;

            var   capsuleCollider = Collider as CapsuleCollider;
            float capsuleRadius   = capsuleCollider.radius * transform.localScale.z;
            float capsuleHeight   = capsuleCollider.height * transform.localScale.y;

            Rigidbody.mass        = bodyDimensions.density * MeatPuppetToolKit.VolumeOfCapsule(capsuleHeight, capsuleRadius);
            Rigidbody.drag        = 1;
            Rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
        }
        //public float GetSquaredDistanceToTarget() {
        //	switch (moveInputType) {
        //		case InputType.Point:
        //		case InputType.Transform:
        //			var currentPosition = transform.position;
        //			var targetPosition = GetMoveTargetPoint();
        //			var difference = targetPosition - currentPosition;
        //			return difference.sqrMagnitude;

        //		default:
        //		case InputType.Direction:
        //			return -1;
        //	}
        //}

        public bool IsDistanceToTargetLessThan(float value)
        {
            switch (moveInputType)
            {
            case InputType.Point:
            case InputType.Transform:
                var currentPosition = transform.position;
                var targetPosition  = GetMoveTargetPoint();

                // treat the y-axis separate; allow a bit more leanency
                float y = currentPosition.y - currentPosition.y;
                if (Mathf.Abs(y) > 0.3f)
                {
                    return(false);
                }
                return(MeatPuppetToolKit.PointAndPointWithinDistanceOfEachOther(currentPosition.JustXZ(), targetPosition.JustXZ(), value));

            default:
            case InputType.Direction:
                return(false);
            }
        }