Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

cannot convert from 'UnityEngine.Vector3' to 'float'

Discussion in 'Editor & General Support' started by Asalfin, Mar 22, 2019.

  1. Asalfin

    Asalfin

    Joined:
    Mar 21, 2019
    Posts:
    11
    Hi,
    I get this error : cannot convert from 'UnityEngine.Vector3' to 'float'
    Using this code :

    Code (CSharp):
    1. public class PlayerController : MonoBehaviour
    2. {
    3.     Rigidbody rBody;
    4.     float speed = 20;
    5.     public float hoverForce = 9.0f;
    6.     public float hoverHeight = 2.0f;
    7.     public float deepHeight = 1.0f;
    8.     public float deepForce = 20.0f;
    9.     public float deepSmoothTime = .12f;
    10.     bool deep;
    11.     Transform cameraT;
    12.     Vector3 targetDir;
    13.     Vector3 deepSmoothVelocity;
    14.  
    15.  
    16.     void Start()
    17.     {
    18.         rBody = GetComponent<Rigidbody>();
    19.         cameraT = Camera.main.transform;
    20.     }
    21.  
    22.     void FixedUpdate()
    23.     {
    24.         //Controls
    25.         Vector3 controlDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    26.         Vector3 actualDirection = cameraT.TransformDirection(controlDirection);
    27.  
    28.         rBody.AddForce(actualDirection * speed);
    29.  
    30.         //Ray Casting for height
    31.         Ray ray = new Ray(transform.position, -transform.up);
    32.         RaycastHit hit;
    33.         {
    34.         if (Physics.Raycast (ray, out hit, hoverHeight))
    35.             {
    36.                 rBody.AddForceAtPosition(Vector3.up * hoverForce * (1.0f - (hit.distance / hoverHeight)), transform.position);
    37.                 Debug.DrawLine(ray.origin, hit.point, Color.blue);
    38.  
    39.                 if (Physics.Raycast(ray, out hit, deepHeight))
    40.                 {
    41.                     rBody.velocity = new Vector3(rBody.velocity.x, Vector3.SmoothDamp(rBody.velocity, new Vector3(rBody.velocity.x, 0, rBody.velocity.z), ref deepSmoothVelocity, deepSmoothTime));
    42.  
    43.                      Debug.DrawLine(ray.origin, hit.point, Color.red);
    44.                     print(rBody.velocity.y);
    45.                 }
    46.             }
    47.             else
    48.             {
    49.                 //GetComponent<Material>(Color.red);
    50.                 Debug.DrawLine(ray.origin, ray.origin + ray.direction * 100, Color.green);
    51.             }
    52.         }
    53.     }
    54. }
    The line that gets an error is this one :
    Code (CSharp):
    1. rBody.velocity = new Vector3(rBody.velocity.x, Vector3.SmoothDamp(rBody.velocity, new Vector3(rBody.velocity.x, 0, rBody.velocity.z), ref deepSmoothVelocity, deepSmoothTime));
    Does anyone know how to fix it ? I would be really grateful !
    Asalfin
     
    Last edited: Sep 24, 2019
  2. Asalfin

    Asalfin

    Joined:
    Mar 21, 2019
    Posts:
    11
    Just got it to work !
    I was simply using a vector3 instead of float.
    Good night :)
     
  3. tehmikeh

    tehmikeh

    Joined:
    Apr 19, 2019
    Posts:
    3
    hey I know this is old but can you explain how you solved it to me? I am having the same error and I cannot seem to figure it out.
     
    Last edited: Dec 17, 2019
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Show the code that is giving you the error.
     
  5. tehmikeh

    tehmikeh

    Joined:
    Apr 19, 2019
    Posts:
    3
    Here is the code I put it into a text file to make it easier to view. Thank you for the help.
     

    Attached Files:

  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Which line is throwing the error?
     
  7. tehmikeh

    tehmikeh

    Joined:
    Apr 19, 2019
    Posts:
    3
    I don't know how to view the errors in visual studio code? There are no red squiggles or anything.