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 unity_vVZc-PQLVkDP-w · Jul 23, 2020 at 12:58 PM · unity 2dvelocityrigidbody.velocity

Adding a velocity to a rigid body inst working.

Hi guys,

Iam creating a simple game. I have problem with my enemy behaviour. When a player attack an enemy, enemy shoud have some knockback. For this Iam using simple rb.velocity = new Vector2D(x ,y). It is interesting that it works only on y cordinate. The enemy just jump (move on y cordinate), but never moves on x cordinate.

Do you know where do I make mistake?

Thanks

 private void Knockback()
     {
         
         knockbackStartTime = Time.time;
 
         rb.velocity = new Vector2(knockbackSpeed.x, knockbackSpeed.y);
         Debug.Log(knockbackSpeed);
 
         //Check time for knockback
         if (Time.time >= knockbackStartTime + knockBackDuration)
         {
             state = State.run;
         }
     }
 

Enemy rigidbody

This picture is RigidBody on enemy

capture.png (67.1 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Captain_Pineapple · Jul 23, 2020 at 02:02 PM

I guess that the knockback does not work well together with the way you move your enemy. If you also set a velocity there it cancels out the knockback.


But from why you postet it's not possible to tell that for sure. It's just an assumption since your code as is seems to be ok.

Comment
Add comment · Show 2 · 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 unity_vVZc-PQLVkDP-w · Jul 24, 2020 at 03:30 PM 0
Share

For moving I am using $$anonymous$$oveTowards function.

 private void $$anonymous$$ove()
     {
         state = State.run;
         Vector2 targetPosition = new Vector2(target.position.x, transform.position.y);
 
         transform.position = Vector2.$$anonymous$$oveTowards(transform.position, targetPosition, speed * Time.deltaTime);
        
     }


targetPosition is position of Player.

avatar image Captain_Pineapple unity_vVZc-PQLVkDP-w · Jul 25, 2020 at 03:59 PM 0
Share

well there you have the issue. If you want to use pyhsics and velocity then you have to stick to that system or design a "hybrid-system" that does hard movement of the enemies and takes their velocity into account.

So for example you could remove the rigidbody and give your Script it's own velocity vector. There you can then add knockback and movementvelocitys as you like and then move the enemy accordingly.


Your current system just overwrites the velocity you set by a hard movement countering the knockback.

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

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

Related Questions

How to pause an object temporarily 1 Answer

Player is not moving 0 Answers

I have an animated character, need to find current speed 0 Answers

Rigidbody2D.Velocity = Vector2 not changing X Position 1 Answer

,Rotate a gameobject around another while being attracted by its gravity 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