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. Dismiss Notice

Error Assets\PlayerMovement.cs(12,49): error CS0664: Literal of type double cannot be implicitly con

Discussion in 'Scripting' started by Spieler2301, Apr 14, 2021.

  1. Spieler2301

    Spieler2301

    Joined:
    Aug 8, 2020
    Posts:
    1
    I dont get what the error means:
    Assets\PlayerMovement.cs(12,49): error CS0664: Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type


    Heres my code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerMovement : MonoBehaviour
    {
    //Made Using this tuturial

    //Makes the _speed Variable show up in unity
    [SerializeField] private float _speed = 7;
    [SerializeField] private float _mouseSensitivty = 50f;
    [SerializeField] private float _minCameraview = -70f, _maxCameraview = 80f;
    [SerializeField] private float _mouseXToY = 1.25;

    private CharacterController _characterController;
    private Camera _camera;
    private float xRotation = 0f;

    // Start is called before the first frame update
    void Start()
    {
    _characterController = GetComponent<CharacterController>();
    _camera = Camera.main;

    //Check if this Script is attached to the Player object
    if (_characterController == null)
    Debug.Log("No Character Controller attached to Player");

    Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update()
    {
    //Get wasd input for player
    float horizontal = Input.GetAxis("Horizontal");
    float vertical = Input.GetAxis("Vertical");
    //move player on wasd input
    Vector3 movement = Vector3.forward * vertical + Vector3.right * horizontal;
    _characterController.Move(movement * Time.deltaTime * _speed);

    //Get Muose position input
    float mouseX = Input.GetAxis("Mouse X") * _mouseSensitivty * Time.deltaTime;
    float mouseY = Input.GetAxis("Mouse Y") * _mouseSensitivty * Time.deltaTime;
    //rotate Camera based on Yinput
    xRotation -= mouseY;
    //xRotation = xRotation - mouseY same as xRotation -= mouseY
    //clamp the cam rotation betwenn 80 and -70 degrees
    xRotation =Mathf.Clamp(xRotation, _minCameraview, _maxCameraview);

    _camera.transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
    //rotate Body and Camera based on Xinput
    transform.Rotate(Vector3.up * mouseX * _mouseXToY);
    //

    }
    }
     
  2. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    Welcome! In this forum, you will get much more help if you format your code with code tags <= LINK.

    Your error message actually tells you what the problem is, and how to fix it. Let's break it down.

    First, it tells you where the problem is:

    Assets\PlayerMovement.cs(12,49): error CS0664: Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type​

    It's happening at Line 12, Column 49. Here's Line 12:

    [SerializeField] private float _mouseXToY = 1.25;​

    It tells you what is causing the trouble:

    Assets\PlayerMovement.cs(12,49): error CS0664: Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type​

    A "literal" is a constant, like a number, character, or string. In this case, it's a number:

    [SerializeField] private float _mouseXToY = 1.25;​

    The error message tells you what's wrong with the number:

    Assets\PlayerMovement.cs(12,49): error CS0664: Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type​

    All literal numbers with decimal points in them are initially assumed by C# to be doubles:

    [SerializeField] private float _mouseXToY = 1.25; // 1.25 has a decimal, so C# thinks it is a double.

    What you are trying to do with the number that is causing the problem:

    Assets\PlayerMovement.cs(12,49): error CS0664: Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type​

    You are trying to assign it to a float, which C# does not know how to do:

    [SerializeField] private float _mouseXToY = 1.25;​

    But the error message tells you how to fix it:

    Assets\PlayerMovement.cs(12,49): error CS0664: Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type

    So just do what it says:

    [SerializeField] private float _mouseXToY = 1.25f;​

    That makes the literal into a float, and that will solve your problem.