Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
2
Question by IKilledKenny_2 · Apr 16, 2015 at 06:35 PM · collisionrigidbodycharacter controllerpush

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");
 
  
  
  
      }
  
  }
  
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 KdRWaylander · Apr 17, 2015 at 06:08 AM 0
Share

What about adding a force when the projectile enters your character controller's collider ?

avatar image IKilledKenny_2 · Apr 17, 2015 at 09:20 AM 0
Share

I tried that but it only moves the capsule collider

2 Replies

· Add your reply
  • Sort: 
avatar image
3

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.

Comment
Add comment · Show 3 · 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 IKilledKenny_2 · Apr 17, 2015 at 03:40 PM 0
Share

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.

avatar image Oribow · Apr 17, 2015 at 04:05 PM 0
Share

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 ;)

avatar image IKilledKenny_2 · Apr 17, 2015 at 06:44 PM 0
Share

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

avatar image
-1

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;
     }
   }
 }
 
Comment
Add comment · 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

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

19 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

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


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