- Home /
How to interact with RigidBodies with Character Controller? Unity 3D
In my Unity game, I have a character controller object for the player. However, I have been informed that the character controller doesn't interact with physics objects (rolling a ball, pushing a wall, etc).
I would like to know how to be able to move/interact physically with the PlayerController component.
There are multiple ways to move around using the charactercontroller component. Brackeys tutorials have some really helpful guides on how to set up your FP player controller using the CharacterController. To interact physically with objects is, in my opinion, not that much of a difference to how you interact with objects normally, however, instead of the rigidbody being on the player, you can place the rigidbody onto the object youre trying to interact with.
Answer by Rambovalle · Dec 22, 2020 at 07:33 PM
@JMasterBoi I had the same problem so I made this you can change the forcemultiplyer to your needs. I hope this is helpful.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PhysicsInteract : MonoBehaviour { public float forceMultiplyer = 1000f, velocity; public Vector3 Velocity;
private Vector3 lastframepos;
//checks collision
private void OnControllerColliderHit(ControllerColliderHit collision)
{
//checks if there is rigidbody
if (collision.rigidbody == null) { return; }
Vector3 pushDir = Velocity;
//Adds force to the object
collision.rigidbody.AddForce(pushDir * velocity * forceMultiplyer * Time.deltaTime, ForceMode.Impulse);
}
private void Update()
{
//calculates velocity
Velocity.x = transform.position.x - lastframepos.x;
Velocity.y = transform.position.x - lastframepos.y;
Velocity.y = transform.position.y - lastframepos.y;
//calculates velocity "Speed"
float vx;
float vy;
float vz;
if(Velocity.x < 0) { vx = Velocity.x * -1; } else { vx = Velocity.x; };
if (Velocity.y < 0) { vy = Velocity.y * -1; } else { vy = Velocity.y; };
if (Velocity.z < 0) { vz = Velocity.z * -1; } else { vz = Velocity.z; };
velocity = vx + vy + vz;
//Sets the lastframe pos
lastframepos = transform.position;
}
}
And you can add float $$anonymous$$ass ollision.rigidbody.AddForce(pushDir velocity $$anonymous$$ass force$$anonymous$$ultiplyer Time.de... If you want
$$anonymous$$ultiply charackters dont show in comments
I am using similar script, but often rigidbody gets pushed away with some crazy big force even though the actual force passed in AddForce method is not big at all. It must be some collision problem, but this is really crucial and i don't think that simply adding force like that can help achieve smooth consistent collision.
Your answer
Follow this Question
Related Questions
How to make rigidbody.AddForce and controller.Move work together? 0 Answers
I can't push a Rigidbody with Character Controller 1 Answer
How to get my Character Controller to not be affected by physics 0 Answers
Weird jitter on Android using Character Controller 0 Answers
Using character controller insted of normal collider 0 Answers