The parachute descends head down. How do you reverse it?

Asked

Viewed 450 times

10

I made a rocket in Unity that I can take off and land after five seconds. However, it lands that way:

Foguete

I want to get him to the ground with his parachute up. How can I do that by code?

Code I have so far:

double t = 5.0;

void Update () {
GameObject Paraquedas;
GameObject CorpoNariz;
CorpoNariz = GameObject.Find("Corpo_Nariz");
Paraquedas = GameObject.Find("Paraquedas");
    rigidbody.AddForce(transform.up * 15);
    t -= Time.deltaTime;
    if (t <= 0) {
    Destroy (CorpoNariz);
    Paraquedas.renderer.enabled = true;
    rigidbody.AddForce(-transform.up * 50);
    rigidbody.drag = 5;
       }
   }
}
  • What is the purpose of transform.Rotate(Time.deltaTime, 0, 0);? It seems to me that merely removing it would solve the problem.

  • @Guilhermebernal was something I did to see if it solved my problem but did nothing. as it is indifferent I will remove from the post

  • Is the parachute facing up in the position in the scene? Nothing should make the rocket turn then.

  • @Guilhermebernal This is the rocket before takeoff: http://i.imgur.com/Hicivbq.png. It lands in the same position as it takes off (without the tip, so the parachute can come out). I have to make sure he stays upside down, so the parachute stays the right way.

  • Some questions: what is the object the code is running on (i.e., which has the component rigidbody)? Why do you add a force "up" to each frame (called update)? and why you force "down" after 5 seconds (this was something for gravity to do, not?)?

    1. It’s the whole rocket. 2) Because he has to go up... 3) Because he has to go down... (I’m new to Unity, I didn’t know if there’s another way to make him fall, so I did so, but I accept other suggestions).
  • Got it. Well, ideally you should add power to the rocket only when an acceleration occurs (maybe at the press of a key?). Already make the rocket fall is something that gravity will do to you.

  • 1

    But about the parachute, so you replied to @Guilhermebernal, the parachute opens on the back of the rocket, is that it? And so you’d like to reverse the rocket?

  • @Luizvieira this

  • Okay. I’ll try to prepare an answer. :)

Show 5 more comments

1 answer

3


Here’s a hint of how you can do whatever you want (sorry, I did in C# by custom, but it’s trivial to convert to javascript):

using UnityEngine;
using System.Collections;

public class Disparar : MonoBehaviour {

    // Funçao de atualizaçao quadro a quadro
    void Update () {

        // So loga as informaçoes
        Debug.Log (rigidbody.velocity.magnitude);
        Debug.Log (noChao ());

        // Acelera se o jogador apertar a tecla ESPACO
        if(Input.GetKeyDown(KeyCode.Space))
            acelera ();

        // Quando detectar que a velocidade inverteu (ficou negativa no y e magnitude bem pequena,
        // ou seja, o foguete parou la no alto), aplica uma força para girar.
        if (!noChao () && rigidbody.velocity.y < 0 && rigidbody.velocity.magnitude <= 1)
            //gira();
            StartCoroutine(fazRetorno());
    }

    // Acelera se o jogador clicar no foguete
    void OnMouseDown() {
        acelera();
    }

    // Acelera o foguete aplicando uma força para a sua direçao "cima".
    void acelera() {
        rigidbody.AddForce(transform.up * 2000);
    }

    // Gira o foguete aplicando uma força para a sua direçao direita a partir de seu topo.
    void gira() {
        Vector3 ponta = transform.position;
        ponta.y += renderer.bounds.size.y / 2;
        rigidbody.AddForceAtPosition (transform.right * 20, ponta);
    }

    // Indica se o foguete esta colidindo com o chao
    bool noChao() {
        return Physics.Raycast(transform.position, -Vector3.up, collider.bounds.extents.y + 0.1f);
    }

    // Gira o foguete 180 graus sobre o eixo X, fazendo-o inverter completamente a direçao de subida
    IEnumerator fazRetorno() {

        Quaternion angulo = Quaternion.Euler(180, 0, 0);
        float velocidade = 0.5f;

        while(Quaternion.Angle(transform.rotation, angulo) > 0)
        {
            transform.rotation = Quaternion.Slerp(transform.rotation, angulo, Time.deltaTime * velocidade);
            yield return new WaitForEndOfFrame();
        }  
        transform.rotation = angulo;
    }

}

Basically the idea is that from a user interaction (click or keyboard), a force is added to the rocket (I tested it with a parallelepiped). At each frame, it checks whether the rocket has stopped up there (i.e., it is not on the ground and the speed on the y axis has been reversed and is small beeeemmm - meaning magnitude less than 1). If this happened, he simply applies another force to the top of the rocket to make it spin.

This example causes the rocket to rise vertically and decelerate until it stops high, so it will rotate and start to fall vertically as well. You will need to adjust the force values to get a cool effect as desired.

EDIT: I’ve done a new function called fazRetorno that doesn’t use physics and simply rotate the rocket without adding any force side. Maybe this is closer than you want. She uses Nice Unity features that are the command yield along with the function Quaternion.Slerp. Basically the execution of the loop (while) that has within the function fazRetorno is diluted over several tables (the yield does this) and the rotation to 180º on the x axis (the variable angulo indicates this) is "interpolated" according to the speed set in the variable velocidade. Although this rotation does not use physics, note that the "fall" of the rocket is all managed by the physics engine of the Unity (i.e., the only force added was at the beginning, at the time of the launching). :)

If your game wants the rocket to be launched on a ballistic trajectory (a parabola), simply combine the vectors to add the force on the y (top) and x or z (front and side) axes at the time of launch. To do this, just add the vectors:

Vector3 forca = (transform.up * 2000) + (transform.right * 300);
rigidbody.AddForce(forca);

Using the rigidbody.velocity you can know the speed of the rocket at any time, and you can decide to open the parachute. It’s another option.

I hope it helps.

P.S.: For this example your rocket must also have a collider (ideally a box collider) besides the rigidbody. :)

  • I tested your code in full and tried to adapt to my current few things. What I managed to do was to make the rocket go more to the right at the time of landing, but it goes down with its parachute on the ground, in the same way. I’m doing something wrong?

  • Well, you’d probably have to add a larger lateral force and the rocket would need to be high enough to have time to rotate. But, I made an edition where I suggest another alternative (function fazRetorno) that doesn’t use physics to spin the rocket. Maybe that’s what you want.

  • was show! But after descending the rocket continues "walking" forward on the ground. You know what could be?

  • Perhaps it did not give time to rotate completely and when hit the ground resulted in some residual force. You can capture the collision event with the ground (add the method onCollisionBegin, if I’m not mistaken) and when colliding with the ground force a complete stop making Rigidbody.velocity = new Vector3(0,0,0);. I didn’t test it, but it should work.

  • 1

    P.S.: You may also need to force the 180 degree rotation after making the forced stop. The last line of the function fazRetorno does that.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.