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 /
  • Help Room /
avatar image
0
Question by Creative Inventory · Nov 13, 2015 at 10:32 PM · addforcestop

stop Addforce

Okay, I have a player that bounces off a tagged object on trigger, using Addforce. the force is set to *300, causing my player to get knocked back, which continuously move . So I placed a cube with a 2D box collider attached to it to stop the player from moving too far. My question is: how can I implement the Addforce to only move a short amount of distance, OR have a timer that stops Addforce after the trigger event occurred. Can someone help me with this. Thank you! this is my script:

      using UnityEngine;

      using System.Collections;

      public class Moveplayer : MonoBehaviour {
 
         
 void OnTriggerEnter2D(Collider2D other) 
 {
     if (other.tag == "Bouncy object")
         GetComponent<Rigidbody2D>().AddForce(transform.right * 300);
     GetComponent<Rigidbody2D>().AddForce(transform.right * 300);

       }
  }

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
0
Best Answer

Answer by joemane22 · Nov 14, 2015 at 05:18 AM

You could use

 GetComponent<RigidBody2D>().AddForce(transform.right * 300, ForceMode.Impulse);


This will add a non-continuous force to the object which will stop moving as long as there is some drag set on the rigidbody. It will also be a little weaker so you may have to tweak the size of the force.

Also as a side note if you are using Rigidbody2D a lot in that script you might just want to create a field for it and assigning it inside that Start() method that way you don't have to get the component constantly which will give you a performance hit in the long run. Of course, if it is just for that one trigger event it should be all good.

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 Creative Inventory · Nov 14, 2015 at 11:43 AM 0
Share

Thank you for replaying beck , It works perfectly! I just have one problem. Why is it my player is being dragged back after collision. (kinda like it's being pulled back by a magnet). How would I stop this. All I want is for my player to get pushed back for a short amount of distance and stays there! Thank you again

avatar image joemane22 Creative Inventory · Nov 14, 2015 at 11:53 AM 0
Share

Add linear drag, angular drag is for rotation. You will have to tweak the numbers to get to what works for your game. To make it easier add a public variable to your class called $$anonymous$$nockBackForce and replace it like this

 GetComponent<RigidBody2D>().AddForce(transform.right * $$anonymous$$nockBackForce, Force$$anonymous$$ode.Impulse);

so you can tweak it from the inspector.

If the drag is high enough and you add the right amount of force along with it, it will only be pushed back a bit. Now there is a different way you can go about it by hard coding the knock back but I think if you play around with the numbers a bit it should work just fine.

Also, I explained Force$$anonymous$$ode.Impulse wrong, it's more of like getting hit rather than a smooth force, thought it might suit your needs a little better.

avatar image Creative Inventory joemane22 · Nov 14, 2015 at 11:58 AM 0
Share

sorry I just fixed it then re-edit my comment, I'll just post it again.

Thank you for replaying beck , It works perfectly! I just have one problem. Why is it my player is being dragged back after collision. (kinda like it's being pulled back by a magnet). How would I stop this. All I want is for my player to get pushed back for a short amount of distance and stays there! Thank you again

avatar image joemane22 Creative Inventory · Nov 14, 2015 at 12:14 PM 0
Share

Alright guess hard coding it is the way to go I made a simple script that will do what you need

 public class $$anonymous$$nockBack: $$anonymous$$onoBehaviour 
     {
 
         //The total distance the player will be knocked back
         public float $$anonymous$$nockBackDistance = 10;
 
         //the speed at which the player will be moving
         public float $$anonymous$$nockBackSpeed = 1;
 
         //control variable to flag this script to begin running
         private bool running = false;
 
         //keeps track of the time elapsed since it started
         private float elapsedTime = 0;
 
         void Start()
         {
             //nothing needed in here
         }
         void Update()
         {
             //if the running boolean was flag begin the knock back
             if(running)
             {
                 //add to the transforms position        //this bit calculates the velocity it has to go to reach the distance in the right time 
                 transform.position += transform.right * ($$anonymous$$nockBackDistance / $$anonymous$$nockBackSpeed) * Time.deltaTime;
 
                 //add the change in time to the elapsed time
                 elapsedTime += Time.deltaTime;
                 if(elapsedTime >= $$anonymous$$nockBackSpeed)
                 {
                     //flag the script to stop running and reset the elapsed time to be ready for the next time
                     running = false;
                     elapsedTime = 0;
                 }
             }
         }
         void OnTriggerEnter2D(Collider2D obj)
         {
             //if the object collided with is the desired one flag for the script to start
             if(obj.tag == "Bouncey object")
             {
                 running = true;
             }
         }
 
     }

please do read through the code comments till you fully understand what's happening. It has a few much-needed concepts in the code you will need to know. If you have any questions on why something works or if there is a bug(altogether possible but I don't think so) feel free to ask.

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

34 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

Related Questions

Force and Velocity problem 0 Answers

Spawned objects won't stop moving after instantiating. 2D Top-Down 1 Answer

Fall down Cube object from platform. 0 Answers

How to move an Instantiated Object towards a moving Object? 0 Answers

Enemy AI Script Error 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