Hi all, i’ve recently moved from Game Maker to Unity. as support for GMS 1.4 stopped, and GMS 2 is too costly for my needs at the moment. c# is a new playground for me, and i’m still getting to grips with it from GML.
I’m creating a top down shooter and thought i’d get stuck in with some of Unity’s newest features.
Below is the code I’ve got to move and rotate my ship. It works great, moves and rotates in direction of left thumbstick fine. Except when you release the thumbstick, it snaps the ship to pointing to the right (or rotation of 0 I think), whereas I want it to keep it it’s last rotation angle. I believe this is because I reset Vector2 back to zero when left thumbstick movement is cancelled.
I am unsure how to cancel out left thumbstick input and stop ship from moving, but at the same not reset it’s rotation…
If anybody can help, or suggest a technique to look at that would be great. Or even am I going about the new input system massively wrong??
Thanks all in advance, any response, advice is much apprecaited!
public class shipcontrols : MonoBehaviour
{
public GameObject LaserProjectile;
public GameObject ship;
public Transform FirePoint;
Vector2 move;
PlayerControls controls;
void Awake()
{
controls = new PlayerControls();
controls.Gameplay.Fire.performed += ctx => Fire();
controls.Gameplay.Move.performed += ctx => move = ctx.ReadValue<Vector2>();
controls.Gameplay.Move.canceled += ctx => move = Vector2.zero;
}
void Update()
{
FirePoint.rotation = transform.rotation;
float deadzone = 0.3f;
Vector2 stickInput = new Vector2(move.x, move.y);
if (stickInput.magnitude < deadzone)
{
stickInput = Vector2.zero;
}
else
{
Vector2 m = new Vector2(move.x, move.y) * Time.deltaTime;
transform.Translate(m, Space.World);
}
float angle = Mathf.Atan2(move.y, move.x) *Mathf.Rad2Deg;
ship.transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}
void OnEnable()
{
controls.Gameplay.Enable();
}
void OnDisable()
{
controls.Gameplay.Disable();
}
void Fire()
{
Instantiate(LaserProjectile, FirePoint.position, FirePoint.rotation);
}
}