![]() So to start with, I added an Asteroid into my game scene and applied the appropriate components to it such as a 2D Rigidbody and a Collider (I used the Circle collider as I thought it best fitted the asteroid) and I also removed the gravity scale so that there was no gravity on the Asteroid.Īfter I had done this I then went on to create a new ‘C Sharp’ script for the asteroid named, ‘AsteroidController’. This was so that the player could shoot asteroids so that they could prevent themselves from being destroyed by an asteroid (which is the whole aim of the game). If you remember from last time I had created the player shooting and fire rate… You’ll notice we left the x and y parameters of this Vector3 as zero that is because we don’t want to rotate on the x and y axis.Today I did more work on my individual Unity game. We do this by calling the Quaternion.Euler method and passing in a new Vector3 object. Once we have our angle in degrees, which we stored in the angle variable, we then convert this into a Quaternion, as this is what Unity stores the rotation value in. The reason we do this is because it gives us a vector which is relative to the character / the arrow game object, rather than to the 0,0 point of the screen itself. The picture below ( Source) illustrates visually how the number of radians is calculated using the Mathf.Atan2 method.Īs can be seen in the illustration above, the points passed to the arctangent function are always relative to an origin at point 0,0, which is why before we call the Mathf.Atan2 method in our script, we first find the position of the player on the screen and then deduct that from where our mouse is positioned. This method will return the angle in Radians which we can then multiply by the constant value defined in Mathf.Rad2Deg to get the degrees by which we need to rotate the arrow. In order to determine the angle our arrow needs to be displayed at, we need to find the arctangent of the mouse’s position on screen which we do by calling the Mathf.Atan2 method. To understand what this script does, we first need to wrap our heads around some basic trigonometry (if you don’t care about why this works, you can skip this part, but I’d advise reading and understanding why). Using UnityEngine using System.Collections public class AimAssist : MonoBehaviour In your project inspector, create a new script called “AimAssist” (or whatever you prefer, the class for this example will be named AimAssist though) and paste the following code in: You can see the pivot point in between the top left corner and bottom left corner: In my case, I wanted the arrow to rotate around the character’s neck, as can be seen below, so I have placed it with the pivot point at the character’s neck. Now, drag the arrow sprite into your scene, and ensure it is a child of your character’s game object so that it’s positioning is always relative to your game object and position it so that the pivot point is in the exact place you want it to rotate around. ![]() If you’re using the arrow I linked above, you can do this by setting the “Pivot” option to “Left” in the Inspector window, as I have made this arrow with the bottom of it on the left hand side: All that we need to do is import the arrow we intend to use, and set the pivot point to be at the bottom-center of our arrow. There’s not much we have to do in way of setting up our objects. ![]() This guide will show you how to create a feature that has become a requirement in one of my latest projects that being an aim assist which will rotate around your character to point in the same direction as where the mouse is on screen.īelow are two screenshots which illustrate where the mouse is positioned with a black X, showing how the arrow rotates to point to it:Īny old arrow graphic will do for the purpose of this guide, but for convenience’s sake the one I am using in the illustration above can be found by Clicking Here (it is all white, so it may look like it hasn’t loaded, but just save it, it’ll be fine). Creating a 2D Rotating Aim Assist in Unity 4.3
0 Comments
Leave a Reply. |