Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Can't implicitly convert float to vector3.

Discussion in 'Scripting' started by Piprian, Sep 3, 2022.

  1. Piprian

    Piprian

    Joined:
    Aug 25, 2022
    Posts:
    5
    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."
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,456
    Post your code (using code tags). It almost allways is a typo or user error
     
  3. Piprian

    Piprian

    Joined:
    Aug 25, 2022
    Posts:
    5
    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.     //Variable to store the instance of PlayerInput
    9.     PlayerInput playerInput;
    10.     CharacterController characterController;
    11.     Animator animator;
    12.  
    13.     //Variables to store player input values
    14.     Vector2 currentMovementInput;
    15.     Vector3 currentMovement;
    16.     bool isMovementPressed;
    17.     float rotationFactorPerFrame = 15.0f;
    18.  
    19.     //Awake is called when the script instance is being loaded
    20.     void Awake()
    21.     {
    22.         //Initially set reference variables
    23.         playerInput = new PlayerInput();
    24.         characterController = GetComponent<CharacterController>();
    25.         animator = GetComponent<Animator>();
    26.  
    27.         //set the player input callbacks
    28.         playerInput.CharacterControls.Move.started += onMovementInput;
    29.         playerInput.CharacterControls.Move.canceled += onMovementInput;
    30.         playerInput.CharacterControls.Move.performed += onMovementInput;
    31.  
    32.     }
    33.  
    34.     void handleRotation()
    35.     {
    36.         Vector3 positionToLookAt;
    37.  
    38.         //the change in position our character should point to
    39.         positionToLookAt = currentMovement.x;
    40.         positionToLookAt = 0.0f;
    41.         positionToLookAt = currentMovement.z;
    42.  
    43.         //the current rotation of our character
    44.         Quaternion currentRotation = transform.rotation;
    45.  
    46.         if (isMovementPressed)
    47.         {
    48.             //creates a new rotation based on where the player is currently pressing
    49.             Quaternion targetRotation = Quaternion.LookRotation(positionToLookAt);
    50.             transform.rotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame * Time.deltaTime);
    51.         }
    52.     }
    53.  
    54.     //handler function to set the player input values
    55.     void onMovementInput (InputAction.CallbackContext context)
    56.     {
    57.         currentMovementInput = context.ReadValue<Vector2>();
    58.         currentMovement.x = currentMovementInput.x;
    59.         currentMovement.z = currentMovementInput.y;
    60.         isMovementPressed = currentMovementInput.x != 0 || currentMovementInput.y != 0;
    61.     }
    62.  
    63.     void handleAnimatíon()
    64.     {
    65.         //get parameter values from animator
    66.         bool isWalking = animator.GetBool("isWalking");
    67.         bool isRunning = animator.GetBool("isRunning");
    68.  
    69.         //start walking if movement is pressed and not already walking
    70.         if (isMovementPressed && !isWalking)
    71.         {
    72.             animator.SetBool("isWalking", true);
    73.         }
    74.  
    75.         //stop walking if movement is not pressed and currently walking
    76.         else if (!isMovementPressed && isWalking)
    77.         {
    78.             animator.SetBool("isWalking", false);
    79.         }
    80.     }
    81.  
    82.     // Update is called once per frame
    83.     void Update()
    84.     {
    85.         handleRotation();
    86.         handleAnimatíon();
    87.         MovePlayerRelativeToCamera();
    88.         characterController.Move(currentMovement * Time.deltaTime);
    89.     }
    90.  
    91.     void MovePlayerRelativeToCamera()
    92.     {
    93.         //Get player input
    94.         float playerVerticalInput = Input.GetAxis("Vertical");
    95.         float playerHorizontalInput = Input.GetAxis("Horizontal");
    96.  
    97.         //Get Camera Normalized Directional Vectors
    98.         Vector3 forward = Camera.main.transform.forward;
    99.         Vector3 right = Camera.main.transform.right;
    100.         forward.y = 0;
    101.         right.y = 0;
    102.         forward = forward.normalized;
    103.         right = right.normalized;
    104.  
    105.         //Create direction-relative-input vectors
    106.         Vector3 forwardRelativeVerticalInput = playerVerticalInput * forward;
    107.         Vector3 rightRelativeVerticalInput = playerHorizontalInput * right;
    108.  
    109.         //Create and apply camera relative movement
    110.         Vector3 cameraRelativeMovement = (forwardRelativeVerticalInput + rightRelativeVerticalInput);
    111.         this.transform.Translate(cameraRelativeMovement, Space.World);
    112.     }
    113.  
    114.     void OnEnable()
    115.     {
    116.         //enable the character controls action map
    117.         playerInput.CharacterControls.Enable();
    118.     }
    119.  
    120.     void OnDisable()
    121.     {
    122.         //disable the character controls action map
    123.         playerInput.CharacterControls.Disable();
    124.     }
    125. }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    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!
     
  5. Piprian

    Piprian

    Joined:
    Aug 25, 2022
    Posts:
    5
    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.)
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    Entirely possible, but at least FIX the three errors I pointed out. They are obviously wrong and will never work;

    Code (csharp):
    1. Vector3 positionToLookAt;
    2. // this will NEVER WORK: the left is a Vector3, the right is a float
    3. positionToLookAt = currentMovement.x;
    Perhaps you mean:

    Code (csharp):
    1. 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:

    Code (csharp):
    1. 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.
     
    Piprian likes this.
  7. Piprian

    Piprian

    Joined:
    Aug 25, 2022
    Posts:
    5
    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...
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    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.
     
    Piprian likes this.