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

Question Newbie trying to code a game with horizontal movement

Discussion in 'Scripting' started by unity_3AE8836F44CCDAF78BF3, Mar 26, 2023.

  1. unity_3AE8836F44CCDAF78BF3

    unity_3AE8836F44CCDAF78BF3

    Joined:
    Mar 21, 2023
    Posts:
    2
    I'm following a tutorial with a vertical and horizontal movement but I just want to add a horizontal movement. Also, The tutorial add camera scripting but I will use cinemachine so I decide to alterate a bit the code and create a new one based on the original.
    After some changes this error apeared.

    Assets\Scripts\Test.cs(32,22): error CS0029: Cannot implicitly convert type 'float' to 'UnityEngine.Vector3'

    I searched for answers in google but, since I'm a newbie, I didn't understand how to solve the error.
    any help, please?

    Original code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerOneMovement : MonoBehaviour
    6. {
    7.     private float horizontalMove;
    8.     private float verticalMove;
    9.  
    10.     private Vector3 playerInput;
    11.  
    12.     public CharacterController Player;
    13.     public float PlayerSpeed;
    14.     public float gravity;
    15.     public float fallVelocity;
    16.     public float jumpForce;
    17.  
    18.     public Camera mainCamera;
    19.     private Vector3 camForward;
    20.     private Vector3 camRight;
    21.     private Vector3 movePlayer;
    22.  
    23.     // Use this for initialization
    24.     void Start()
    25.     {
    26.         Player = GetComponent<CharacterController>();
    27.     }
    28.  
    29.     // Funcion movimiento jugador.
    30.     void Update()
    31.     {
    32.         horizontalMove = Input.GetAxis("Horizontal");
    33.         verticalMove = Input.GetAxis("Vertical");
    34.  
    35.         playerInput = new Vector3(horizontalMove, 0, verticalMove);
    36.         playerInput = Vector3.ClampMagnitude(playerInput, 1);
    37.    
    38.         camDirection();
    39.  
    40.         movePlayer = playerInput.x * camRight + playerInput.z * camForward;
    41.  
    42.         movePlayer = movePlayer * PlayerSpeed;
    43.        
    44.         Player.transform.LookAt(Player.transform.position + movePlayer);
    45.      
    46.         SetGravity();
    47.  
    48.         PlayerSkills();
    49.  
    50.         Player.Move(movePlayer * Time.deltaTime);
    51.  
    52.         Debug.Log(Player.velocity.magnitude);
    53.     }
    54.  
    55.     // Funcion para la camara.
    56.     public void camDirection()
    57.     {
    58.         camForward = mainCamera.transform.forward;
    59.         camRight = mainCamera.transform.right;
    60.  
    61.         camForward.y = 0;
    62.         camRight.y = 0;
    63.  
    64.         camForward = camForward.normalized;
    65.         camRight = camRight.normalized;
    66.     }
    67.  
    68.     // Funcion para habilidades.
    69.     public void PlayerSkills()
    70.     {
    71.         if (Player.isGrounded && Input.GetButtonDown("Jump"))
    72.         {
    73.             fallVelocity = jumpForce;
    74.             movePlayer.y = fallVelocity;
    75.         }
    76.     }
    77.  
    78.     // Funcion para la gravedad.
    79.     public void SetGravity()
    80.     {
    81.         if (Player.isGrounded)
    82.         {
    83.             fallVelocity = -gravity * Time.deltaTime;
    84.             movePlayer.y = fallVelocity;
    85.         }
    86.         else
    87.         {
    88.             fallVelocity -= gravity * Time.deltaTime;
    89.             movePlayer.y = fallVelocity;
    90.         }
    91.     }
    92. }
    My code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Test : MonoBehaviour
    6. {
    7.     private float horizontalMove;
    8.     public float PlayerSpeed;
    9.     public float gravity;
    10.     public float fallVelocity;
    11.     public float jumpForce;
    12.  
    13.     public CharacterController Player;
    14.  
    15.     private Vector3 movePlayer;
    16.     private Vector3 playerInput;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         Player = GetComponent<CharacterController>();
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         horizontalMove = Input.GetAxis("Horizontal");
    28.  
    29.         playerInput = new Vector3(horizontalMove, 0);
    30.         playerInput = Vector3.ClampMagnitude(playerInput, 1);
    31.  
    32.         movePlayer = playerInput.x;
    33.  
    34.         movePlayer = movePlayer * PlayerSpeed;
    35.        
    36.         Player.transform.LookAt(Player.transform.position + movePlayer);
    37.      
    38.         SetGravity();
    39.  
    40.         PlayerSkills();
    41.  
    42.         Player.Move(movePlayer * Time.deltaTime);
    43.  
    44.         Debug.Log(Player.velocity.magnitude);
    45.     }
    46.    
    47.     // Habilidades
    48.     public void PlayerSkills()
    49.     {
    50.         if (Player.isGrounded && Input.GetButtonDown("Jump"))
    51.         {
    52.             fallVelocity = jumpForce;
    53.             movePlayer.y = fallVelocity;
    54.         }
    55.     }
    56.  
    57.     // Gravity
    58.     public void SetGravity()
    59.     {
    60.         if (Player.isGrounded)
    61.         {
    62.             fallVelocity = -gravity * Time.deltaTime;
    63.             movePlayer.y = fallVelocity;
    64.         }
    65.         else
    66.         {
    67.             fallVelocity -= gravity * Time.deltaTime;
    68.             movePlayer.y = fallVelocity;
    69.         }
    70.     }
    71. }
    72.  
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
    movePlayer
    is a Vector3. The x component of a
    playerInput
    is a float, and C#/Unity does not know how to add them together.

    Perhaps you just wanted set the x component of movePlayer?
    movePlayer.x = playerInput.x;
    for example.
     
  3. unity_3AE8836F44CCDAF78BF3

    unity_3AE8836F44CCDAF78BF3

    Joined:
    Mar 21, 2023
    Posts:
    2
    Thank you so much!!! who would thought it will be that simple?