Can't implicitly convert float to vector3.

Hello!

This is probably a stupid question but I’ve been trying to figure this out myself for more than a day now and I still have no idea.

I’ve been following this tutorial:

Can someone please tell me why the code he writes from 21:36 to 21:42 works?
For me it throws up an error message saying something along the lines of: “Cannot implicitly convert float to vector3.”

Post your code (using code tags). It almost allways is a typo or user error

Oh I didn’t mean to imply that it’s not my mistake. I just can’t find it.

This code is (or was supposed to be) a copy of the code in the video with some extra code that’s meant to change movement inputs to be relative to the cinemachine camera.

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

public class PlayerMovement : MonoBehaviour
{
    //Variable to store the instance of PlayerInput
    PlayerInput playerInput;
    CharacterController characterController;
    Animator animator;

    //Variables to store player input values
    Vector2 currentMovementInput;
    Vector3 currentMovement;
    bool isMovementPressed;
    float rotationFactorPerFrame = 15.0f;

    //Awake is called when the script instance is being loaded
    void Awake()
    {
        //Initially set reference variables
        playerInput = new PlayerInput();
        characterController = GetComponent<CharacterController>();
        animator = GetComponent<Animator>();

        //set the player input callbacks
        playerInput.CharacterControls.Move.started += onMovementInput;
        playerInput.CharacterControls.Move.canceled += onMovementInput;
        playerInput.CharacterControls.Move.performed += onMovementInput;

    }

    void handleRotation()
    {
        Vector3 positionToLookAt;

        //the change in position our character should point to
        positionToLookAt = currentMovement.x;
        positionToLookAt = 0.0f;
        positionToLookAt = currentMovement.z;

        //the current rotation of our character
        Quaternion currentRotation = transform.rotation;

        if (isMovementPressed)
        {
            //creates a new rotation based on where the player is currently pressing
            Quaternion targetRotation = Quaternion.LookRotation(positionToLookAt);
            transform.rotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame * Time.deltaTime);
        }
    }

    //handler function to set the player input values
    void onMovementInput (InputAction.CallbackContext context)
    {
        currentMovementInput = context.ReadValue<Vector2>();
        currentMovement.x = currentMovementInput.x;
        currentMovement.z = currentMovementInput.y;
        isMovementPressed = currentMovementInput.x != 0 || currentMovementInput.y != 0;
    }

    void handleAnimatíon()
    {
        //get parameter values from animator
        bool isWalking = animator.GetBool("isWalking");
        bool isRunning = animator.GetBool("isRunning");

        //start walking if movement is pressed and not already walking
        if (isMovementPressed && !isWalking)
        {
            animator.SetBool("isWalking", true);
        }

        //stop walking if movement is not pressed and currently walking
        else if (!isMovementPressed && isWalking)
        {
            animator.SetBool("isWalking", false);
        }
    }

    // Update is called once per frame
    void Update()
    {
        handleRotation();
        handleAnimatíon();
        MovePlayerRelativeToCamera();
        characterController.Move(currentMovement * Time.deltaTime);
    }

    void MovePlayerRelativeToCamera()
    {
        //Get player input
        float playerVerticalInput = Input.GetAxis("Vertical");
        float playerHorizontalInput = Input.GetAxis("Horizontal");

        //Get Camera Normalized Directional Vectors
        Vector3 forward = Camera.main.transform.forward;
        Vector3 right = Camera.main.transform.right;
        forward.y = 0;
        right.y = 0;
        forward = forward.normalized;
        right = right.normalized;

        //Create direction-relative-input vectors
        Vector3 forwardRelativeVerticalInput = playerVerticalInput * forward;
        Vector3 rightRelativeVerticalInput = playerHorizontalInput * right;

        //Create and apply camera relative movement
        Vector3 cameraRelativeMovement = (forwardRelativeVerticalInput + rightRelativeVerticalInput);
        this.transform.Translate(cameraRelativeMovement, Space.World);
    }

    void OnEnable()
    {
        //enable the character controls action map
        playerInput.CharacterControls.Enable();
    }

    void OnDisable()
    {
        //disable the character controls action map
        playerInput.CharacterControls.Disable();
    }
}

That’s the EASY part of the error message!! The error message is telling you!

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.

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.

As for your actual error, it’s just apples and oranges:

Some help to fix “Cannot implicitly convert type ‘Xxxxx’ into ‘Yyyyy’:”

http://plbm.com/?p=263

You should explain to yourself what each of lines 39 to 41 are doing, what datatype is involved on each side. No magic here!

Thank you.

I was able to find the error with the conversion myself. (Lines 39 to 41)
I could probably find a way to rewrite it by googling but I figured it’s better if I learn what went wrong instead of working around it since the same 3 lines seem to work just fine in the tutorial.

That makes me think I must have a mistake somewhere else. (Or at least something that is different from the tutorial.)

Entirely possible, but at least FIX the three errors I pointed out. They are obviously wrong and will never work;

Vector3 positionToLookAt;
// this will NEVER WORK: the left is a Vector3, the right is a float
positionToLookAt = currentMovement.x;

Perhaps you mean:

positionToLookAt.x = currentMovement.x;

?

Either way, you MUST initialize positionToLookAt in its entirety, so just do it.

instead of declaring it, declare and assign all to zero:

Vector3 positionToLookAt = Vector3.zero;

Now go jam in whatever x, y and z you want.

Or combine it into one big hairy new Vector3() initialization, up to you.

1 Like

Oh my god thank you!

That’s it.
I must have compared my code to the tutorial like 30 times. I have no idea how I missed something that obvious…

Eyeballs can be like that unfortunately. I’ve had it myself.

One good strategy is any script that is less than a few lines long, just delete it and retype that section.

It’s unlikely you’ll make the same mistakes twice.

1 Like