Operator '+' cannot be applied to operands of type 'Vector3' and 'Vector3'

Hi there,

I’m getting an error in Visual Studio when I try and add the two Vectors together (8:22 in the video) saying that it can’t add two Vector3 types.

This is my PlayerBaseState code

public abstract class PlayerBaseState : State
{
    protected PlayerStateMachine stateMachine;

    public PlayerBaseState(PlayerStateMachine stateMachine)
    {
        this.stateMachine = stateMachine;
    }

    protected void Move(Vector3 motion, float deltaTime)
    {
        stateMachine.CharacterController.Move((motion + stateMachine.ForceReceiver.Movement) * deltaTime);
    }
}

And here’s my ForceReceiver code (the public method at the bottom I added to see if it worked instead of the shorthand Nathan used, but am getting the same error so that doesn’t seem to be the problem

public class ForceReceiver : MonoBehaviour
{
    float verticalVelocity;

    CharacterController characterController;

    public Vector3 Movement => Vector3.up * verticalVelocity;

    void Start()
    {
        characterController= GetComponent<CharacterController>();
    }

    void Update()
    {
        if (verticalVelocity < 0f && characterController.isGrounded)
        {
            verticalVelocity = Physics.gravity.y * Time.deltaTime;
        }
        else
        {
            verticalVelocity += Physics.gravity.y * Time.deltaTime;
        }
    }

    public Vector3 ReturnVerticalMovement()
    {
        Vector3 movement = Vector3.up* verticalVelocity;
        return movement;
    }
}

Have spent an hour Googling it to no avail, any help would be appreciated! I’m sure it’s just something silly I’m overlooking…

Thanks in advance

1 Like

Never mind, I’d somehow replaced the UnityEngine namespace with the System.Numerics namespace!

Not entirely sure how but it’s all making sense again!

1 Like

Modern IDEs try to ‘help out’ and sometimes they don’t. Visual Studio has this very helpful (but not always) ability to determine what the type is you are using but don’t have a using for yet, and adds it for you. Sometimes it’s the wrong one

1 Like

I use JetBrains Rider, and this used to happen to me a lot. Finally found a setting where I can tell JetBrains not to autosuggest certain namespaces… in this case, I told it that both System.Random and System.Numerics were off limites.

1 Like

Interesting, I wonder if Visual Studio has a similar setting. Will look into it, thanks Brian

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms