Search Unity

Argument `#1' cannot convert `UnityEngine.Vector3' expression to type `float'

Discussion in 'Scripting' started by ogifull9, Jan 19, 2020.

  1. ogifull9

    ogifull9

    Joined:
    Nov 1, 2019
    Posts:
    4
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Igrac : MonoBehaviour
    5. {
    6.     public bool controllable = false;
    7.  
    8.     public float speed = 7.0f;
    9.     public float jumpSpeed = 6.0f;
    10.     public float gravity = 20.0f;
    11.  
    12.     private Vector3 moveDirection =new Vector3(0, 0, 0);
    13.     private CharacterController controller;
    14.  
    15.     // Use this for initialization
    16.     void Start()
    17.     {
    18.         controller = GetComponent<CharacterController>();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         if (controller.isGrounded && controllable)
    25.         {
    26.             moveDirection += new Vector3(Input.GetAxis("Vertical") * transform.right, 0, Input.GetAxis("Horizontal") * transform.forward);
    27.             moveDirection = transform.TransformDirection(moveDirection);
    28.             moveDirection *= speed;
    29.  
    30.             if (Input.GetButton("Jump"))
    31.                 moveDirection.y = jumpSpeed;
    32.  
    33.         }
    34.         moveDirection.y -= gravity * Time.deltaTime;
    35.         controller.Move(moveDirection * Time.deltaTime);
    36.     }
    37. }
    38.  
     
  2. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
    Firstly, transform.right and transform.forward are Vector3s. On line 26 you're passing them as arguments to the Vector3 constructor - but it expects floats. You can achieve what you're trying to do by summing them instead.

    Secondly, you're transforming moveDirection from local space to world space, but transform.right and transform.forward are both already in world space so this is unnecessary.

    Thirdly, multiplying
    moveDirection by speed every frame will result in an acceleration.

    Replace lines 26-28 with this instead:
    Code (CSharp):
    1. var worldSpaceDir = transform.right * Input.GetAxis("Vertical") +
    2.                     transform.forward * Input.GetAxis("Horizontal");
    3.  
    4. moveDirection += worldSpaceDir.normalized * speed;
     
    Last edited: Jan 19, 2020
  3. ogifull9

    ogifull9

    Joined:
    Nov 1, 2019
    Posts:
    4
    Now my character moves super fast
     
  4. ogifull9

    ogifull9

    Joined:
    Nov 1, 2019
    Posts:
    4
    My speed is determined by how long i hold it and the controls are super slippery and the forward movements are inverted
     
  5. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
    I see. The code provided above fixes the problems with your original code. It works (aside from the per frame acceleration) as it would if yours had compiled.
    This is due to how Input.GetAxis works and is setup in the InputManager. If you don't want that behaviour you can either use a different Input method/setup or snap the values by rounding them. Snapping would look like this:
    Code (CSharp):
    1. var worldSpaceDir = transform.right * Mathf.Round(Input.GetAxis("Vertical")) +
    2.                     transform.forward * Mathf.Round(Input.GetAxis("Horizontal"));
    In that case, negate the forward direction:
    Code (CSharp):
    1. var worldSpaceDir = transform.right * Mathf.Round(Input.GetAxis("Vertical")) -
    2.                     transform.forward * Mathf.Round(Input.GetAxis("Horizontal"));