Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Bug Compile Error: Cannot implicitly convert type (float, float) to Vector2

Discussion in 'Scripting' started by Wolfgang385, May 15, 2024.

  1. Wolfgang385

    Wolfgang385

    Joined:
    Nov 11, 2022
    Posts:
    14
    Hey I've been running into this problem while trying to do some physics in Unity. So i made everything floats (as you'll see in the code below) by casting it as one and storing it in float variables so idk what's going on. Please help.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class IDK : MonoBehaviour
    6. {
    7.     private Vector2 bossV;
    8.     private Vector2 bossJump;
    9.     private float initialV, jumpAngle, jumpAngleA, jumpAngleB, jumpAngleC;
    10.     public float jumpTime;
    11.     public Rigidbody2D rb;
    12.     private double bossVX, bossVY;
    13.  
    14.     void JumpAttack()
    15.     {
    16.         bossV = this.transform.position;
    17.         jumpAngleA = (float)(0.5 * 4.0 * -9.8 * System.Math.Pow(bossV.x / initialV, 2));
    18.         Debug.LogFormat("jumpAngleA = {0}", jumpAngleA);
    19.         jumpAngleB = (bossV.x);
    20.         jumpAngleC = (float)(-1.0 * bossV.y + 0.5 * 4.0 * -9.8 * System.Math.Pow(bossV.x / initialV, 2));
    21.         jumpAngle = (float)(System.Math.Atan((-1.0 * jumpAngleB + System.Math.Sqrt(System.Math.Pow(jumpAngleB, 2) - 4.0 * jumpAngleA * jumpAngleC)) / 2.0 * jumpAngleA));
    22.         bossVX = (-1f * initialV * System.Math.Cos(jumpAngle) * jumpTime + bossV.x);
    23.         bossVY = (-0.5f * 4f * -9.8f * jumpTime * jumpTime + initialV * System.Math.Sin(jumpAngle) * jumpTime);
    24.         bossV = (System.Convert.ToSingle(bossVX), System.Convert.ToSingle(bossVY));
    25.     }
    26. }
    27.  
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,432
    You're using tuples when the error is saying a Vector2 is required. You can just
    new()
    a Vector2 rather than using tuples.
     
    Spy-Master likes this.
  3. Wolfgang385

    Wolfgang385

    Joined:
    Nov 11, 2022
    Posts:
    14
    omg it was so simple. thank you so much!
     
    spiney199 likes this.