- Home /
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);
}
}
}
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);
}
}
}
}
maybe using this.GetComponent().AddForce(dir * force); works
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>()
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).
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
Follow this Question
Related Questions
How to make rigidbodies on each side of a cube fall towards the cube? [multiple gravity / addForce] 0 Answers
Rigidbody.velocity seems to be breaking jumping physics 0 Answers
Freeze Position + collision? 0 Answers
When Movement speed Change once it never change again 1 Answer
Physics in 2D mode not working? 0 Answers