简体   繁体   中英

Problem with getting access to values in Unity C#

I Can't get values from variable(Vector2).

My goal is send float variable from PlayerAttack to PlayerAttacking.

public class PlayerAttack : MonoBehaviour
{
    [Header("Components")]
    [SerializeField]
    private Rigidbody2D _Player_Rigidbody;
    [SerializeField]
    private Camera _Main_Camera;

    public Transform target;
    public Vector2 offset = Vector2.zero;

    private PlayerState CurrentPlayerState;
    void Start()
    {

    }

    void Update()
    {
        Vector3 Mouse = Input.mousePosition;
        Vector3 ScreenPoint = _Main_Camera.WorldToScreenPoint(target.localPosition);

        // Offset  - help you realize 4 ways attack system
        offset = new Vector2(Mouse.x - ScreenPoint.x, Mouse.y - ScreenPoint.y);
        Debug.Log("PlayerAttack Log: " + offset.ToString());
        FindPlayerState(CurrentPlayerState);

        if(VirtualInputManager.Instance.Attack && CurrentPlayerState.ToString() != aro_game.PlayerState.attack.ToString())
        {
            StartCoroutine(AttackCo());
            //Debug.Log("Virtual Input Manager said true and Current state doesnt equal attack");
            Debug.Log("ATTACK LOGIC");
        }
    }
    public PlayerState FindPlayerState(PlayerState temp_state)
    {
        GameObject Object = GameObject.Find("Player");
        PlayerMovement PlayerState = Object.GetComponent<PlayerMovement>();

        temp_state = PlayerState.CurrentPlayerState;

        return temp_state;
    }
    private IEnumerator AttackCo()
    {
        CurrentPlayerState = PlayerState.attack;
        yield return new WaitForSeconds(0.65f);
        CurrentPlayerState = PlayerState.idle;
    }
}

then I created a method to get values from this variable.(This is CharacterState code):

protected void FindAttackAngle(Vector2 temp_angle)
    {
        GameObject Object = GameObject.Find("Player");
        PlayerAttack attackangle = Object.GetComponent<PlayerAttack>();
        temp_angle = attackangle.offset;
        Debug.Log("CharacterState Log: " + temp_angle.ToString());
    }

And if attack animation starts the PlayerAttacking script inherited from CharacterState must start, too.

public class PlayerAttacking : CharacterState
{
    private PlayerState _state;
    private Vector2 _attackangle = Vector2.zero;
    public override void OnStateEnter(Animator animator, AnimatorStateInfo animatorStateInfo, int layerIndex)
    {
        FindAttackAngle(_attackangle);
        Debug.Log("PlayerAttackingAnimation Log:" + _attackangle.ToString());
        CheckPlayerState(_state);
        if (_state != PlayerState.attack)
        {
            // Front attack
            //Debug.Log(_attackangle.ToString());
            if ((_attackangle.x < 1f && _attackangle.y < 1f) && (_attackangle.x > -1 && _attackangle.y > 1))
            {
                Debug.Log("FRONT ATTACK");
                animator.SetBool("!Attack", true);
            }
            //Back attack
            if ((_attackangle.x > -1f && _attackangle.y > -1f) && (_attackangle.x < 1 && _attackangle.y < -1f))
            {
                Debug.Log("BACK ATTACK");
                animator.SetBool("!Attack", true);
            }
            //Left attack
            if ((_attackangle.x < -1f && _attackangle.y < -1f) && (_attackangle.x > -1f && _attackangle.y > 1f))
            {
                Debug.Log("LEFT ATTACK");
                animator.SetBool("!Attack", true);
            }
            //Right attack
            if ((_attackangle.x > 1f && _attackangle.y > -1f) && (_attackangle.x < 1f && _attackangle.y < 1f))
            {
                Debug.Log("RIGHT ATTACK");
                animator.SetBool("!Attack", true);
            }
        }
    }
    public override void OnStateUpdate(Animator animator, AnimatorStateInfo animatorStateInfo, int layerIndex)
    {
    }
    public override void OnStateExit(Animator animator, AnimatorStateInfo animatorStateInfo, int layerIndex)
    {
        animator.SetBool("!Attack", false);
        animator.SetBool("Attack", false);
    }
}

You can see the Debug Log here .

Why is the last Log equal to 0:0? And how can I change this value if the issue is not happening due to an error in my code?

If you need more details, ask your questions in the comments section pleas.

The problem occurs when you call FindAttackAngle(_attackangle) The reason is that Vector2 is a structure, not an object. You can read about the difference between passing structures as parameters versus passing objects as parameters here

The TLDR version of that linked article is that if you pass a structure variable into a method, it is passed by value . That means that if you change the structure's fields inside the method, the structure will still have its original values outside of the method. Objects, by contrast, are passed by reference , which is why changes to their properties inside the method will be reflected outside the method (which is what you're expecting to happen).

To fix this, specify that you want to pass by reference like this:

// Added the ref keyword on the parameter
protected void FindAttackAngle(ref Vector2 temp_angle)
{
    ...
}

Then when you call FindAttackAngle

// Added the ref keyword
FindAttackAngle(ref _attackangle);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM