- Home /
How do I rotate an object using Vector2 inputs? (New Input System)
I have a dash ability in my game that causes the player to dash towards the mouse or the direction the controller knob is being pressed. That all works fine. I want to rotate an arrow-like game object using the input to indicate where the player will dash. I already have a reference to the game object:
public GameObject dashAim;
But i'm not sure how to use the inputs for the rotation. I've had help with this before, but this was using the old input system:
public class RotateToMouse : MonoBehaviour
{
void Update()
{
//rotation
Vector3 mousePos = Input.mousePosition;
mousePos.z = 5.23f;
Vector3 objectPos = Camera.main.WorldToScreenPoint(transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}
}
i'm just not quite sure how to repurpose this with the new inputs. I already tried this:
dashAim.transform.rotation = Quaternion.Euler(new Vector3(direction.x, direction.y));
but it didn't do anything. Does anyone know how I can do this? Here's the code i'm using for the inputs, thanks to @Hellium
// To be called instead of the Dash function when adding the listener
public void ReadMouseInput(InputAction.CallbackContext context)
{
Vector2 mousePosition = context.ReadValue<Vector2>();
Vector3 mousePosition3 = mousePosition;
Vector3 direction = (mousePosition3 - camera.WorldToScreenPoint(rb.transform.position)).normalized;
Dash(new Vector2(direction.x, direction.y));
}
// Callback of the Left Stick
public void ReadStick(InputAction.CallbackContext context)
{
Vector2 stickDirection = context.ReadValue<Vector2>().normalized;
Dash(new Vector2(stickDirection.x, stickDirection.y));
}
I'd really appreciate any help with this. Thanks!
Answer by Hellium · Sep 03, 2021 at 07:41 PM
You might be able to do something similar to how you handle dash.
public class RotateToMouse : MonoBehaviour
{
void Start()
{
// Add the listeners to the actions
}
void Update()
{
// Not needed anymore
}
public void ReadMouseInput(InputAction.CallbackContext context)
{
Vector2 mousePosition = context.ReadValue<Vector2>();
Vector2 objectPosition = (Vector2) Camera.main.WorldToScreenPoint(transform.position);
Vector2 direction = (mousePosition - objectPosition).normalized;
RotateAim(direction);
}
public void ReadStick(InputAction.CallbackContext context)
{
Vector2 stickDirection = context.ReadValue<Vector2>().normalized;
RotateAim(stickDirection);
}
public void RotateAim(Vector2 direction)
{
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}
}
worked great! Only issue was that it was rotating the whole player at first, but I got it sorted out by changing
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
to
dashAim.transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
Your answer
Follow this Question
Related Questions
Adding an offset to instantiated object based on mouse distance from player 1 Answer
How do I use controller knob input like mouse position? (New Input System) 1 Answer
How do I use an Input axis instead of Input.mousePosition??? 1 Answer
How can I stop getting mouse position of Ui Elements? 0 Answers
Input System: Input Action "started" Callback doesn't work properly on controller triggers? 1 Answer