suriyun-production / mmorpg-kit-docs

This is document for MMORPG KIT project (https://www.assetstore.unity3d.com/#!/content/110188?aid=1100lGeN)
https://suriyun-production.github.io/mmorpg-kit-docs
49 stars 11 forks source link

Problem when you are close to a target #2362

Closed Niinra closed 7 months ago

Niinra commented 8 months ago

There is a problem when you are very close to a target and you point it towards the enemy. Look for the enemy and this error happens with the projectiles. If you are very close, they somehow try to find the target and this happens in an example video:https://imgur.com/gHwiSkr

The solution that I see possible is to deactivate the auto-targeting when the character is too close, of course if this is possible obviously

insthync commented 8 months ago

I will fix it

insthync commented 7 months ago

Try replace DamageInfoExtensions to

using UnityEngine;

namespace MultiplayerARPG
{
    public static class DamageInfoExtensions
    {
        public static void GetDamagePositionAndRotation(this IDamageInfo damageInfo, BaseCharacterEntity attacker, bool isLeftHand, AimPosition aimPosition, Vector3 stagger, out Vector3 position, out Vector3 direction, out Quaternion rotation)
        {
            if (GameInstance.Singleton.DimensionType == DimensionType.Dimension2D)
            {
                Transform damageTransform = damageInfo.GetDamageTransform(attacker, isLeftHand);
                position = damageTransform.position;
                GetDamageRotation2D(attacker.Direction2D, out rotation);
                direction = attacker.Direction2D;
#if UNITY_EDITOR
                attacker.SetDebugDamage(position, direction, rotation, isLeftHand);
#endif
                return;
            }
            if (aimPosition.type == AimPositionType.Direction)
            {
                position = aimPosition.position;
                rotation = Quaternion.Euler(Quaternion.LookRotation(aimPosition.direction).eulerAngles + stagger);
                direction = rotation * Vector3.forward;
            }
            else
            {
                // NOTE: Allow aim position type `None` here, may change it later
                Transform damageTransform = damageInfo.GetDamageTransform(attacker, isLeftHand);
                position = damageTransform.position;
                GetDamageRotation3D(attacker.transform.forward, position, aimPosition.position, stagger, out rotation);
                direction = rotation * Vector3.forward;
            }
#if UNITY_EDITOR
            attacker.SetDebugDamage(position, direction, rotation, isLeftHand);
#endif
        }

        public static void GetDamageRotation2D(Vector2 aimDirection, out Quaternion rotation)
        {
            rotation = Quaternion.Euler(0, 0, (Mathf.Atan2(aimDirection.y, aimDirection.x) * (180 / Mathf.PI)) + 90);
        }

        public static void GetDamageRotation3D(Vector3 entityForward, Vector3 origin, Vector3 target, Vector3 stagger, out Quaternion rotation)
        {
            Vector3 direction = target - origin;
            if (Vector3.Dot(entityForward, direction) < 0.5f)
            {
                // Not in front of character, so set direction to character forward
                direction = entityForward;
            }
            rotation = Quaternion.Euler(Quaternion.LookRotation(direction).eulerAngles + stagger);
        }
    }
}
Niinra commented 7 months ago

It fixed a little, but it still happens when you are too close, example video:https://imgur.com/0AejcdT

insthync commented 7 months ago

Hmm, try add log message to GetDamageRotation3D function, I want to see a value of Vector3.Dot(entityForward, direction), entityForward, target, origin and direction when it is having issues

Niinra commented 7 months ago

Hmm, try add log message to GetDamageRotation3D function, I want to see a value of Vector3.Dot(entityForward, direction), entityForward, target, origin and direction when it is having issues`

image

Ehm, I don't know if what you were asking for was this debug in this method because I was able to translate but it doesn't show anything. I don't know if I misunderstood you, but it leaves nothing in the console and there was no different behavior, it's still the same despite the change code

insthync commented 7 months ago

Yes, do like that it has nothing shown?

Niinra commented 7 months ago

No, it doesn't throw anything

insthync commented 7 months ago

Try change codes in ShooterPlayerCharacterController -> IsInFront function to

        public virtual bool IsInFront(Vector3 target)
        {
            return Vector3.Dot(_cameraForward, target - EntityTransform.position) > 0.5f;
        }
insthync commented 7 months ago
using UnityEngine;

namespace MultiplayerARPG
{
    public static class DamageInfoExtensions
    {
        public static void GetDamagePositionAndRotation(this IDamageInfo damageInfo, BaseCharacterEntity attacker, bool isLeftHand, AimPosition aimPosition, Vector3 stagger, out Vector3 position, out Vector3 direction, out Quaternion rotation)
        {
            if (GameInstance.Singleton.DimensionType == DimensionType.Dimension2D)
            {
                Transform damageTransform = damageInfo.GetDamageTransform(attacker, isLeftHand);
                position = damageTransform.position;
                GetDamageRotation2D(attacker.Direction2D, out rotation);
                direction = attacker.Direction2D;
#if UNITY_EDITOR
                attacker.SetDebugDamage(position, direction, rotation, isLeftHand);
#endif
                return;
            }
            if (aimPosition.type == AimPositionType.Direction)
            {
                position = aimPosition.position;
                rotation = Quaternion.Euler(Quaternion.LookRotation(aimPosition.direction).eulerAngles + stagger);
                direction = rotation * Vector3.forward;
                if (attacker.IsOwnerClient)
                {
                    Debug.LogError("here " + position + " " + direction + " " + Vector3.Dot(attacker.EntityTransform.forward, direction));
                }
            }
            else
            {
                // NOTE: Allow aim position type `None` here, may change it later
                Transform damageTransform = damageInfo.GetDamageTransform(attacker, isLeftHand);
                position = damageTransform.position;
                GetDamageRotation3D(attacker.transform.forward, position, aimPosition.position, stagger, out rotation);
                direction = rotation * Vector3.forward;
                if (attacker.IsOwnerClient)
                {
                    Debug.LogError("here2 " + position + " " + direction + " " + Vector3.Dot(attacker.EntityTransform.forward, direction));
                }
            }
#if UNITY_EDITOR
            attacker.SetDebugDamage(position, direction, rotation, isLeftHand);
#endif
        }

        public static void GetDamageRotation2D(Vector2 aimDirection, out Quaternion rotation)
        {
            rotation = Quaternion.Euler(0, 0, (Mathf.Atan2(aimDirection.y, aimDirection.x) * (180 / Mathf.PI)) + 90);
        }

        public static void GetDamageRotation3D(Vector3 entityForward, Vector3 origin, Vector3 target, Vector3 stagger, out Quaternion rotation)
        {
            Vector3 direction = target - origin;
            if (Vector3.Dot(entityForward, direction) < 0.5f)
            {
                // Not in front of character, so set direction to character forward
                direction = entityForward;
            }
            rotation = Quaternion.Euler(Quaternion.LookRotation(direction).eulerAngles + stagger);
        }
    }
}

Tell me the value when the problem is occurring.

insthync commented 7 months ago

Don't do anything yet, I am currently have time to try it and fix it, I will tell you when it was fixed.

insthync commented 7 months ago

Okay, try this, change codes in ShooterPlayerCharacterController -> IsInFront function to


        public virtual bool IsInFront(Vector3 target)
        {
            // Get aim position direction
            AimPosition aimPosition = PlayingCharacterEntity.GetAttackAimPosition(ref _isLeftHandAttacking, target);
            switch (aimPosition.type)
            {
                case AimPositionType.Direction:
                    // Check that the direction is in front of character or not
                    return Vector3.Dot(aimPosition.direction, EntityTransform.forward) > 0.5f;
            }
            // 2D mode?
            return true;
        }

Or this

        public virtual bool IsInFront(Vector3 target)
        {
            // Get aim position direction
            AimPosition aimPosition = PlayingCharacterEntity.GetAttackAimPosition(ref _isLeftHandAttacking, target);
            switch (aimPosition.type)
            {
                case AimPositionType.Direction:
                    // Check that the direction is in front of character or not
                    return Vector3.Angle(aimPosition.direction, EntityTransform.forward) < 115f;
            }
            // 2D mode?
            return true;
        }
insthync commented 7 months ago

Reopen if it is not working :)