- Home /
Finding the x and y of Mathf.Atan2 after converted to degrees
Hello I'm trying to figure out how solve the x and y of Math.Atan2 after it is calculated and converted into a Degree using Mathf.Rad2Deg since I want to add a small change in the angle like 15 degrees or so which can be changable. I am mainly trying to implement this so I can have a 2d platformer directional shooter that shoots multiple bullets at once for my enemies to add more difficulty to the game
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public float y;
public float x;
public class PointAndShoot : MonoBehaviour
{
void FixedUpdate() {
float rotationZ = Mathf.Atan2(y, x) * Mathf.Rad2Deg;
//Want to figure out if its possible to add or subtract degrees from rotationZ and figure out what the
//x and y are in that degree angle
}
}
Answer by Namey5 · Apr 08, 2020 at 04:16 AM
You can figure out where the new point [x,y] would be through trig identities;
//Calculate angle theta
float theta = Mathf.Atan2(y, x) * Mathf.Rad2Deg;
//Make whatever change you want to the angle, in this case subtract 10 degrees
theta -= 10f;
//Let's say we have an angle 'a', which we get from 'a = atan2 (y, x)'
//'atan' is the inverse of 'tan', therefore 'atan (tan (a)) = a'
//We also know that 'tan (a) = sin (a) / cos (a)'
//Therefore 'atan (sin (a) / cos (a)) = a'
//You'll notice that 'Vector2 (cos (a), sin (a))' is a point on the circumference of a unit circle rotated 'a' degrees
//This is actually the basis of how 'atan2' is calculated in the first place, i.e. 'atan2 (y, x) ~ atan (y / x)' (not strictly correct, but close enough for this example)
//Therefore, for a point/vector at 'theta' degrees; x = cos (theta), y = sin (theta)
float newX = Mathf.Cos (theta * Mathf.DegToRad);
float newY = Mathf.Sin (theta * Mathf.DegToRad);
You can read more about atan2 here;
Your answer
Follow this Question
Related Questions
how can i determine the angle of the face of an object relative to the world? 0 Answers
user input azimuth and elevation angle. Need Urgent Help 0 Answers
Shade Based on Normal Map World Position Angle (Updated) 1 Answer
Align line of units based on vectors 0 Answers
Clamp Rotation of an object depending of the Rotation of an other one 0 Answers