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 Ege10 · Apr 03, 2020 at 12:42 PM · velocityaddforce

I can't addforce because of velocity

I am moving my character with velocity. But (there is a bomb in game) when the bomb bombs it doesn't addforce to character becasuse of velocity. Here is the movement code:

     float y = Input.GetAxis("Vertical");
     var v3 = -tra.forward * hız*y;
     v3.y = rigi.velocity.y;
     rigi.velocity = v3;
     anim.SetFloat("f", y);


and here is the bomb code:

    public void OnTriggerEnter(Collider other)
 {
     if(other.tag=="bumbum")
     {
         Vector3 v1 = transform.position;
         Vector3 v2 = other.gameObject.transform.position;
         Vector3 v3 = v1 - v2;
         rigi.AddForce(v3 * 20, ForceMode.Impulse);
     }
 }

How can I fix 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
1
Best Answer

Answer by MichaI · Apr 03, 2020 at 03:12 PM

@Ege10 It's because AddForce works using rigidbody velocity. In your movement script you are overriding it with your movement vector.

This are solutions that come to my mind:

.1. In movement instead of assigning movement to velocity, multiply current velocity by small number to simulate drag and add movement like that:

 float drag = 0.1f;
 float y = Input.GetAxis("Vertical");
         var v3 = -transform.forward * hız * y;
         rigid.velocity = new Vector3(rigid.velocity.x * drag, rigid.velocity.y, rigid.velocity.z * drag);
         rigid.velocity += v3;

.2. When explosion happens disable movement for some time.

.3. Instead of assigning movement to velocity, add it and than in next update subtract last movement vector multiplied by drag force from velocity before adding the new one.

.4. Make explosion as Coroutine that lasts some time and adds this force to velocity after your movement script

.5. I searched through Internet and found this script that should work with external forces http://wiki.unity3d.com/index.php/RigidbodyFPSWalker


Also make sure to place your movement script in FixedUpdate ;)

Comment
Add comment · Show 1 · 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 Ege10 · Apr 03, 2020 at 04:33 PM 0
Share

Thanks for helping. I chose the first one. It worked!

avatar image
0

Answer by SmokeyWitch · Apr 03, 2020 at 01:21 PM

Hi @Ege10

Add a debug.log in if(other.tag=="bumbum") to check if the code reaches that line of code. Also, check if you didn't forget these things:

  1. add a collider to both bomb and character game objects.

  2. tick the trigger checkbox on character's collider

  3. add a dynamic rigid body to at least one of them.

  4. check if you add a tag of bumbum to the bomb game object.

  5. if the add collider2d component to game objects you have to use OnTriggerEnter2D(Collider2D collider) instead.

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 Ege10 · Apr 03, 2020 at 02:18 PM 0
Share

I allready did all those things. Before that I was using transform.Translate to move the character. Then I decided to use velocity. And addforce stoped working.

avatar image SmokeyWitch Ege10 · Apr 03, 2020 at 02:31 PM 0
Share

@Ege10 you have to use rigi.velocity = Vector.zero then add a force to it. or if you want to have both velocity and force you can use Force$$anonymous$$ode.VelocityChange mode

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

128 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

Related Questions

Moving the player in a ballistic trajectory in 3D 0 Answers

Football free kick question? 1 Answer

Constant force motion: s = s0 + v0 * t + a0 * t * t. Why? 1 Answer

[C#, 2D] How do I apply force to a player using vector 3 velocity to move 1 Answer

Roller coaster physics 0 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