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 Mikul_ · Sep 03, 2020 at 07:19 PM · movementphysics2dmovement scriptplatformer

How can I add an impulse force on top of my walking force?

I want to have 3 forces in my 2D platformer game - basic left and right walking, a jump, and a gun which boosts the player away from the mouse.

The only way I have found to make my player move linearly with a constant velocity has been to do something like this:

 float hAxis = Input.GetAxisRaw("Horizontal");
 rb.velocity = new Vector2(hAxis * spd, rb.velocity.y);

and my gun force code is

 if (Input.GetMouseButtonDown(0))
         {
             var dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
             rb.velocity = -dir.normalized * gunForce;
         }

However, by directly setting the x velocity of the rigidbody it overrides whatever force the gun was putting on the character, which I don't want. I would want for moving left when the gun is sending you right to slowly stop the character, and if the character was moving right and the gun is also sending you right I would want the character to either continue at the speed the gun was moving the character or to slowly move to the normal walking speed (whichever is easier to code).

Any way that I have tried to change the walking script has just made the movement exponential.

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

Answer by Chrissyeah_ · Sep 03, 2020 at 08:51 PM

@Mikul_

Potentially there are ways this can be simplified, but you can use Mathf.Lerp to transition the value overtime. If you add this code and then adjust your velocity code to rb.velocity = new Vector2(hAxis * (spd + Impulse), rb.velocity.y); Then you can achieve the effect you are looking for.

 public bool ImpulseTrigger;
 float ImpulseTimer;
 public float Impulse;
 float initialImpulse = 5;
 float LerpInterpolator;
 
 
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Space))
     {
         TriggerImpulse();
     }
 }
 
 private void FixedUpdate()
 {
     if (ImpulseTrigger)
     {
         ImpulseTimer -= Time.fixedDeltaTime;
         if (ImpulseTimer < 0)
         {
             ImpulseTrigger = false;
         }
         LerpInterpolator += Time.deltaTime;
         Impulse = Mathf.Lerp(initialImpulse, 0, LerpInterpolator);
     }
 }
 
 void TriggerImpulse()
 {
     LerpInterpolator = 0;
     ImpulseTrigger = true;
     ImpulseTimer = 2;
 }
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 Mikul_ · Sep 04, 2020 at 10:08 PM 0
Share

This works great, and is an interesting mechanic by itself, but I want the gun to shoot the player in the opposite direction of the mouse, whether that be up, down, left, right, or anywhere in between. This code boosts the player forward. I tried implementing the lerp idea into my own code but I still couldn't figure it out. Thanks for your help though.

avatar image Mikul_ Mikul_ · Sep 04, 2020 at 10:41 PM 0
Share

I've figured it out, I just stopped using the rigidbody gravity and made my own. I made the impulse lerp down like you suggested at it works like a charm. Thanks I couldn't have figured it out without you

avatar image Chrissyeah_ Mikul_ · Sep 09, 2020 at 11:34 AM 0
Share

@$$anonymous$$ikul_ I'm glad you came right :D You are more then welcome to mark my answer as correct ;P

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

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

Velocity is not matching the Vector3 it is assigned 1 Answer

Does anyone know what i am doing wrong? 0 Answers

2d platformer dash move. 1 Answer

Rigidbody2D character movement problem 0 Answers

Jump only sometimes works and player doesn't always jump high? 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