- Home /
How to increase the range of the player aiming?
This is my whole script, I added it as a component to an anchor. in the script, the anchor should follow the mouse position and rotate at its z-axis BUT: The mouse moves 360 ° and I don't want my anchor to move like that. so The mouse should just follow the anchor when it is in front of the player
//__
public class aiming: MonoBehaviour
{
public GameObject player;
public GameObject anker;
public void FixedUpdate()
{
Aim();
}
public void Aim()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float y = anker.transform.rotation.y;
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
difference.Normalize();
transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
}
}
//___
Answer by unity_3DmGkY57st7DXQ · Jul 04, 2019 at 02:20 PM
The solution I found is to make him just aim, when the mouse in inRange, to check that, I checked the value of the rotation in the z-axis. the value I wanted is 60 from both sides (60 and -60), that's why I used the Mathf.Abs method. here is my code now:
public class aiming : MonoBehaviour
{
public bool inRange = false;
public GameObject player;
public void FixedUpdate()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
//-------------------SOLUTION--------------------------------------
if (Mathf.Abs(rotationZ) >= 60)
{
inRange = true;
}
else
{
inRange = false;
}
if (!inRange)
{
Aim();
}
}
//---------------------------------------------------------------------------
public void Aim()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
difference.Normalize();
transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
}
}
Answer by Optrix · Jul 04, 2019 at 04:31 AM
So to simplify the question, you'd like to know if a particular point is in front of or behind a GameObject?
There are probably more efficient ways, but I like the Unity plane class.
You create a plane given the forward vector and position of your player. It has functions for determining size, distance and nearest point etc.
Plane P = new Plane(player.transform.Forward,player.transform.position);
if (P.GetSide(transform.position))
{
//Yes, this is "In Front"
}
You can also compare the angle (Vector3.Angle) of your forward vector and the difference between the GameObjects positions if you'd like to narrow your field further - for example, the front 100 degrees rather than the full front 180 degrees.
float Angle = Vector3.Angle(player.transform.forward, (transform.position - player.transform.position).normalized);
This is even in degrees, so you don't need to deal with radian conversion.
first one doesn't work. the second one sounds good.
-------------------------------------------------------------------------------------------------------------------------
first, I will try to make it more clear: the player is the parent of the anchor. the anchor is the " point of rotation" and it has the arm sprite as a child. In my script, the rotation of the anchor depends on the position of the mouse cursor. this rotation is at the z-axis. When the mouse is in front of the player, its corner is between 90° and -90° degrees. the behind the player is it then from -90 to -180 and from 90 to 180, 180 and - 180 are at the same point (the middle), in that point on the other side is 0.
-------------------------------------------------------------------------------------------------------------------------
For your second solution, what is the angle, when the mouse is in front of the player? I should do it in an if statement, right? Thank you
Thank you very much for trying to help me, I already found a solution:)
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Character Not Rotating 0 Answers
how can i stop rotation without stopping y axis 2 Answers
[C#]Angle of shooting equale to mouse direction 1 Answer
How to lock z axis rotation 2 Answers