- Home /
New Input system mouse returns wrong values
I've been trying to use the new input system to allow both mouse and game-pad to control where thruster is located around another object (Blaster game object) based on the direction to a target rotating around a pivot (Similar to this but instead of rotating in object space it should rotate in world space around a point: )
It all works fine when as target I use another game object's in the world the rotation works perfectly (see below) https://youtu.be/jzZpOynPUZY
but when I switch to use the mouse position as the target a nasty glitch happens when the mouse is around 45° from the main pivot object (see below) https://youtu.be/Sxip_k23GEU
I can't say it only happens when cursor is at 45° as sometimes it also happens just around 85° (so I would constrain this problem to the first quadrant)
This is how I achieve this, but I can't seem to find the issue with my code as I have confirmed the raw values received by the mouse are incorrect at times.
#define INPUT
using UnityEngine;
using UnityEngine.InputSystem;
#if INPUT
#else
[ExecuteInEditMode]
#endif
public class BlasterController : MonoBehaviour
{
public Transform pivot;
public Transform target;
public float radius = 1.5f;
public LineRenderer lineRenderer;
Vector2 aimDirection;
Vector2 previousAimDirection;
// Unity MonoBehaviors
private void Awake()
{
_controls = new InputMaster();
_controls.Gameplay.Aim.performed += ctx => aimDirection = ctx.ReadValue<Vector2>();
//_controls.Gameplay.Aim.canceled += ctx => aimDirection = Vector2.zero;
}
private void OnEnable()
{
_controls.Enable();
}
private void OnDisable()
{
_controls.Disable();
}
// Update is called once per frame
void Update()
{
Debug.Log($"Aim direction [{aimDirection.x},{aimDirection.y}]");
#if INPUT
if (aimDirection.x > 0f && aimDirection.y > 0f)
{
var gamepad = Gamepad.current;
var mouse = Mouse.current;
if (gamepad != null)
{
aimDirection.Normalize();
}
else if (mouse != null)
{
Vector3 screenSpaceMousePos = new Vector3(aimDirection.x, aimDirection.y, -Camera.main.transform.position.z);
Vector3 worldSpaceMousePos = Camera.main.ScreenToWorldPoint(screenSpaceMousePos);
Vector2 mousePos = new Vector2(worldSpaceMousePos.x, worldSpaceMousePos.y);
Vector2 pivotPos = new Vector2(pivot.position.x, pivot.position.y);
aimDirection = mousePos - pivotPos;
aimDirection.Normalize();
}
#else
Vector2 targetPos = new Vector2(target.position.x, target.position.y);
Vector2 pivotPos = new Vector2(pivot.position.x, pivot.position.y);
aimDirection = targetPos - pivotPos;
aimDirection.Normalize();
#endif
var inverseOrbit = aimDirection * -radius;
transform.position = inverseOrbit;
if (lineRenderer && lineRenderer.enabled)
{
lineRenderer.positionCount = 2;
lineRenderer.startWidth = 0.3f;
lineRenderer.endWidth = 0.3f;
lineRenderer.SetPosition(0, pivot.position);
lineRenderer.SetPosition(1, inverseOrbit);
}
else
Debug.LogError("noLineRendererFound");
#if INPUT
}
else
{
Debug.LogError($"the aim direction goes negative!! [{aimDirection.x},{aimDirection.y}]");
Debug.LogError($"Previously was [{previousAimDirection.x},{previousAimDirection.y}]");
}
previousAimDirection = aimDirection;
#endif
}
private PlayerInput _playerInput;
private InputMaster _controls;
}
Has anyone got any idea why this is happening?
Your answer
Follow this Question
Related Questions
Input.GetButton not working 1 Answer
How do you detect if two specific objects are touching each other without collision. 3 Answers
2D Top-Down Movement: Transform.up and right don't change 2 Answers
Drawing projectile trajectory 5 Answers
How can I stop the player movement from being too fast while moving diagonally in my 2D game? 2 Answers