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

Question Error "There is no argument given .... parameter 'x' of 'Mathf.Atan2"

Discussion in 'Getting Started' started by nomatt55, May 8, 2024.

  1. nomatt55

    nomatt55

    Joined:
    Mar 25, 2024
    Posts:
    4
    Hello,

    I am currently at an impass with a script I am writing. Below is the code. As you can see, I am trying to find the angle for an object to point at so that it follows the mouse. I am very close to finishing this, but I am getting an error from my 27th line.

    " Assets\CannonScript.cs(27,30): error CS7036: There is no argument given that corresponds to the required formal parameter 'x' of 'Mathf.Atan2(float, float)' "

    Code (CSharp):
    1.  cannonAngle = (Mathf.Atan2((realMouseLocation.x, realMouseLocation.y)) * Mathf.Rad2Deg);
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CannonScript : MonoBehaviour
    6. {
    7.     public Vector3 mouseLocation = Vector3.zero;
    8.     public Vector3 realMouseLocation = Vector3.zero;
    9.     public Vector3 cameraLocation = Vector3.zero;
    10.     public float cannonAngle = 0.0f;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.        
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         mouseLocation = Input.mousePosition;
    22.         realMouseLocation = Camera.main.ScreenToWorldPoint(mouseLocation);
    23.         cameraLocation = CameraScript.CameraPosition;
    24.         cameraLocation.z = 0.0f;
    25.         realMouseLocation = realMouseLocation - (gameObject.transform.position - cameraLocation);
    26.         Debug.Log(realMouseLocation);
    27.         cannonAngle = (Mathf.Atan2((realMouseLocation.x, realMouseLocation.y)) * Mathf.Rad2Deg);
    28.         Debug.Log(cannonAngle);
    29.     }
    30. }
    31.  
    32.  
    Pretty please help me understand why this error is being thrown and what I am not understanding here. If you guys have a better way to complete this task please enlighten me.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,421
    You're close! Your parentheses are wrong.

    Take a closer look.

    You are passing ONE argument into Mathf.Atan2() and it wants two arguments.

    SUPER easy mistake to make.