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

No definition for Atan2

Discussion in '2D' started by Zurvivor0412, Aug 7, 2016.

  1. Zurvivor0412

    Zurvivor0412

    Joined:
    Aug 7, 2016
    Posts:
    36
    I'm working on a project while following Brackeys 2D platformer tutorials on youtube and on his 5th episode, while following the code, i got to a point where it says that there is no definition for Atan2, when i completely followed his tutorial with no issues until then? Just wanted to get a fix so i can carry on making, Thanks.
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    You can get Atan2 by using; Mathf.Atan2()
     
  3. Zurvivor0412

    Zurvivor0412

    Joined:
    Aug 7, 2016
    Posts:
    36
    This is my code:

    using UnityEngine;
    using System.Collections;

    public class ArmRotation : MonoBehaviour
    {
    public int rotationOffset = 90;
    public object MathF { get; private set; }


    // Update is called once per frame
    void Update()
    {
    Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    difference.Normalize();

    float RotZ = MathF.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.Euler(0f, 0f, RotZ + rotationOffset);
    }

    }

    But it says Error CS1061 'object' does not contain a definition for 'Atan2' and no extension method 'Atan2' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) Any help?
     
  4. Gnatty

    Gnatty

    Joined:
    May 17, 2012
    Posts:
    77
    MathF is a static function, you do not need this line.
    Code (CSharp):
    1. public object MathF { get; private set; }
    And use like PGJ suggested ...

    Mathf.Atan2()
     
  5. Zurvivor0412

    Zurvivor0412

    Joined:
    Aug 7, 2016
    Posts:
    36
    When I delete the line of code
    Code (CSharp):
    1. public object MathF { get; private set; }
    It then displays...
    Error CS0103 The name 'MathF' does not exist in the current context
     
  6. Zurvivor0412

    Zurvivor0412

    Joined:
    Aug 7, 2016
    Posts:
    36
    As well where would I input Mathf.Atan2()?
     
  7. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,033
    It's "Mathf", not "MathF". Case matters.
     
  8. Gnatty

    Gnatty

    Joined:
    May 17, 2012
    Posts:
    77
  9. Zurvivor0412

    Zurvivor0412

    Joined:
    Aug 7, 2016
    Posts:
    36
    Thanks everyone it is now fixed and i will have a look at that manual!