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 /
avatar image
0
Question by CaptainPickle77 · Feb 08, 2019 at 03:13 AM · movementphysicsrecoil

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);
     }
 }
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

202 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

Related Questions

How to move object without physic? 1 Answer

Every time my character hits or touches the walls he gets stuck or acts as if the object the character hit was a magnet. 0 Answers

Keep Horizontal Momentum after Jump 2 Answers

Why are these object passing through each other? 1 Answer

Non slippery movement 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