Im trying to rotate something to the mouses position. Im using this script:
public int offset;
// Update is called once per frame
void Update()
{
Vector3 diffrance = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float RotZ = Math.Atan2(diffrance.y, diffrance.x) * Math.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, RotZ + offset);
}
The only error that I get is:
‘Math’ does not contain a definition for ‘Rad2Deg’
How do I fix this because I think Math does contain a definition for Rad2Deg.
System.Math does indeed not contain Rad2Deg. However, UnityEngine.Mathf does, so that’s the class you’re actually looking for.
I put Using UnityEngine.Mathf And got this error:
A ‘using namespace’ directive can only be applied to namespaces; ‘Mathf’ is a type not a namespace. Consider a ‘using static’ directive instead
Any thing Im doing wrong? Im on version 2019.3
Mathf isn’t a namespace, it is a class. You either directly specify it by calling it with UnityEngine.Mathf, or you use the namespace UnityEngine by itself and call just Mathf. If you’re in a standard monobehavior I believe they already have the namespace unityengine added so Mathf by itself should be fine.
that doesn’t seem to be working either…
using System;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
public class WeponAim : MonoBehaviour
{
public int offset;
// Update is called once per frame
void Update()
{
Vector3 diffrance = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float RotZ = Math.Atan2(diffrance.y, diffrance.x) * UnityEngine.Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, RotZ + offset);
}
}
error:
CS0266: Cannot implicitly convert type ‘double’ to ‘float’. An explicit conversion exists (are you missing a cast?)
I know that doesn’t look right but IDK maybe Im just dumb.
Math is a double library as I recall, so you’ll have to do a convert on it. Easiest way is just to use:
float RotZ = (float)Math.Atan2(diffrance.y, diffrance.x) * UnityEngine.Mathf.Rad2Deg;
Ok this worked thank you so much. Really appreciate it.