• 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 montymomentum_unity · Apr 02, 2020 at 12:35 PM · rigidbodyplayeraddforcerigidbody.addforce

Force is being added to wrong RigidBody

I'm using a script that adds force to the player when in collision with an enemy, my problem however is that the force gets added to the enemy instead of the player.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Bounce : MonoBehaviour
 {
     void OnCollisionEnter(Collision c)
     {
         // force is how forcefully we will push the player away from the enemy.
         float force = 1000;
 
         // If the object we hit is the enemy
         if (c.gameObject.tag == "Thump")
         {
             print("Hit");
             // Calculate Angle Between the collision point and the player
             Vector3 dir = c.contacts[0].point - transform.position;
             // We then get the opposite (-Vector3) and normalize it
             dir = -dir.normalized;
             // And finally we add force in the direction of dir and multiply it by force. 
             // This will push back the player
             print("Step 2");
             GetComponent<Rigidbody>().AddForce(dir * force);
         }
     }
 }

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 montymomentum_unity · Apr 03, 2020 at 10:38 AM 0
Share

I have deducted my problem lies with either the Character Controller or my $$anonymous$$ovement script. using UnityEngine; using System.Collections; using UnityEngine.Scene$$anonymous$$anagement;

 public class movement : $$anonymous$$onoBehaviour
 {
 
     private CharacterController controller;
 
     public float speed = 1.0F;
     public float jumpSpeed = 1.0F;
     public float gravity = 1.0F;
     private float verticalVelocity = 0;
     void Update()
     {
         CharacterController controller = GetComponent<CharacterController>();
         if (controller.isGrounded)
         {
             Vector3 moveDirection = new Vector3(Input.GetAxis("Horizontal") * speed, verticalVelocity, Input.GetAxis("Vertical") * speed);
             moveDirection = transform.TransformDirection(moveDirection);
             controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
             if (Input.GetButton("Jump"))
             {
                 verticalVelocity = jumpSpeed;
 
             }
             else
             {
                 verticalVelocity = 0;
             }
         }
         else
         {
             verticalVelocity -= gravity * Time.deltaTime;
             
                 if (Input.GetAxis("Vertical") > 0)
             {
                 Vector3 moveDirection = new Vector3(Input.GetAxis("Horizontal") * (speed / 2), verticalVelocity, Input.GetAxis("Vertical") * speed);
                 moveDirection = transform.TransformDirection(moveDirection);
                 controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
             }
                else
             {
                 Vector3 moveDirection = new Vector3(Input.GetAxis("Horizontal") * (speed / 2), verticalVelocity, Input.GetAxis("Vertical") * (speed/4));
                 moveDirection = transform.TransformDirection(moveDirection);
                 controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
             }
         }
         
     }
 }
avatar image SmokeyWitch · Apr 03, 2020 at 02:59 PM -1
Share

maybe using this.GetComponent().AddForce(dir * force); works

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FlaSh-G · Apr 03, 2020 at 01:08 AM

To get the rigidbody of the object you collided with, use

 c.gameObject.GetComponent<Rigidbody>()
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 montymomentum_unity · Apr 03, 2020 at 10:16 AM 0
Share

If I replace GetComponent<Rigidbody>().AddForce(dir * force); with c.gameObject.GetComponent<Rigidbody>().AddForce(dir * force); the force still just gets added to the object and not the Player, no matter who I attach the script to (and changing the tags accordingly).

avatar image FlaSh-G montymomentum_unity · Apr 03, 2020 at 05:31 PM 0
Share

If your player character's velocity is not controlled by the rigidbody, pushing the rigidbody will not be a solution. Get your script component from the player GameObject and apply force to that - which is something you have to make your character controller script support in the first place.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

193 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

Related Questions

How to make rigidbodies on each side of a cube fall towards the cube? [multiple gravity / addForce] 0 Answers

Physics in 2D mode not working? 0 Answers

Addforce up to kenemetic rigidbody of the triggerobject player 2 Answers

How can I move with UI buttons without having to press many times? 2 Answers

Freeze Position + collision? 0 Answers


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