- Home /
Question by
matthew_gigante · Apr 24, 2018 at 01:39 AM ·
c#unity 2dtransform.rotationflippinggunfire
Unity2d shooting bullets is off when the gun is rotated to the left
Pictured above is my player (blue rectangle) shooting a gun. The bullets were both fired from the same x value, but the gun was flipped when the gun was on the left. When the player moves their mouse to the right of the player, the gun faces the right, and bullets shoot from the barrel when clicked. When the player moves their mouse to the left, the gun's y axis is flipped, but while like this, bullets shoot from the handle of the gun, not where they are supposed to. How can I make the bullets fire from the same spot when the y axis is flipped on the gun? My code is here:
Player controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(AudioSource))]
public class playerController : MonoBehaviour {
public GameObject player;
public GameObject mainCamera;
public GameObject bullet;
public GameObject gun;
public int moveSpeed;
public int jumpSpeed;
public int doubleJump;
public int cameraX;
public int cameraY;
public int cameraZ;
public int playerValue;
public Rigidbody2D rb;
public bool onFloor;
public bool cameraMove;
// Use this for initialization
void Start () {
doubleJump = 0;
cameraZ = -10;
rb = GetComponent<Rigidbody2D>();
AudioSource audio = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update () {
//movement section
if (Input.GetKeyDown (KeyCode.Space) && doubleJump < 2) {
doubleJump++;
jump ();
}
if (Input.GetKeyDown (KeyCode.Mouse0)) {
Instantiate (bullet, gun.transform.position, gun.transform.rotation);
}
}
void FixedUpdate(){
if (Input.GetKey (KeyCode.D)) {
rb.velocity = new Vector2 (moveSpeed, rb.velocity.y);
}
if (Input.GetKey (KeyCode.A)) {
rb.velocity = new Vector2 (-moveSpeed, rb.velocity.y);
}
}
void OnCollisionEnter2D(Collision2D coll) {
if (coll.gameObject.tag == "floor") {
onFloor = true;
doubleJump = 0;
}
}
public void jump() {
//rb.AddForce (transform.up * jumpSpeed);
rb.velocity = new Vector2(0,0);
GetComponent<Rigidbody2D> ().AddForce (new Vector2(0, jumpSpeed));
//GetComponent<Rigidbody2D> ().AddForce (transform.up * jumpSpeed);
onFloor = false;
}
}
Bullet Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bulletScript : MonoBehaviour {
public float speed;
public Rigidbody2D rb;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void FixedUpdate () {
rb.AddForce (transform.right * speed);
}
}
Arm rotation script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class armRotation : MonoBehaviour {
public int rotationOffset = 0;
public GameObject gun;
private SpriteRenderer mySpriteRenderer;
void Start(){
mySpriteRenderer = gun.GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void FixedUpdate () {
Vector3 mousePos = Input.mousePosition;
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
mousePos = mousePos - pos;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg - rotationOffset));
if (transform.rotation.z > 0.70 || transform.rotation.z < -0.70) {
mySpriteRenderer.flipY = true;
} else
if (transform.rotation.z < 0.70 || transform.rotation.z > -0.70) {
mySpriteRenderer.flipY = false;
}
}
}
gun.png
(16.5 kB)
Comment