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
0
Question by McMutton · Jun 25, 2011 at 10:04 PM · velocityobjectsanglebetween

Adding velocity to an object based on angle between it and another?

Afternoon, fellows.

Currently, I've gotten the player the ability to damage an enemy, and what I want to do next is to give some attacks knockback. I'm wanting the angle in which the enemy is pushed back to be the same as the angle between the player and the enemy.

So, for example, if the enemy is above the player and knockback is applied, the enemy would be pushed upwards, since there'd be about a 180 degree angle between it and the player. Or if the enemy was straight ahead of the player and it was applied, then the enemy would be pushed at a zero-degree angle. Stuff like that.

What sort of crazy stuff could I use to achieve this?

(Also, do note that the enemy in question has a rigidbody attached, but it won't let me set it as non-kinematic for whatever reason)

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

Answer by Dreamblur · Jun 25, 2011 at 10:15 PM

Get the positions of the player and of the enemy. Subtract those two and use the resulting vector as the direction vector for the velocity. No angle calculations required.

EDIT:

Kinda tricky with the conditions that you set, but here's a workaround to not being able to use a non-kinematic rigidbody:

 var fDistOfKnockback : float = 2;
 var dirVector : Vector3 = (enemy.transform.position - transform.position).normalized * fDistOfKnockback;
 var newEnemyPos : Vector3 = enemy.transform.position + dirVector;
 Vector3.Lerp(enemy.transform.position, newEnemyPos, t);

That's obviously pseudocode only, but it shows the theory. Basically, once you get the direction vector that your enemy is supposed to be knocked back towards, get a position a certain distance away from the enemy's position that's within the path of the direction vector. Then lerp your enemy towards that new position to simulate knockback.

Prolly not the best way, but it works given the constraints we're working with.

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
avatar image
0

Answer by aldonaletto · Jun 25, 2011 at 10:25 PM

EDITED:

Oh God! I guess it's time to change my glasses: I missed that kinematic thing. If you're not applying any kind of fake gravity to your enemies, you can simulate the movement as @Dreamblur suggested (upvote him - it's a good solution):

 private var newPos:Vector3;
 private var tKnock:float = 0;
 var knockForce:float = 5;
 
 // when knocking the enemy:
   var dir = enemy.position-transform.position;
   newPos = dir.normalized*knockForce+transform.position;
   tKnock = Time.time+1.2; // enemy movement will last 1.2 seconds
 
 function Update(){
 
   if (Time.time<=tKnock){
     enemy.transform.position = Vector3.Lerp(enemy.transform.position, newPos, knockForce*Time.deltaTime);
   }
 }

This script must be attached to the player.

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 McMutton · Jun 26, 2011 at 06:20 AM 0
Share

The issue is, though, that the enemy's rigidbody won't accept the AddForce or velocity bits, due to it always being kinematic. Even if I attempt to turn it off, it just turns back on- Probably due to the fact that it's constantly moving utilizing transforms.

avatar image Dreamblur · Jun 26, 2011 at 07:17 AM 0
Share

I edited my answer based on the new info you provided.

avatar image aldonaletto · Jun 26, 2011 at 05:10 PM 0
Share

Answer edited to work with kinematic enemies.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Converting vector3 velocity to an angel 2 Answers

Move object towards an angle the distance of two points 1 Answer

Rigidbody loses speed 1 Answer

Vector between two objects 1 Answer

Get angle between 2 Vector2's 5 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