- Home /
Character Controller with rigidbody
Hello Unity3D.I have a question about character controllers.How can i make it that when my rigidbody projectile it pushes the character controller on the z axis or on the y axis?I have seen tutorials on how to do it but none of the seem to work with the character controller just the capsule collider.If anyone knows how i can push a character controller with a rigidbody.Can you please tell me how?
var mass: float = 3.0; // the lower the mass, the higher the impact
var hitForce: float = 2.5; // impact "force" when hit by rigidbody
var player: GameObject;
private var impact = Vector3.zero; // character momentum
private var character: CharacterController;
function Start(){
character = GetComponent(CharacterController);
}
function AddImpact(force: Vector3){
var dir = force.normalized;
dir.z = 0.5; // add some velocity upwards - it's cooler this way
impact += dir.normalized * force.magnitude / mass;
}
function Update(){
if (impact.magnitude > 0.2){ // if momentum > 0.2...
character.Move(impact * Time.deltaTime); // move character
}
// impact vanishes to zero over time
impact = Vector3.Lerp(impact, Vector3.zero, 5*Time.deltaTime);
}
function OnCollisionEnter (col: Collision){ // collision adds impact
AddImpact(col.relativeVelocity * hitForce);
if(col.gameObject.tag == "Dummy"&&player.animation.IsPlaying("Astral_Kick_Barrage_Start")){
col.transform.gameObject.animation.Play ("Hit_Flying");
}
}
What about adding a force when the projectile enters your character controller's collider ?
I tried that but it only moves the capsule collider
Answer by Oribow · Apr 17, 2015 at 03:12 PM
The CharacterController isnt meant to interact with rigidbodys:
The Character Controller is mainly used for third-person or first-person player control that does not make use of Rigidbody physics.
http://docs.unity3d.com/Manual/class-CharacterController.html
You need to switch your movement script from CharacterController to rigidbody in order to work. Search for Rigidbody controller.
I have tried to make my character controller movement script into capsule collider movement script because the codes i wrote for it work perfectly for the capsule collider.But when i script it.The capsule collider doesn;t move at all....Do you know why?Because i rather have that ins$$anonymous$$d of the character controller DX.
What you are doing with the capsule collider? Attach a rigidbody to your player and move him by applying forces. Look here for an example: http://wiki.unity3d.com/index.php?title=RigidbodyFPSWalker Dont forget to accept my answer ;)
The reason why i want the capsule collider ins$$anonymous$$d of the character controller is the fact that the scripts that i coded only works well with the capsule colliders.So i thought it would be easier to change my movement script to capsule collider
Answer by IKilledKenny_2 · Apr 17, 2015 at 03:37 PM
var pushPower = 2.0;
private var yRot: float;
function Update () {
var Controller : CharacterController = GetComponent(CharacterController);
var vertical : Vector3 = transform.TransformDirection(Vector3.forward);
var horizontal : Vector3 = transform.TransformDirection(Vector3.right);
var height : Vector3 = transform.TransformDirection(Vector3.up);
if(Input.GetAxis("Vertical")||Input.GetAxis("Horizontal")){
if (!animation.IsPlaying("Jump"))
if (!animation.IsPlaying("Jump"))
if(!animation.IsPlaying("Kick1"))
if(!animation.IsPlaying("Sword_5"))
if(!animation.IsPlaying("Sword_6"))
if(!animation.IsPlaying("Sword_7"))
if(!animation.IsPlaying("Sword_8"))
if(!animation.IsPlaying("Spinning_Slashes"))
if(!animation.IsPlaying("Rex_s_Combo_Finish_2"))
animation.CrossFade("Rex_s_Run");
animation["Rex_s_Run"].speed = walkSpeed/100;
Controller.Move((vertical * (walkSpeed * Input.GetAxis("Vertical"))) * Time.deltaTime);
Controller.Move((horizontal * (walkSpeed * Input.GetAxis("Horizontal"))) * Time.deltaTime);
}else{
if (!animation.IsPlaying("Jump"))
if(!animation.IsPlaying("Overhead_slash"))
if(!animation.IsPlaying("Overhead_slash2"))
if(!animation.IsPlaying("Overhead_slash4"))
if(!animation.IsPlaying("Overhead_slash3"))
if(!animation.IsPlaying("Sword_5"))
if(!animation.IsPlaying("Sword_6"))
if(!animation.IsPlaying("Sword_7"))
if(!animation.IsPlaying("Sword_8"))
if(!animation.IsPlaying("Sword1"))
if(!animation.IsPlaying("Sword2"))
if(!animation.IsPlaying("Sword3"))
if(!animation.IsPlaying("Sword4"))
if(!animation.IsPlaying("Kick4"))
if(!animation.IsPlaying("Berserk"))
if(!animation.IsPlaying("Teleport"))
if(!animation.IsPlaying("Sword_4"))
if(!animation.IsPlaying("Sword_6"))
if(!animation.IsPlaying("Sword_7"))
if(!animation.IsPlaying("Sword_8"))
if(!animation.IsPlaying("Sword_flury"))
if(!animation.IsPlaying("Spinning_Slashes"))
if(!animation.IsPlaying("BeyBlade"))
if(!animation.IsPlaying("Stab"))
if(!animation.IsPlaying("Demonic_Wave"))
if(!animation.IsPlaying("Slash_Combo"))
if(!animation.IsPlaying("Slash_Combo_2"))
if(!animation.IsPlaying("Rex_Pause"))
if(!animation.IsPlaying("Rex_Pause_2"))
if(!animation.IsPlaying("Rex_Sword_Throw_"))
if(!animation.IsPlaying("Rex_Sword_Throw_Part_2"))
if(!animation.IsPlaying("Rex_Sword_Flying"))
if(!animation.IsPlaying("Escalibur"))
if(!animation.IsPlaying("Windmill"))
if(!animation.IsPlaying("Blade_Dance"))
if(!animation.IsPlaying("Rex_Sword_Combo"))
if(!animation.IsPlaying("Rex_Demon__Attack_1"))
if(!animation.IsPlaying("Rex_Demon__Attack_2"))
if(!animation.IsPlaying("Rex_Demon__Attack_4"))
if(!animation.IsPlaying("Rex_Demon__Attack_3"))
if(!animation.IsPlaying("Rex_s_Combo_Finish_2"))
if(!animation.IsPlaying("Rex_Seal_Combo"))
if(!animation.IsPlaying("Rex_Sword_Kick"))
if(!animation.IsPlaying("Rex_Sword_Kick_2"))
if(!animation.IsPlaying("Rex_s_Launcher"))
if(!animation.IsPlaying("Rex_s_Laucher"))
if(!animation.IsPlaying("Wide_Slash_2"))
animation.CrossFade("Rex_Demon__Fighting_Stance");
animation["Rex_Demon__Fighting_Stance"].speed =0.25;
}
}
function LateUpdate(){
// Rotate the Character to match the direction he/she is going
if(Input.GetAxis("Vertical") == 0){
if(Input.GetAxis("Horizontal") > 0){
body.localEulerAngles.y = 90;//Right sideways running
}else if(Input.GetAxis("Horizontal") < 0){
body.localEulerAngles.y = 270;//Left sideways running
}
}else if(Input.GetAxis("Vertical") > 0){
if(Input.GetAxis("Horizontal") > 0){
body.localEulerAngles.y = -90;
}else if(Input.GetAxis("Horizontal") < 0){
body.localEulerAngles.y = -270;
}
}else if(Input.GetAxis("Vertical") < 0){
if(Input.GetAxis("Horizontal") == 0){
body.localEulerAngles.y = -180;
}else if(Input.GetAxis("Horizontal") > 0){
body.localEulerAngles.y = -180;
}else if(Input.GetAxis("Horizontal") < 0){
body.localEulerAngles.y = -180;
}
}
}
Your answer
Follow this Question
Related Questions
Character Controller and Rigidbody intersect on collision. Bad. 0 Answers
Physics goes crazy when rigidbody collides with moving character controller 0 Answers
How to make certain rigidbody2d's not able to push other rigidbody2d's? 2 Answers
Limiting rigidbody physics influence on another rigidbody 0 Answers
Cannot register Collision from behind. 0 Answers