- Home /
The question is answered, right answer was accepted
2D top down shooter controller: on joystick release, player automatically faces top. Ideas?
Hello, im still quite a beginner on Unity.
I have a question concerning my 2D topdown shooter game. The game can be played on controller. The left joystick is then used to control the player movement, and the right to control the camera. When you move the right joystick, you are able to aim towards any direction, but on joystick release, the player automatically aims top, to degree 0 again, here I will attach two photos to help you understand. On picture 1 i aim in a specific direction with my right joystick, by maintaining the joysticks position
but on right joystick release, my player automatically aims top again.
here is my code to control the player direction, (a public bool exists to choose if you want a controller or mouse control)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovementP1 : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rb;
Vector2 movement;
Vector2 mousePos;
public Camera cam;
public bool useController;
internal static object instance;
// Update is called once per frame
void Update()
{
if (!PauseMenu.gameIsPaused)
{
movement.x = Input.GetAxisRaw("HorizontalP1");
movement.y = Input.GetAxisRaw("VerticalP1");
if (!useController)
{
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
}
if (useController)
{
Vector2 lookDir = new Vector2(Input.GetAxisRaw("RHorizontalP1"), -Input.GetAxisRaw("RVerticalP1"));
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
if (lookDir.sqrMagnitude > 0.0f)
{
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
}
if(speedenhancer.speedEnhanceP1)
{
if (moveSpeed != 5.5f)
{
moveSpeed = 5.5f;
}
}
}
void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
if (!useController)
{
Vector2 lookDir = mousePos - rb.position;
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
rb.rotation = angle;
}
}
}
it is not an enormous issue but i want that the player aims in the same direction as before the joystick release, if you know what i mean, i just think it is more intuitive. I hope i was clear enough and would be so happy to receive help to resolve this small issue ^^
Answer by Rechronicle · Oct 11, 2021 at 03:41 AM
It's because you check the direction directly to input, when you release controller input, it will go back to the default state.
if(direction.sqrMagnitude > 0.1f)
add this if statement before checking on the direction. It basically prevents any changes if we're on zero state/released state.
Oh thanks, i actually just needed to replace the (lookdir.sqrMagnitude from 0.0 to 0.1f. Thank you so much and it was so basic, sorry :)
Follow this Question
Related Questions
Any good FPS Character Controller Script? 1 Answer
Writing weapon imprecision 1 Answer
Need help with top down controller script 1 Answer
Third Person Controller 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers