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 /
This question was closed Mar 03, 2018 at 09:47 AM by jacktjong17 for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by jacktjong17 · Feb 26, 2018 at 07:53 PM · rigidbody2dnot workingnoob

How to record/save a velocity vector and apply it later?

Ok, so I've been trying to create this mechanic where:

When the character braces itself, they will teleport themself a set distance and come out of the ground with the same speed they went in (the y velocity gets flipped then of course, so they jump up from the ground instead of just launching into the ground)

And I'm having trouble with adding the velocity back up after the teleport. Here's the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LandingScript : MonoBehaviour {
 
     private Rigidbody2D rb2d;
     private Vector2 velocityLS;
     
     public bool triggerZone = false;
     public bool bracing = false;
     
     public float xAddPosition = 2;
     public float yAddPosition = 1;
 
     // Use this for initialization
     void Start () 
     {
         rb2d = GetComponent<Rigidbody2D>();
     }
     
     // Update is called once per frame
     void Update ()
     {
         if (triggerZone == true)
         {
             if (Input.GetAxis("Vertical") < 0)
             {
                 bracing = true;
             }
             else if (Input.GetButton("Vertical"))
             {
                 bracing = false;
             }
             
             if (bracing == true && triggerZone == true)
             {    
                 float xSpeed = rb2d.velocity.x;
                 float ySpeed = rb2d.velocity.y;
                 
                 transform.position = new Vector2(transform.position.x + xAddPosition, transform.position.y + yAddPosition);
                 
                 rb2d.AddForce(new Vector2(xSpeed, -ySpeed));
                 
                 bracing = false;
             }
         }
         
         if (Input.GetButton("Vertical"))
         {
             triggerZone = false;
         }
     
     }
     
     void OnTriggerEnter2D (Collider2D coll)
     {
         if (coll.gameObject.tag == "Trigger")
         {
             triggerZone = true;
         }
     }
 }

Tips are welcome on basically anything really (I'm just starting out with programming...)

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

3 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by Lilius · Feb 26, 2018 at 08:03 PM

Change this rb2d.AddForce(new Vector2(xSpeed, -ySpeed)); to rb2d.velocity = new Vector2(xSpeed, -ySpeed);

Not tested, but I use it like this in 3D version (with vector3 instead of Vector2).

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 kide0_h0jima · Feb 27, 2018 at 02:44 AM

bro, been trying to figure this out myself. the update loops keeps tampering with my equation. my player has to continue accelerating at a reduced speed after decelerating. I have tried coroutines, invoke, and "local variables"(at least how I unerstand them). Have you figured it out yet?

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 jacktjong17 · Feb 27, 2018 at 11:48 AM 1
Share

For me Lilius' way worked. But from what I can understand you're having trouble because the Update loop is resetting your variables?

 if (//conditional)
             {    
                 float xSpeed = rb2d.velocity.x; //saving variables
                 float ySpeed = rb2d.velocity.y;
                 
                 transform.position = new Vector2(x, y); //Do what you want without the variables here
                 
                 rb2d.velocity = new Vector2(xSpeed, -ySpeed / 2); //applying variables
             }

I guess this may work? I'm not sure what you're trying to do exactly. p.s. what this does is it only updates the variables when the condition is fulfilled. You could also set the variables earlier in the same void/int. But I could probably help better with the code xP

avatar image kide0_h0jima jacktjong17 · Feb 27, 2018 at 11:03 PM 0
Share

thank you jack! I was trying so hard to figure out how your code would work. I kept on looking at, if (//conditional) for a few $$anonymous$$utes, then it hit me. The solution was rather simple lol, all I really needed to do was save my var in the during the input for changing directions! STUPID $$anonymous$$E xD you still did help me, with that "push". thanks!!!

avatar image
0

Answer by ventedpennies · Feb 27, 2018 at 04:19 AM

why addforce? why not just set the velocity?

ySpeed = rb2d.velocity.y;

just flip it

rb2d.velocity.y = -ySpeed;

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

Follow this Question

Answers Answers and Comments

135 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

Related Questions

OnCollisionEnter 2d not working 0 Answers

Rigidbody2D and Raycasting. Good practice? 0 Answers

How to kill player when colliding with enemy? 0 Answers

[noob] I have a script that theoretically should manage movement of a 2D GameObject... 1 Answer

Collider2D OnTriggerEnter2D not working, IsTrigger=true and Collider2D IsTrigger=false 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