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
. :)
What is the purpose of
transform.Rotate(Time.deltaTime, 0, 0);
? It seems to me that merely removing it would solve the problem.– Guilherme Bernal
@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
– athosbr99
Is the parachute facing up in the position in the scene? Nothing should make the rocket turn then.
– Guilherme Bernal
@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.
– athosbr99
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?)?
– Luiz Vieira
– athosbr99
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.
– Luiz Vieira
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?
– Luiz Vieira
@Luizvieira this
– athosbr99
Okay. I’ll try to prepare an answer. :)
– Luiz Vieira