Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by bloxcool4 · Mar 28, 2020 at 09:45 PM · rotationunity 2dplatformergungun script

Rotation Flicker in 2d platformer gun

So I've gotten started with Unity recently, and I have been working on a 2d platformer with shooting. Everything is going smoothly but I couldn't help but notice a little flicker whenever I rotate the gun in my game- the rotation works fine but when say the player stops moving right and starts moving left, the rotation flickers for a moment until it goes back to the correct position. I have the gun as a child of the player, and the transform where the bullet comes out as a child of the gun.

Problem can be seen here: https://gyazo.com/0fdb979b2e7e7d8b8448601b5e38fcbb

Script for gun (child of Player):

 public class ShootGun : MonoBehaviour
 {
     public float shotSpread;
     public int pauseRate;
     public Transform bSpawnLocation;
     public GameObject Bullet;
 
     private Vector3 mousePos;
     private Vector3 objectPos;
     private float angle;
     private float rotationAngle;
     private bool mousePressed;
     private GameObject bullet;
     private Rigidbody2D bulletRb;
 
     void Update()
     {
         mousePos = Input.mousePosition;
         mousePos.z = 0;
 
         objectPos = Camera.main.WorldToScreenPoint(transform.position);
         mousePos.x = mousePos.x - objectPos.x;
         mousePos.y = mousePos.y - objectPos.y;
 
         angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
         Debug.Log(angle);
         transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
 
         rotationAngle = (transform.eulerAngles.z < 180f) ? transform.eulerAngles.z : 360 - transform.eulerAngles.z;
 
         if (rotationAngle > 90)
         {
             if (transform.localScale.y > 0)
             {
                 transform.localScale = new Vector3(transform.localScale.x, -transform.localScale.y, transform.localScale.z);
             }
         }
         else if (rotationAngle < 90)
         {
             if (transform.localScale.y < 0)
             {
                 transform.localScale = new Vector3(transform.localScale.x, -transform.localScale.y, transform.localScale.z);
             }
         }
 
         if (mousePressed)
         {
             if (Time.frameCount % pauseRate == 0)
             {
                 bullet = Instantiate(Bullet, bSpawnLocation.position, Quaternion.Euler(new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, angle - 90)));
                 bulletRb = bullet.GetComponent<Rigidbody2D>();
                 bulletRb.velocity = new Vector3(Mathf.Cos((angle + Random.Range(-shotSpread, shotSpread)) * Mathf.Deg2Rad) * 20, Mathf.Sin((angle + Random.Range(-shotSpread, shotSpread)) * Mathf.Deg2Rad) * 20, 0);
             }
         }
 
         if (Input.GetMouseButtonDown(0))
         {
             mousePressed = true;
         }
         else if (Input.GetMouseButtonUp(0))
         {
             mousePressed = false;
         }
     }
 }

Main script for Player (should be self explanatory):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
     public float moveSpeed;
     public float jumpForce;
 
     private Rigidbody2D rb;
     private Vector2 velocity;
     private float width;
     private float height;
     private Vector3 scale;
     private Vector3 position;
     private float xScale;
     private bool canJump;
 
     void Start()
     {
         rb = GetComponent<Rigidbody2D>();
         width = GetComponent<Collider2D>().bounds.extents.x * 2.0f;
         height = GetComponent<Collider2D>().bounds.extents.y * 2.0f;
         scale = transform.localScale;
         xScale = scale.x;
     }
 
     void Update()
     {
         position = transform.position;
     }
 
     public void ProcessInput(float horizontal, bool jumpKeyPressed)
     {
         velocity = rb.velocity;
         velocity.x = horizontal * moveSpeed;
         rb.velocity = velocity;
 
         if (jumpKeyPressed && CanJump())
         {
             velocity.y = jumpForce;
             rb.velocity = velocity;
         }
 
         if (velocity.x < 0.0f)
         {
             scale.x = -xScale;
             transform.localScale = scale;
 
             if (transform.GetChild(0).gameObject.transform.localScale.x > 0)
             {
                 transform.GetChild(0).gameObject.transform.localScale = new Vector3(-transform.GetChild(0).gameObject.transform.localScale.x, transform.GetChild(0).gameObject.transform.localScale.y, transform.GetChild(0).gameObject.transform.localScale.z);
             }
         }
         else if (velocity.x > 0.0f)
         {
             scale.x = xScale;
             transform.localScale = scale;
 
             if (transform.GetChild(0).gameObject.transform.localScale.x < 0)
             {
                 transform.GetChild(0).gameObject.transform.localScale = new Vector3(-transform.GetChild(0).gameObject.transform.localScale.x, transform.GetChild(0).gameObject.transform.localScale.y, transform.GetChild(0).gameObject.transform.localScale.z);
             }
         }
     }
 
     private bool CanJump()
     {
         if (Physics2D.BoxCast(transform.position, new Vector2(width, height), 0.0f, Vector2.down, 0.1f))
         {
             return true;
         }
 
         return 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.

Follow this Question

Answers Answers and Comments

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

Inaccurate shooting script for gun 1 Answer

Why won't the coin disappear when the player collides with it? 1 Answer

Problem with the gun reloading 0 Answers

how would I make a capsule rotate and go down a platform on the y axis? 0 Answers

How do I make a client hosted game with multiple character classes? 0 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