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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

transform.position.y error

Discussion in 'Scripting' started by JAVIERMUNOZ, Apr 2, 2020.

  1. JAVIERMUNOZ

    JAVIERMUNOZ

    Joined:
    Dec 25, 2019
    Posts:
    8
    Hello so im getting another error with my brick breaker game. This time while trying to add projectiles on the paddle. The error on the console is : Assets\Scripts\Paddle.cs(69,114): error CS1503: Argument 2: cannot convert from 'double' to 'float'

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using UnityEngine;
    4.  
    5. public class Paddle : MonoBehaviour
    6. {
    7.     #region Singleton
    8.  
    9.     private static Paddle _instance;
    10.     public static Paddle Instance => _instance;
    11.  
    12.  
    13.     public bool PaddleIsTransforming { get; set; }
    14.     private void Awake()
    15.     {
    16.         if (_instance != null)
    17.         {
    18.             Destroy(gameObject);
    19.         }
    20.         else
    21.         {
    22.             _instance = this;
    23.         }
    24.     }
    25.  
    26.     #endregion
    27.  
    28.  
    29.     private float speed = 10f;
    30.     private float input;
    31.     private SpriteRenderer sr;
    32.  
    33.     public bool PaddleIsShooting { get; set; }
    34.     public GameObject leftMuzzle;
    35.     public GameObject rightMuzzle;
    36.     public Projectile bulletPrefab;
    37.  
    38.     public float extendShrinkDuration = 10;
    39.     public float paddleWidth = 2;
    40.     public float paddleHeight = 0.28f;
    41.     private BoxCollider2D boxCol;
    42.     // Start is called before the first frame update
    43.     void Start()
    44.     {
    45.         sr = GetComponent<SpriteRenderer>();
    46.         boxCol = GetComponent<BoxCollider2D>();
    47.     }
    48.  
    49.     // Update is called once per frame
    50.     void  Update()
    51.     {
    52.         if (transform.position.x < -11.3f)
    53.         {
    54.             transform.position = new Vector3(-11.3f, transform.position.y, transform.position.z);
    55.         }
    56.         if (transform.position.x > 11.3f)
    57.         {
    58.             transform.position = new Vector3(11.3f, transform.position.y, transform.position.z);
    59.         }
    60.         input = Input.GetAxisRaw("Horizontal");
    61.         UpdateMuzzlePosition();
    62.     }
    63.  
    64.     private void UpdateMuzzlePosition()
    65.     {
    66.         leftMuzzle.transform.position = new Vector3 (this.transform.position.x - (this.sr.size.x / 2) + 0.1f, this.transform.position.y + 0.2, this.transform.position.z);
    67.         rightMuzzle.transform.position = new Vector3 (this.transform.position.x + (this.sr.size.x / 2) + 0.153f, this.transform.position.y + 0.2, this.transform.position.z);
    68.     }
    69.  
    70.     private void FixedUpdate()
    71.     {
    72.  
    73.         GetComponent<Rigidbody2D>().velocity = Vector2.right * input * speed;
    74.     }
    75.     public void StartWidthAnimation(float newWidth)
    76.     {
    77.         StartCoroutine(AnimatePaddleWidth(newWidth));
    78.     }
    79.  
    80.     public IEnumerator AnimatePaddleWidth(float width)
    81.     {
    82.         this.PaddleIsTransforming = true;
    83.         this.StartCoroutine(ResetPaddleWidthAfterTime(this.extendShrinkDuration));
    84.  
    85.         if (width > this.sr.size.x)
    86.         {
    87.             float currentWidth = this.sr.size.x;
    88.             while (currentWidth < width)
    89.             {
    90.                 currentWidth += Time.deltaTime * 2;
    91.                 this.sr.size = new Vector2(currentWidth, paddleHeight);
    92.                 boxCol.size = new Vector2(currentWidth, paddleHeight);
    93.                 yield return null;
    94.             }
    95.         }
    96.         else
    97.         {
    98.             float currentWidth = this.sr.size.x;
    99.             while (currentWidth > width)
    100.             {
    101.                 currentWidth -= Time.deltaTime * 2;
    102.                 this.sr.size = new Vector2(currentWidth, paddleHeight);
    103.                 boxCol.size = new Vector2(currentWidth, paddleHeight);
    104.                 yield return null;
    105.             }
    106.         }
    107.         this.PaddleIsTransforming = false;
    108.     }
    109.  
    110.     private IEnumerator ResetPaddleWidthAfterTime(float seconds)
    111.     {
    112.         yield return new WaitForSeconds(seconds);
    113.         this.StartWidthAnimation(this.paddleWidth);
    114.  
    115.     }
    116.  
    117.     private void OnCollisionEnter2D(Collision2D collision)
    118.     {
    119.         if (collision.gameObject.tag == "Ball")
    120.         {
    121.             Rigidbody2D ballrb = collision.gameObject.GetComponent<Rigidbody2D>();
    122.             Vector3 hitPoint = collision.contacts[0].point;
    123.  
    124.             Vector3 paddleCenter = new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y);
    125.             ballrb.velocity = Vector2.zero;
    126.  
    127.             float difference = paddleCenter.x - hitPoint.x;
    128.  
    129.             if (hitPoint.x < paddleCenter.x)
    130.             {
    131.                 ballrb.AddForce(new Vector2(-(Mathf.Abs(difference * 200)), BallManager.Instance.initialBallSpeed));
    132.             }
    133.             else
    134.             {
    135.                 ballrb.AddForce(new Vector2((Mathf.Abs(difference * 200)), BallManager.Instance.initialBallSpeed));
    136.  
    137.             }
    138.         }
    139.     }
    140.  
    141.     public void StartShooting()
    142.     {
    143.         if (!this.PaddleIsShooting)
    144.         {
    145.             this.PaddleIsShooting = true;
    146.             StartCoroutine(StartShootingRoutine());
    147.         }
    148.     }  
    149.      
    150.      public IEnumerator StartShootingRoutine()
    151.     {
    152.             float fireCooldown = .5f;
    153.             float fireCooldownLeft = 0;
    154.  
    155.             float shootingDuration = 10;
    156.             float shootingDurationLeft = shootingDuration;
    157.  
    158.             Debug.Log("START SHOOTING");
    159.  
    160.             while (shootingDurationLeft >= 0)
    161.             {
    162.                 fireCooldownLeft -= Time.deltaTime;
    163.                 shootingDurationLeft -= Time.deltaTime;
    164.  
    165.                 if (fireCooldownLeft <= 0)
    166.                 {
    167.                 this.Shoot();
    168.                     fireCooldownLeft = fireCooldown;
    169.                     Debug.Log($"Shoot at {Time.time}");
    170.                
    171.                 }
    172.  
    173.                 yield return null;
    174.             }
    175.  
    176.             Debug.Log("STOP SHOOTING");
    177.             this.PaddleIsShooting = false;
    178.         }
    179.  
    180.     private void Shoot()
    181.     {
    182.         leftMuzzle.SetActive(false);
    183.         rightMuzzle.SetActive(false);
    184.  
    185.         leftMuzzle.SetActive(true);
    186.         rightMuzzle.SetActive(true);
    187.     }
    188. }
     
    Last edited: Apr 2, 2020
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
  3. JAVIERMUNOZ

    JAVIERMUNOZ

    Joined:
    Dec 25, 2019
    Posts:
    8
    The issue is in line 66 and 67
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    you're missing
    f
    after
    0.2
     
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    also in lines 38, 39, 66, 67 (division by 2), 90, 101, 131, 153, 155, 160, 165.
    you are using literal integers where in fact you should use literal floats.
    mostly the compiler will correct this for you, but sometimes it will produce crazy results.
    just don't do this, C# isn't javascript.

    if you don't believe me, have this
    Code (csharp):
    1. var num = 1;
    2. float result = num / 2;
    3. bool predictable = result > 0;
    what do you think predictable evaluates to?
    true or false?
     
  6. JAVIERMUNOZ

    JAVIERMUNOZ

    Joined:
    Dec 25, 2019
    Posts:
    8
    Right, ok thanks and the reason I'm writing this code like this is due to a tutorial I'm following as I'm still a newbie to all this
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    So just to be clear, when you write a numeric literal in C# it will generally be treated as an integer, or if it has a decimal place it will be treated as a double. It is fine to use these defaults if those types are the exact types you want. Otherwise you should be specific.

    The issue with doubles is you potentially lose information when converting them to floats. So they don't do what's called an implicit conversion when you try to assign them to a float. You have to make it an explicit conversion to basically tell the compiler you are aware of what this means and are doing it on purpose. But in this case, just put "f" after all your float literals and save yourself the headache :p

    This is an example of an explicit conversion by the way

    Code (csharp):
    1. double someDouble = 0.23771;  //Declaring a double and assigning it a value
    2. float someFloat = (float)someDouble;  //Declaring a float and assigning it the value of someDouble converted into a float
    3.