Search Unity

Question (39, 21) and (41,21) error CS0029: Cannot implicitly convert type 'float' to 'UnityEngine.Vector3'

Discussion in 'Getting Started' started by unity_9D49407110F5EFB875B2, Feb 13, 2024.

  1. unity_9D49407110F5EFB875B2

    unity_9D49407110F5EFB875B2

    Joined:
    Aug 24, 2023
    Posts:
    5
    Can someone help me?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public float moveSpeed, gravityModifier;
    8.     public CharacterController charCon;
    9.  
    10.     private Vector3 moveInput;
    11.  
    12.     public Transform camTrans;
    13.  
    14.     public float mouseSensitvity;
    15.     public bool invertX;
    16.     public bool invertY;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.        
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         //moveInput.x = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
    28.         //moveInput.z = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
    29.  
    30.         float yStore = moveInput.y;
    31.  
    32.         Vector3 vertMove = transform.forward * Input.GetAxisRaw("Vertical");
    33.         Vector3 HoriMove = transform.right * Input.GetAxisRaw("Horizontal");
    34.  
    35.         moveInput = HoriMove + vertMove;
    36.         moveInput.Normalize();
    37.         moveInput = moveInput * moveSpeed;
    38.  
    39.         moveInput = yStore;
    40.  
    41.         moveInput = Physics.gravity.y * gravityModifier * Time.deltaTime;
    42.  
    43.         if (charCon.isGrounded)
    44.         {
    45.             moveInput.y = Physics.gravity.y * gravityModifier * Time.deltaTime;
    46.         }
    47.  
    48.         charCon.Move(moveInput * Time.deltaTime);
    49.  
    50.  
    51.         //control camera rotation
    52.         Vector2 mouseInput = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")) * mouseSensitvity;
    53.  
    54.         if (invertX)
    55.         {
    56.             mouseInput.x = -mouseInput.x;
    57.         }
    58.         if (invertY)
    59.         {
    60.             mouseInput.y = -mouseInput.y;
    61.         }
    62.  
    63.         transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + mouseInput.x, transform.rotation.eulerAngles.z);
    64.  
    65.         camTrans.rotation = Quaternion.Euler(camTrans.rotation.eulerAngles + new Vector3(-mouseInput.y, 0f, 0f));
    66.     }
    67. }
    68.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    It's just as your error says. You have a Vector3 variable:
    Code (CSharp):
    1. private Vector3 moveInput;
    and a float variable:
    Code (CSharp):
    1. float yStore = moveInput.y;
    and then you're trying to assign the float to the Vector3 variable:
    Code (CSharp):
    1. moveInput = yStore;
    This doesn't make sense. You can't put a float into a Vector3 variable anymore than you can shove a square peg into a round hole.

    Did you perhaps mean to do something like this?
    Code (CSharp):
    1. moveInput.y = yStore;
    You need to think consciously about what each line of code is doing and whether it makes sense.
     
  3. unity_9D49407110F5EFB875B2

    unity_9D49407110F5EFB875B2

    Joined:
    Aug 24, 2023
    Posts:
    5
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 5, 2024
    Posts:
    474
     
    Kurt-Dekker and PraetorBlue like this.
  5. unity_9D49407110F5EFB875B2

    unity_9D49407110F5EFB875B2

    Joined:
    Aug 24, 2023
    Posts:
    5
    (42,23): error CS1061: 'Vector3' does not contain a definition for 'GetKey' and no accessible extension method 'GetKey' accepting a first argument of type 'Vector3' could be found (are you missing a using directive or an assembly reference?)

    need help again

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public float moveSpeed, gravityModifier, jumpPower, runSpeed = 12f;
    8.     public CharacterController charCon;
    9.  
    10.     private Vector3 moveInput;
    11.  
    12.     public Transform camTrans;
    13.  
    14.     public float mouseSensitvity;
    15.     public bool invertX;
    16.     public bool invertY;
    17.  
    18.     private bool canJump;
    19.     public Transform groundCheckPoint;
    20.     public LayerMask whatIsGround;
    21.  
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.        
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.         //moveInput.x = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
    32.         //moveInput.z = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
    33.  
    34.         float yStore = moveInput.y;
    35.  
    36.         Vector3 vertMove = transform.forward * Input.GetAxisRaw("Vertical");
    37.         Vector3 HoriMove = transform.right * Input.GetAxisRaw("Horizontal");
    38.  
    39.         moveInput = HoriMove + vertMove;
    40.         moveInput.Normalize();
    41.  
    42.         if (moveInput.GetKey(KeyCode.LeftShift))
    43.         {
    44.             moveInput = moveInput * runSpeed;
    45.         }
    46.         else
    47.         {
    48.             moveInput = moveInput * moveSpeed;
    49.         }
    50.  
    51.         moveInput.y = yStore;
    52.  
    53.         moveInput.y += Physics.gravity.y * Time.deltaTime;
    54.  
    55.         if (charCon.isGrounded)
    56.         {
    57.             moveInput.y = Physics.gravity.y * gravityModifier * Time.deltaTime;
    58.         }
    59.  
    60.         canJump = Physics.OverlapSphere(groundCheckPoint.position, .25f, whatIsGround).Length > 0;
    61.  
    62.         //handle jump
    63.         if (Input.GetKeyDown(KeyCode.Space) && charCon.isGrounded)
    64.         {
    65.             moveInput.y = jumpPower;
    66.         }
    67.  
    68.         charCon.Move(moveInput * Time.deltaTime);
    69.  
    70.  
    71.         //control camera rotation
    72.         Vector2 mouseInput = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")) * mouseSensitvity;
    73.  
    74.         if (invertX)
    75.         {
    76.             mouseInput.x = -mouseInput.x;
    77.         }
    78.         if (invertY)
    79.         {
    80.             mouseInput.y = -mouseInput.y;
    81.         }
    82.  
    83.         transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + mouseInput.x, transform.rotation.eulerAngles.z);
    84.  
    85.         camTrans.rotation = Quaternion.Euler(camTrans.rotation.eulerAngles + new Vector3(-mouseInput.y, 0f, 0f));
    86.     }
    87. }
    88.  
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    You are really just making typos/silly mistakes. Look at the line of code indicated in the error. It doesn't make any sense.
     
    Ruslank100, Kurt-Dekker and Ryiah like this.