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 /
avatar image
1
Question by SpeedTutor · Jul 28, 2013 at 08:00 PM · javascriptcolliderknockback

Object push character controller

alt text

I've tried to research this myself, but getting quite confused with all the code that's floating around. I want that box which is animated to move "side-to-side" to collide with the 3rd person controller to knock it off. I hoped just applying a rigidbody to the object would work, but I guess not.

Thanks!

img.jpg (164.1 kB)
Comment
Add comment · Show 1
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 tomnoble92 · Aug 01, 2013 at 10:44 AM 0
Share

im struggling with this as well help would be appreciated

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by aldonaletto · Jul 28, 2013 at 09:00 PM

If you just want to detect when a rigidbody collides with the CharacterController, use OnCollisionEnter in the character script:

 function OnCollisionEnter(col: Collision){
   // a rigidbody hit the character - do whatever you want here
 }

But if you want the rigidbody to push the CharacterController, you'll need to add specific code to make it move when hit - like the script ImpactReceiver.js (attach to the character):

 var mass: float = 3.0; // the lower the mass, the higher the impact
 var hitForce: float = 2.5; // impact "force" when hit by rigidbody 
 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.y = 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);
 }
 
 @script RequireComponent(CharacterController)

Comment
Add comment · Show 4 · 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 SpeedTutor · Jul 28, 2013 at 11:54 PM 0
Share

Looks good, but not sure if I'm doing something wrong somewhere.

I added the script to the 3rd person controller, added a rigidbody to the moving object but nothing seems to happen. I've tried setting the rigidbody to "is kinematic" but didn't change the outcome. I'm not supposed to reference the object with a rigidbody, am I?

avatar image aldonaletto · Jul 29, 2013 at 02:09 AM 0
Share

This code applies an impact proportional to the velocity of the rigidbody relative to the character - if it's too low, nothing happens, since the resulting impact is lower than 0.2 (line 16 of this code). You may try to increase the field hitForce so that the impact is big enough to be noticed.

avatar image SpeedTutor · Jul 29, 2013 at 11:10 AM 0
Share

I tried adjust the 'hitforce', but no change. All the way upto 1000... $$anonymous$$aybe I'm making a simple error here. Cheers.

avatar image dadude123 · Dec 25, 2014 at 01:52 AM 0
Share

i know the answer is extremely late, but you have to use void OnControllerColliderHit (ControllerColliderHit hit) not OnCollisionEnter, which is only for normal rigidbodies

avatar image
-1

Answer by pugwash · Apr 15, 2015 at 12:12 PM

I had a go with this script and it did not work until I changed the last function as follows. Replace function OnCollisionEnter(col: Collision){ // collision adds impact AddImpact(col.relativeVelocity hitForce); } with the following and make the hitForce about 60. //Tag all the pushing objects as pusher otherwise the character will detect the floor function OnControllerColliderHit(hit : ControllerColliderHit) { if (hit.gameObject.tag=="pusher"){ AddImpact(hit.normal hitForce); } } enjoy

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

How to block dragging if collider hits 1 Answer

Problems with OnTriggerEnter 1 Answer

Game laggs when player comes into contact with enemy. 1 Answer

Boolean wont acitvate if the enemy enters a trigger 1 Answer

Collision, change skybox in game 1 Answer


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