• 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 /
avatar image
0
Question by scroolerr · Aug 03, 2020 at 08:20 AM · 2d-platformer2d rotation

Player arm rotation bugs for 1-2 frames when aiming at mouse.

Hey, so basically, my player arm bugs and aims down or up for 1 or 2 frames when I flip my gun after 90 degrees, and when im shooting meanwhile doing that, the fire function can fire in those 2 frames, and some bullets go up and down instead of the direction im aiming, here's my script for movement:

 using UnityEngine;
 using CodeMonkey.Utils;
 
 public class Movement : MonoBehaviour
 {
     // Stuff
     Animator Animator;
     public AudioSource PuloSom,PuloDSom;
     Rigidbody2D rb;
 
     // Variables
     [SerializeField]
     float jumpStrenght = 26f;
     [SerializeField]
     float vel = 8f;
     public static bool face;
 
     // Ground Check
     public Transform groundCheck;
     public LayerMask whatIsGrounded;
     public bool grounded = false;
 
     // Stuff
     float fJumpPressedRemember = 0;
     [SerializeField]
     float fJumpPressedRememberTime = 0.2f;
 
     // Shooting
     public GameObject bullet;
     public AudioSource TiroSom, SomHit;
     public GameObject cano;
     public GameObject MuzzleLight;
 
     // Aiming
     public GameObject ArmPivot;
     Vector3 MousePos;
     void Start()
     {
         rb = GetComponent<Rigidbody2D>();
         ArmPivot = GameObject.Find("ArmPivot");
         Animator = GetComponent<Animator>();
     }
     // FixedUpdate runs faster than normal update, this is good for physics, etc.
     void Update()
     {
         if (!GameMenuManager.GamePaused)
         {
             if (!PlayerManager.Dead)
             {
                 Jump();
                 HandleShoot();
             }
         }
     }
     void FixedUpdate()
     {
         if (!GameMenuManager.GamePaused)
         {
             if (!PlayerManager.Dead)
             {
                 // Checks if the groundcheck (son of player) is touching something with the layer Ground
                 grounded = Physics2D.OverlapCircle(groundCheck.position, 0.15f, whatIsGrounded);
                 Move();
                 HandleAiming();
 
                 if (MousePos.x < transform.position.x && !face)
                 {
                     Flip();
                     ArmPivot.transform.localScale = new Vector2(ArmPivot.transform.localScale.x * -1, ArmPivot.transform.localScale.y * -1);
                 }
                 else if (MousePos.x > transform.position.x && face)
                 {
                     Flip();
                     ArmPivot.transform.localScale = new Vector2(ArmPivot.transform.localScale.x * -1, ArmPivot.transform.localScale.y * -1);
                 }
             }
         }
     }
     void Move()
     {
         if (!GameMenuManager.GamePaused)
         {
             if (!PlayerManager.Dead)
             {
 
                 if (Input.GetAxisRaw("Horizontal") > 0.1)
                 {
                     rb.velocity = new Vector2(vel, rb.velocity.y);
                     Animator.SetFloat("Moving",1);
                 }
                 else if (Input.GetAxisRaw("Horizontal") < -0.1)
                 {
                     rb.velocity = new Vector2(-vel, rb.velocity.y);
                     Animator.SetFloat("Moving",1);
                 }
                 else if (Input.GetAxisRaw("Horizontal") == 0 && grounded)
                 {
                     rb.velocity = new Vector2(0, rb.velocity.y);
                     Animator.SetFloat("Moving",0);
                 }
             }
         }
     }
     void Jump()
     {
         fJumpPressedRemember -= Time.deltaTime;
         if (Input.GetKeyDown(KeyCode.UpArrow) || (Input.GetKeyDown(KeyCode.W)))
         {
             fJumpPressedRemember = fJumpPressedRememberTime;
         }
 
         if(fJumpPressedRemember > 0 && grounded)
         {
             fJumpPressedRemember = 0;
             rb.velocity = Vector2.up * jumpStrenght;
             Instantiate(PuloSom, new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y, this.gameObject.transform.position.z), Quaternion.identity);
         }
     }
     void Flip()
     {
         // Flipping the character depending on the input, showed before on Update
         if (!GameMenuManager.GamePaused)
         {
             face = !face;
             Vector3 Scala = this.transform.localScale;
             Scala.x *= -1;
             this.transform.localScale = Scala;
         }
     }
     void HandleAiming()
     {
         MousePos = UtilsClass.GetMouseWorldPosition();
         Vector3 Direction = (MousePos - ArmPivot.transform.position).normalized;
         float angle = Mathf.Atan2(Direction.y, Direction.x) * Mathf.Rad2Deg;
         ArmPivot.transform.eulerAngles = new Vector3(0, 0, angle);
 
         if (angle > 90 || angle < -90)
         {
             ArmPivot.transform.localScale = new Vector2(-1,-1);
         }
         else
         {
             ArmPivot.transform.localScale = new Vector2(1,1);
         }
     }
     void HandleShoot()
     {
         if (!GameMenuManager.GamePaused)
         {
             if (Input.GetMouseButtonDown(0))
             {
                 Instantiate(bullet, new Vector3(cano.transform.position.x, cano.transform.position.y, cano.transform.position.z), cano.transform.rotation);
                 Animator.SetBool("IsShooting", true);
                 MuzzleLight.SetActive(true);
                 Instantiate(TiroSom, new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y, this.gameObject.transform.position.z), Quaternion.identity);
             }
             else
             {
                 MuzzleLight.SetActive(false);
                 Animator.SetBool("IsShooting", false);
             }
         }
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

142 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Slope Rotation 0 Answers

Instantiate a GameObject with a specific Z rotation 2 Answers

(2D sidescrolling platformer) Flipping my character right or left depending on the mouse? 2 Answers

RotateAround using physics 1 Answer

2d shooting problem 1 Answer


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