Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Jermaine · Jul 02, 2011 at 02:57 AM · pushknockback

Player Knockback / PushBack

Can someone just point me in the right direction. I have my scene where the player walks through a trigger, there is a explosion.

Can someone tell me how would I cause the player to also be knocked or pushed back when they go to into the trigger?

I'm sure its probably something simple, I tried searching but only thing that comes up is about pushing a enemy back. And I can't find anything in the Docs. I want to push the player back.

Thanks in advance for any help.

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 aldonaletto · Jul 02, 2011 at 03:06 AM 0
Share

Which character are you using? The standard FPS Controller, the Character Controller with custom script or a rigidbody?

avatar image Jermaine · Jul 02, 2011 at 03:15 AM 0
Share

I'm using basic fps character controller

2 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by aldonaletto · Jul 02, 2011 at 03:39 AM

With the standard FPS controller, you may use a specific function in the CharacterMotor.js script, SetVelocity. You can put this script in the trigger object:

 var velBack:float = 15;
 
 function OnTriggerEnter(col:Collider){
 
     if (col.tag == "Player"){
         dir = (col.transform.position - transform.position).normalized;
         var charMotor = col.GetComponent(CharacterMotor);
         charMotor.SetVelocity(dir*velBack);
     }
 }

It calculates the direction from the center of the trigger to the player, than apply a velocity defined by velBack. Notice that the character must be tagged Player for this to work.

EDITED:

There's a little problem in the CharacterMotor script: it sends a message OnExternalVelocity whenever SetVelocity is called, what produces annoying warnings in the console view because there's no OnExternalVelocity function to receive it. The easiest solution is to comment out or delete the SendMessage instruction in CharacterMotor.js (line 582, I guess) - it's completely useless anyway.
If one doesn't want to mess with this script, another solution is to add an empty function OnExternalVelocity to any other script attached to the First Person Controller:

 function OnExternalVelocity(){
 }

This way SendMessage will have a receiver and stop bothering us with stupid warnings.

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 MetaMythril · Jan 03, 2013 at 12:49 AM 0
Share

Seriously awesome script. This worked great for what I needed. Thanks! :)

avatar image aldonaletto · Jan 04, 2013 at 07:18 PM 0
Share

You're probably receiving weird error messages in the console view when the character is pushed back - it's due to an useless feature of Character$$anonymous$$otor. Take a look at my answer: I edited it and included a couple hints on how to solve this.

avatar image MetaMythril · Jan 05, 2013 at 01:17 AM 0
Share

haha, I did see that error, my way of fixing it was to just edit the line like so:

Send$$anonymous$$essage("OnExternalVelocity", Send$$anonymous$$essageOptions.DontRequireReceiver);

Thanks for the update! :)

avatar image shopguy · Jan 27, 2013 at 04:07 AM 0
Share
  • exactly what I needed! Thanks! (can't upvote yet, not enough rep I guess)

avatar image
3

Answer by testure · Jul 02, 2011 at 03:09 AM

If your player is a rigid body, all you have to do is apply a force to it in an opposite direction.

If your player is not a rigid body, it's slightly more complicated if you want it to look at all realistic but the idea is essentially the same. Here are the steps you would need to take:

  • get the direction of the explosion's force. I would do this by taking the player's position vector and subtracting it from the explosion's position vector. You will probably want to zero the Y values out of both of these vectors before you subtract them, since you're likely going to want a planar vector (XZ) and handle the 'height' of the knockback yourself.

  • that should give you a vector that points in the direction you're wanting your player to travel. From here you need a 'movement speed' variable. If you've ever implemented Jump functionality with a character controller, the concept is essentially the same.

  • Multiply your direction vector by your movement speed, and apply gravity each frame.

  • You may also want to apply an 'air friction' so that your player does not maintain a constant speed when he flies through the air. This is entirely optional though, obviously.

  • Once the character controller is grounded again, the 'knock back' is complete and you can probably just zero out the knock back vector, unless you want the character to slide a bit.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Making enemies push player back. 1 Answer

2D dash/knockback? 1 Answer

Player "pinball" effect 0 Answers

Detecting when Player stoped to push a rigidbody? 3 Answers

Applying a force to multiple objects at once... 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