Quantcast
Channel: Questions in topic: "gun"
Viewing all articles
Browse latest Browse all 592

Rotation Flicker in 2d platformer gun

$
0
0
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(); 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(); width = GetComponent().bounds.extents.x * 2.0f; height = GetComponent().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; } } ,

Viewing all articles
Browse latest Browse all 592

Trending Articles