Error CS1503 Cannot convert from "UnityEngine.Vector3" to "float"

I’m super new to coding and was looking up some brackeys tutorial to get started. I used the new input system and started going, but the example he showed was sort of 2d in the sense that it only goes up down left and right. So from there I went to his other tutorial on how to do first person movement. So in trying to combine the two, (basically the new input system + character controller) I stumbled on a problem where I’m trying to convert Vector values into floats to then allow me to move my character by a certain amount, mind you I’m doing to this controller and not keyboard.


using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
PlayerControls controls;
private UnityEngine.Vector2 move;
private UnityEngine.Vector2 rotate;

public CharacterController controller;
public float moveSpeed;
public float rotateSpeed;
private void Awake()
{
controls = new PlayerControls();
controls.Player.Move.performed += ctx => move = ctx.ReadValue<UnityEngine.Vector2>();
controls.Player.Move.canceled += ctx => move = UnityEngine.Vector2.zero;
controls.Player.Rotate.performed += ctx => rotate = ctx.ReadValue<UnityEngine.Vector2>();
controls.Player.Rotate.canceled += ctx => rotate = UnityEngine.Vector2.zero;
}
void Update()
{
Cursor.lockState = CursorLockMode.Locked;
//float lookX = move.x
//float lookY = move.y;
UnityEngine.Vector3 m = new UnityEngine.Vector3(transform.right * move.x, 0, transform.forward * move.y); This is the error line
if(m != UnityEngine.Vector3.zero){
controller.Move(m * moveSpeed * Time.deltaTime);
}
//transform.Translate(transform.forward * moveSpeed * Time.deltaTime);
UnityEngine.Vector3 r = new UnityEngine.Vector3(0, rotate.x, 0) * rotateSpeed * Time.deltaTime;
transform.Rotate(r, Space.World);
}
private void OnEnable()
{
controls.Player.Enable();
}
private void OnDisable()
{
controls.Player.Disable();
}
}


So in trying to figure out what I can do to fix this, I took to google and found a number of things but I can’t seem to make them work. One said “Just make it a float because it already is one.” but that didn’t work, so one said “Make the Vector values an array, and make a number of float arrays to for each value.” but I’m not sure how to implement that. If there are any ways that are known for fixing this, I’d really appreciate it.

Your problem is here:

new UnityEngine.Vector3(transform.right * move.x, 0, transform.forward * move.y);

Each argument to the Vector3 constructor should be a simple float value. Instead, you are passing an entire Vector3 to the X argument and the Z argument.

I think you just want this:

Vector3 m = new Vector3(move.x, 0, move.y);

And if you want this to be relative to the current forward direction of your character, you can do this:

Vector3 m = transform.rotation * new Vector3(move.x, 0, move.y);

By the way you don’t need to write “UnityEngine.” in front of Vector3 and Vector2. It’s just extra fluff that makes your code harder to read. The reason you may be getting an error when you don’t include that is because you have this line at the beginning of your file:

using System.Numerics;

You should delete that line. You don’t need it and it causes the ambiguity for Vector3 and Vector2.

So it’s because I’m putting a vector into a vector and trying to change it with “transform”, so wouldn’t the problem lie with the way I’m producing the values in the first place?

controls.Player.Move.performed += ctx => move = ctx.ReadValue<UnityEngine.Vector2>();
controls.Player.Move.canceled += ctx => move = UnityEngine.Vector2.zero;

Should I be storing it as a float instead, and if so, how would I go about doing it?

As for “Vector3 m = new Vector3(move.x, 0, move.y);”, this would be fine if I had a stationary camera to just move forward, back, left, right, but I have a free moving camera.

For the “Vector3 m = transform.rotation * new Vector3(move.x, 0, move.y);”, I was just going to use the character controller to simplify it.

Also, got rid of all the unnecessary stuff that you mentioned. The UnityEngine. before the vectors and the System.Numerics.

No what you have is fine.

I think you just need a better grasp on Vector math and how the various operators act with different types. For example:

Vector3 + Vector3 = Vector3
Vector3 - Vector3 = Vector3
Vector3 * float = Vector3 (scaled by the float)
Vector3 / float = Vector3 (scaled by the inverse of the float)

Quaternion * Vector3 = Vector3 (rotated by the quaternion) << This is the one I suggested to you to rotate your input by the transform’s rotation

An alternative to transform.rotation * new Vector3(move.x, 0, move.y) could be this:

Vector3 m = (transform.forward * move.y) + (transform.right * move.x)

In terms of the types, that breaks down to this:
(Vector3 * float) + (Vector3 * float)
which further simplifies to:
Vector3 + Vector3
which results in:
Vector3.

I think this matches a bit more what you were trying to do in your original code.

Okay, I’ll give it a shot. Thank you so much!

Oh my god lol, all I needed to do was change the line to “Vector3 m = (transform.forward * move.y) + (transform.right * move.x)” this one and it just worked… Thanks

1 Like

Yep. Your logic was fine, you just didn’t quite have the way to express it in code down. That does exactly what your original code intended.

1 Like

Yeah i got the same with this

 Collider[] colliders = Physics.OverlapSphere(transform.position, Sight);
        foreach (Collider nearbyObjects in colliders)
        {
            if(nearbyObjects.tag == WorkerTag)
            {
               myNavMeshAgent.SetDestination(nearbyObjects);
            }
        }

Feel free to make your own thread for your own error. This is not the same, although it’s similar. You’re passing the wrong type of parameter into SetDestination.

thanks im still new to coding ill make a new thread

1 Like