Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 PlasticFrames · Jul 07, 2021 at 11:35 AM · collisionrigidbodyinventoryaddexplosionforcecontact.point

Apply explosive force to player at point of contact

Afternoon,

I'm trying to make a player get thrown back when they collide with a flat energy barrier. They're colliding successfully and the enemies react appropriately but the player doesn't seem to be affected by the explosive force.

Any help is appreciated!

 public Rigidbody playerBody;
 public Rigidbody enemyBody;

 public float nudgeForce = 100f;
 public float knockMultiplier = 2f;    
 public float reactionRadius = 2f;

 public Vector3 forceOrigin;

 void Start() 
 {
     playerMove = GameObject.FindWithTag("Player").GetComponent<PlayerMove>();
     healthScript = GameObject.FindWithTag("Player").GetComponent<PlayerHealth>();
     playerBody = GameObject.FindWithTag("Player").GetComponent<Rigidbody>();   
 }

 void OnCollisionEnter(Collision other) 
 {
     forceOrigin = other.contacts[0].point;

     if (other.gameObject.CompareTag("Player"))
     {
         playerBody.AddExplosionForce(nudgeForce * knockMultiplier, forceOrigin, reactionRadius, 0, ForceMode.Impulse);
         //StartCoroutine(healthScript.MakeInvulnerable());
     }
     else if (other.gameObject.CompareTag("Enemy"))
     {
         enemyBody = other.gameObject.GetComponent<Rigidbody>();
         enemyBody.AddExplosionForce(nudgeForce * knockMultiplier, forceOrigin, reactionRadius, 0, ForceMode.Impulse);
     }    
 }

}

Comment
Add comment · Show 2
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
avatar image DenisIsDenis · Jul 07, 2021 at 11:39 AM 0
Share

If a character's movement is based on a Rigidbody, then it all depends on how the player moves. If you change velocity directly, then any AddForce() will be ignored. It is advisable to see how you move the character.

avatar image PlasticFrames DenisIsDenis · Jul 07, 2021 at 12:16 PM 0
Share

This is the movement script and I've actually got the collision already working relatively to the enemies themselves so maybe it's something about the contact point?

public class PlayerMove : MonoBehaviour { public Rigidbody playerBody; public Transform runCam;

 public float speed = 6f;
 public float turnSmoothTime = 0.1f;
 public float turnSmoothVelocity;
 [SerializeField] float verticalBoost;

 public Vector3 moveDirection;
 public Vector3 velocity;


 void Start()
 {
     playerBody = GetComponent<Rigidbody>();
     runCam = Camera.main.transform;

     //Cursor.lockState = CursorLockMode.Locked; //Seems to help camera control
     GetComponent<Rigidbody>().velocity = velocity * Time.deltaTime;
 }

 void FixedUpdate()
 {
     float h = Input.GetAxisRaw("Horizontal");
     float v = Input.GetAxisRaw("Vertical");

     Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical") * verticalBoost);
     Vector3 dir = new Vector3(h, 0f, v).normalized;

     if(dir.magnitude >= 0.1f)
     {
         float targetAngle = Mathf.Atan2(dir.x, dir.z) * Mathf.Rad2Deg + runCam.eulerAngles.y;
         float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
         transform.rotation = Quaternion.Euler(0f, angle, 0f);
         moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
         playerBody.MovePosition(transform.position + input * Time.deltaTime * speed);
     }
 }

}

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Borjka · Jul 07, 2021 at 11:54 AM

Hi, - Check if player has right tag - Use Debug.log to see if it goes in the branch where you add forces - Try to increase forces, maybe they are too small

Comment
Add comment · Show 1 · Share
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
avatar image PlasticFrames · Jul 07, 2021 at 12:09 PM 0
Share

Used otherCollider.tag and otherCollider.attachedRigidbody to check and it's definitely detecting the correct object. Changing force didn't make a difference either. Bizarrely it affects the enemies as intended.

avatar image
0

Answer by Harry_Drew · Jul 07, 2021 at 11:57 AM

You're adding a rigidbody to the enemys before you add the explosion force which seems to work. Your not doing that with the player though. Maybe try adding the rigidbody to the player of if the player already has a rigidbody chekc its not in kinematic mode.

Comment
Add comment · Show 2 · Share
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
avatar image DenisIsDenis · Jul 07, 2021 at 12:03 PM 0
Share

PlasticFrames is not an Add, but a Get component of the enemy's Rigidbody (that is, it already exists):

 enemyBody = other.gameObject.GetComponent<Rigidbody>();

And the player also already has a Rigidbody:

 playerBody = GameObject.FindWithTag("Player").GetComponent<Rigidbody>();
avatar image PlasticFrames · Jul 07, 2021 at 12:09 PM 0
Share

Tried this but still no change unfortunately and definitely not kinematic.

avatar image
0

Answer by DenisIsDenis · Jul 07, 2021 at 01:20 PM

I've tested your code. Indeed, the player pushes off intermittently, not always. The following code works better in my opinion. In it, I used not the first point of contact, but the arithmetic mean of their coordinate, so that the point of impact on the wall was more accurate. Also I use AddForce() instead of AddExplosionForce(), as the behavior of AddForce is more predictable for me.

 void OnCollisionEnter (Collision other)
 {
     forceOrigin = Vector3.zero;
     for (int i = 0; i < other.contacts.Length; i++)
     {
         forceOrigin += other.contacts [0] .point;
     }
     forceOrigin /= other.contacts.Length;

     if (other.gameObject.CompareTag ("Player"))
     {
         playerBody.AddForce ((playerBody.position - forceOrigin).normalized * nudgeForce * knockMultiplier, ForceMode.VelocityChange);
     }
     else if (…)
     {
     …
     }
 }

EDITED: @PlasticFrames, I decided to approach the question from the player's perspective. After all, through its OnCollisionEnter (), we can get the normal of the surface we are in contact with. Accordingly, we will know where to push the player away.


Here is a simple player script that can walk right-left-forward-backward and will push off objects with the TagOfEnergyBarrier tag (you can choose any tag and assign it to your Energy Barrier):

 using UnityEngine;
 
 public class MovePlayer : MonoBehaviour{
     Rigidbody rb;
     void Start(){
         rb = GetComponent<Rigidbody>();
     }
 
     void FixedUpdate(){
         var move = transform.position + new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")) * 0.1f;
         rb.MovePosition(move);
     }

     // that's what you need
     private void OnCollisionEnter(Collision collision){
         if (collision.gameObject.CompareTag("TagOfEnergyBarrier"))
         {
             rb.AddForce(collision.GetContact(0).normal * 10, ForceMode.Impulse);
         }
     }
 }
Comment
Add comment · Show 1 · Share
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
avatar image PlasticFrames · Jul 07, 2021 at 01:40 PM 0
Share

I tried this but it didn't seem to work for me and I've honestly no idea why! The reason for explosion force was that I couldn't find a way to direct the apply force before.

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

221 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

Related Questions

What does rigidbodies sleeping have to do with collision detection? 0 Answers

AddExplosionForce only pushing objects in +Y 5 Answers

RigidBody and collision.contacts 0 Answers

ExplosionForce On Instantiated Object 1 Answer

Fixed Joint - Collision Problem 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