The question is answered, right answer was accepted
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...)
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).
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?
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
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!!!
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;
Follow this Question
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