Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Oct 11, 2021 at 06:03 AM by pommerose94 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by pommerose94 · Oct 10, 2021 at 10:57 AM · controllertopdownshooter

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

alt text

but on right joystick release, my player automatically aims top again.

alt text

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 ^^

1.png (5.5 kB)
2.png (3.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pommerose94 · Oct 11, 2021 at 06:02 AM 0
Share

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

Answers Answers and Comments

136 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges