help

Severity Code Description Project File String Suppression Status Error CS0034 The “+” operator for operands like “Vector3” and “Vector2” is ambiguous. Assembly-CSharp C:\Users\DayzServ\death or life\Assets\scripts\Player1.cs 26 Active

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player1 : MonoBehaviour
{
    public float speed;

    private Rigidbody rb;
    private Vector2 moveInput;
    private Vector2 moveVelocity;

    void Start()
    {
         rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
        moveVelocity = moveInput.normalized * speed;
    }

    void FixedUpdate()
    {
        rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime);
    }

Remove the moveInput field because you don’t need it as a field, it’s just used once to get everything you need (here: velocity).

Input should be converted to a Vector3 right away (exception: 2D projects). Assuming movement is along horizontal axis X/Z, hence Y is set to 0 so input doesn’t move the object up or down but only forward/backward and left/right.

Then multiply velocity with fixedDeltaTime right away, otherwise you’d have to apply it anywhere you use it.

    void Update()
    {
        var input = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
        moveVelocity = input.normalized * speed * Time.fixedDeltaTime;
    }
    void FixedUpdate()
    {
        rb.MovePosition(rb.position + moveVelocity);
    }

not work

Please do not make duplicate posts.

The complete error message contains everything you need to know to fix the error yourself.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)
  • also possibly useful is the stack trace (all the lines of text in the lower console window)

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly? Are you structuring the syntax correctly? Look for examples!

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

Break up line 26;

  • assign the position to a temporary Vector2
  • add the moveVelocity * Time.deltaTime
  • assign the temporary back to rb.position