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
0
Question by SrBilyon · Nov 02, 2010 at 01:24 AM · physicsenemyknockback

Hitting an Enemy and Pushing them back

I want to knock back an enemy when they are hit by the player. The enemy has a custom script that handles gravity (It has a character controller).

Here is the script:

//addGravity private var walkSpeed : float = 1.0; var gravity =20.0;

// The current move direction in x-z var moveDirection : Vector3 = Vector3.zero; // The current vertical speed var verticalSpeed = 0.0; // The current x-z move speed var moveSpeed = 0.0;

private var charController : CharacterController;

function Start() { charController = GetComponent(CharacterController); animation.wrapMode = WrapMode.Loop; }

function ApplyGravity() { verticalSpeed -= gravity * Time.deltaTime; }

function Update () { ApplyGravity(); var movement = moveDirection moveSpeed + Vector3 (0, verticalSpeed, 0) ;//+ inAirVelocity; charController.Move(movement (Time.deltaTime));

if (charController.isGrounded) {verticalSpeed = 0;} }

Now, I have a script that handles the damage calculation when they are hit. I want to handle the knockback here. Here is the function from that script:

function ApplyDamage (damage : int) { var myself : addGravity = GetComponent(addGravity); charController = GetComponent(CharacterController);

 flinch= true;
 eA.SendMessage ("Flinch"); //Sending this to the Flinch function in the AIFollow.cs Script
 animation.Play("gotbit");

 // We've been hit, so play the 'struck' sound. This should be a metallic 'clang'.
 if (audio && struckSound)
     audio.PlayOneShot(struckSound);

 if (hitPoints <= 0)
     return;

 //Knockback Physics 
 //======================================================================//
 var pos = transform.TransformPoint(punchPosition);

     var slamDirection = transform.InverseTransformDirection(target.position - transform.position);
     charController.Move(slamDirection * strength);

 myself.verticalSpeed = 5;
 hitPoints -= damage;
 //======================================================================//

 if (!dead && hitPoints <= 0)
 {
     Die();
     dead = true;
 }

 //This will set the flinch to false
 yield WaitForSeconds (flinchTime);
 flinch = false;
 eA.SendMessage ("unFlinch"); //Sending this to the unflinch function in the AIFollow.cs Script

}

This line is suppose to move the enemy in the opposite direction (even though right now, its more like warping and I'm not sure how to fix that, maybe time.delta?) but it only works for one direction.

charController.Move(slamDirection * strength);

Can anyone help me with this?

Comment
Add comment
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

2 Replies

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

Answer by Atnas1010 · Nov 02, 2010 at 03:13 AM

I would place this function on the enemy, and call SlamBack when he has to get slammed.

function SlamBack(slamDistance : Vector3)
{
    var slamTime = Time.time;
    while (slamTime+smoothTime > Time.time)
    {
        charController.Move(-slamDistance*(slamTime+smoothTime-Time.time)/smoothTime*Time.deltaTime);
        yield;
    }
}

To get him slammed longer, you make slamDistance greater, and to make him come to a rest slower, you edit smoothTime.

Is this the kind of interpolation you are looking for?

Comment
Add comment · Show 7 · 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 SrBilyon · Nov 02, 2010 at 03:41 AM 0
Share

I'm such a novice, do I called this inside the ApplyDamage function? and if so, what what do i put in the param?

avatar image SrBilyon · Nov 02, 2010 at 03:42 AM 0
Share

like SlamBack(param);?

avatar image SrBilyon · Nov 02, 2010 at 03:47 AM 0
Share

Oh i think i got it! SlamBack(transform.TransformDirection(slamDirection)); it that the best way for me to do it?

avatar image Atnas1010 · Nov 02, 2010 at 03:50 AM 0
Share

I'm not sure what that would do.. :) I would use SlamBack(slamDirection * strength)

avatar image SrBilyon · Nov 02, 2010 at 04:03 AM 0
Share

SlamBack(slamDirection * -strength); worked for me, the other way around did the opposite.

Show more comments
avatar image
2

Answer by IJM · Nov 02, 2010 at 02:34 AM

I am sure of one thing:

slamDirection = PlayersGameObject.transform.forward;

I hope this has helped you =)

EDIT:

You can try this for the animation:

enemy.transform.LookAt(Player.transform.position);//call this once the enemy is hit
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 SrBilyon · Nov 02, 2010 at 03:05 AM 0
Share

Thanks, this works as far as sending the enemy in the right directions, but how would I interpolate the motion? Right now the enemy just kind of warps ahead.

avatar image IJM · Nov 02, 2010 at 03:39 AM 1
Share

I meade a small edit on my Answer, I think that this will make your animation look right.

avatar image SrBilyon · Nov 02, 2010 at 04:04 AM 1
Share

Thanks for the initial answers. I wish I could check yours up also!

avatar image IJM · Nov 02, 2010 at 04:55 AM 0
Share

Npa mate ;) I'm glad that I helped. Now when I see the answer, I realize that I didn't understand the question correctly =)

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

No one has followed this question yet.

Related Questions

Character Controllers Knockback 1 Answer

Force on Character Controller (Knockback) 4 Answers

Need help with knockback in C# Please! 2 Answers

How to make arch knockback 3 Answers

How To Make NPC Follow Player When In Range? 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