- Home /
Need help Adding Physics based recoil to my gun
In my game, I have 2 spheres that have a gun rotate around them. They are able to shoot and charge up shots with their guns. But what I want to do is add a recoil force based on the size of the bullet that they shoot. But I have been having SO MUCH trouble with getting the forces to work just right. If Anyone has any sort of input please let me know
Here's my code for the shooting and player movement: public class PlayerMovement1 : MonoBehaviour { //Global variable for all players public static float bulletSpeed = 3f; public float sizeIncreaseRate = 0.4f; public float playerSpeed = 10f; public float jumpSpeed = 50; public float recoil = -25;
public GameObject pivot;
public GameObject bulletEmitter;
public GameObject bullet;
public GameObject bulletInstance;
private Rigidbody rb;
private Rigidbody bulletRB;
private bool isReleased = true;
private bool m_isAxisInUse = false;
private bool inAir = false;
public bool isExpanding = false;
private float bulletSizetoRecoil;
//player control variables
private float rTriggerRaw;
private float moveHorizontal;
private float rsVertical;
private float rsHorizontal;
private float rTrigger;
private float xAxis;
private float yAxis;
private float playerNumber;
private bool aButton;
void Start()
{
//Gets the component of the player's RigidBody
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
Vector3 movement = new Vector3(moveHorizontal, rb.velocity.y);
rb.MovePosition(transform.position + (movement * playerSpeed * Time.deltaTime));
//Jump when the player presses A
if (aButton && inAir == false)
{
rb.AddForce(0, jumpSpeed * 2f, 0, ForceMode.Force);
inAir = true;
}
//Controls the aim of the gun depending on the angle of the right joystick
if (yAxis != 0 || xAxis != 0)
{
float joystickAngle = Mathf.Atan2(xAxis, yAxis) * Mathf.Rad2Deg;
pivot.transform.rotation = Quaternion.Slerp(pivot.transform.rotation, Quaternion.Euler(pivot.transform.rotation.x, pivot.transform.rotation.y, joystickAngle - 90F), Time.deltaTime * 5);
}
Shoot();
}
void Update()
{
if (gameObject.name == "Player 2")
{
rTriggerRaw = Input.GetAxisRaw("Right Trigger1");
moveHorizontal = Input.GetAxis("Horizontal1");
rsVertical = Input.GetAxis("Mouse Y1");
rsHorizontal = Input.GetAxis("Mouse X1");
rTrigger = Input.GetAxis("Right Trigger1");
aButton = Input.GetButton("A Button1");
xAxis = Input.GetAxis("Mouse X1");
yAxis = Input.GetAxis("Mouse Y1");
playerNumber = 2;
}
if (gameObject.name == "Player 1")
{
rTriggerRaw = Input.GetAxisRaw("Right Trigger");
moveHorizontal = Input.GetAxis("Horizontal");
rsVertical = Input.GetAxis("Mouse Y");
rsHorizontal = Input.GetAxis("Mouse X");
rTrigger = Input.GetAxis("Right Trigger");
aButton = Input.GetButton("A Button");
xAxis = Input.GetAxis("Mouse X");
yAxis = Input.GetAxis("Mouse Y");
}
}
//If thes player touches the ground then the player is no longer in the air
void OnCollisionEnter(Collision ground)
{
if (ground.collider.tag == "Ground")
{
inAir = false;
}
}
//Names the bullet depending on which player shoots the bullet
void NameBullet()
{
bulletInstance.gameObject.name = "Bullet " + playerNumber;
}
void Shoot()
{
if (rTriggerRaw != 0 )
{
if (m_isAxisInUse == false)
{
bullet.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
bulletInstance = Instantiate(bullet, bulletEmitter.transform.position, pivot.transform.rotation);
m_isAxisInUse = true;
}
bulletInstance.GetComponent<Bullet>().isExpanding = true;
bulletInstance.transform.localScale += new Vector3(sizeIncreaseRate * Time.deltaTime, sizeIncreaseRate * Time.deltaTime, sizeIncreaseRate * Time.deltaTime);
bulletSizetoRecoil = bulletInstance.transform.localScale.x * 2f;
bulletInstance.GetComponent<Bullet>().bulletSizeToKnockBack = bulletInstance.transform.localScale.x * 3;
if (bulletInstance.transform.localScale.x >= 1.25f || bulletInstance.transform.localScale.y >= 1.25f || bulletInstance.transform.localScale.z >= 1.25f)
{
bulletInstance.transform.localScale = new Vector3(1.25f, 1.25f, 1.25f);
}
if (m_isAxisInUse == true)
{
bulletInstance.transform.position = bulletEmitter.transform.position;
}
}
if (rTriggerRaw == 0)
{
if (bulletInstance != null)
{
bulletRB = bulletInstance.GetComponent<Rigidbody>();
if (bulletRB != null)
{
isReleased = true;
if (bulletSizetoRecoil >= 1.1 && isReleased == true)
{
rb.AddForce(pivot.transform.right * bulletSizetoRecoil * recoil * Time.deltaTime, ForceMode.Impulse);
isReleased = false;
}
bulletInstance.GetComponent<Bullet>().isExpanding = false;
bulletRB.AddForce(pivot.transform.right * bulletSpeed, ForceMode.VelocityChange);
NameBullet();
m_isAxisInUse = false;
Destroy(bulletInstance, 2f);
}
}
}
}
}
Here's the code for the Bullet:
public GameObject player;
public GameObject pivot;
public GameObject explosionEffect;
private Rigidbody collisionRB;
private Collider bulletCollider;
public bool isExpanding = false;
public float bulletSizeToKnockBack;
void Start ()
{
bulletCollider = GetComponent<Collider>();
}
void Update()
{
if (isExpanding == true) {bulletCollider.enabled = false;}
if (isExpanding == false) { bulletCollider.enabled = true;}
}
void OnCollisionEnter(Collision collision)
{
if (collision.collider.tag == ("Player") && isExpanding == false)
{
collisionRB = collision.collider.GetComponent<Rigidbody>();
collisionRB.AddForce(transform.right * (100 * bulletSizeToKnockBack), ForceMode.Force);
GameObject explosionInstance = Instantiate(explosionEffect, transform.position, transform.rotation);
Destroy(gameObject);
Destroy(explosionInstance, 1f);
}
if(collision.collider.tag == ("Ground") && isExpanding == false)
{
Destroy(gameObject);
}
}
Your answer
Follow this Question
Related Questions
How to move object without physic? 1 Answer
Keep Horizontal Momentum after Jump 2 Answers
Why are these object passing through each other? 1 Answer
Non slippery movement 1 Answer