Hi, everyone!
Prepare for noobish questions, but here goes:
I’m trying to make something like a helicopter and I’m using a rigbody which I apply force to, to keep it floating mid-air.
What I’m trying now is to have the chopper automatically “stand up” if it’s leaning when no button is pressed.
I make it turn with “AddTorque” and then I try to stabilize it by adding a bit of torque in the opposite direction, but I have several problems:
The chopper doesn’t stand up, it just stops rotating.
If you’re leaning towards the right and quickly tap left, it’ll switch over to leaning to the left instead.
I would really appreciate some help here. I bet I’m doing this all wrong, but that’s okay since this is a learning experience.
The code is playable here:
Unity Web Player - chopper2 (WASD-controls)
I’ve pasted the code here, to make it easier to read:
http://www.pasteit4me.com/35002
And I know the problem is in the:
if (!(Input.GetButton("Left")) !(Input.GetButton("Right")))
at the end
If someone wants me to post the code here, I will. I just thought it will clutter the forum.
Thanks in advance.
Hello!
Just a quick thought
you can detect when the chopper is on the ground using the OnCollisionEnter, and checkout the Vector.right or forward or up (depending on your mesh orientation) to know if the chopper is leaning.
then, after that, what you can do is to make a quick interpolation to the base orientation of your vector (Disabling physics) and then your chopper should be ready
I hope this helps you out!
Hello!
Just a quick thought
you can detect when the chopper is on the ground using the OnCollisionEnter, and checkout the Vector.right or forward or up (depending on your mesh orientation) to know if the chopper is leaning.
then, after that, what you can do is to make a quick interpolation to the base orientation of your vector (Disabling physics) and then your chopper should be ready
I hope this helps you out!
Ah, thanks.
If I’m not missunderstanding you, I think you missunderstood me.
What I meant is that I want it to rotate back to its “standing angle” even when in air. It has nothing to do with the ground or landing.
But thank you.
Yes, I did misunderstood you
I think that your problem is in here:
rigidbody.AddTorque(0, 0, -leaning)
What I would do is to compare the orientation of your Vector3.up and apply an indrementing leaning (lets say add or substract by a factor of 0.1) to the actual orientation, until the orientation is equal to your “Start” orientation.
Hope this is what you are looking at
(lets say add or substract by a factor of 0.1) to the actual orientation, until the orientation is equal to your “Start” orientation.
Hope this is what you are looking at
That’s what I’ve wanted to do all along, but I don’t know how.
Since I’m not allowed to modify the current:
rigidbody.transform.rotation.eulerAngles.z
How can I set the rotation directly?
Thanks.
Well, you can try this:
if (Input.GetButton("Right")){
rigidbody.AddTorque(0, 0, -0.1);
}
that will add a small torque to the current torque you have, I’m not sure if that will work, but I do know and agree with you that you have to change the z angle by adding or subtracting torque, as you are playing with it.
Well, you can try this:
if (Input.GetButton("Right")){
rigidbody.AddTorque(0, 0, -0.1);
}
that will add a small torque to the current torque you have, I’m not sure if that will work, but I do know and agree with you that you have to change the z angle by adding or subtracting torque, as you are playing with it.
I’m sorry but I’m not sure about what you’re trying to achieve by doing that? My left/right input works with the current code:
if (Input.GetButton("Left"))
{
rigidbody.AddTorque(0, 0, leaning);
}
if (Input.GetButton("Right"))
{
rigidbody.AddTorque(0, 0, -leaning);
}
I could easily set “leaning” to 0.1. So with the code you wrote, nothing would change.
Or am I totally missunderstanding something now?
Could you please give me your values on the scripts I try something
Hello
I’ve just found something. and is in this line:
float currentAngularZRotation = Mathf.Abs(rigidbody.angularVelocity.z);
That value is almost 0, so that might be the var that is not letting you “organize” the value here:
rigidbody.AddTorque(0, 0, -leanRestoreSpeed * currentAngularZRotation);
as -leanRestoreSpeed * currentAngularZRotation is almost 0, is not applying any torque
I dont know if you are aware of this, but I’ll be thinking how to solve this
as -leanRestoreSpeed * currentAngularZRotation is almost 0, is not applying any torque
I dont know if you are aware of this, but I’ll be thinking how to solve this
First of all, a big big thanks for helping me out.
But that is only about 0 when the chopper is almost perfectly “stable”, right? So it shouldn’t be a problem, per se. If I’m thinking right.
Well,
I’m rotating the chopper to almost 87 degrees and the value is almost 0 :S
Please check it and let me know if I’m wrong
D’oh!
You’re absolutely right.
So I change the line into:
rigidbody.AddTorque(0, 0, leanRestoreSpeed * currentAngularZRotation * leanRestoreSpeedModifier);
If I set the leanRestoreSpeedModifier to 3 or anything less, nothing happens. If I set it to 3.1 it’s almost like an instant restore.
Why is there such a huuuuuuuge difference between 3 and 3.1?
Or am I missing something?
Edit:
The magic number seems to be 3.011. On 3.011 nothing happens. On 3.012 I get an instant movement.
Hello, I changed it as you said to 3, and it does work as same as 3.1 BUT.
I have noticed that I’m getting also this message on the console log:
Debug.Log(“HUGE ERROR!”);
Here is my code:
http://pastie.org/619820
And the screenshots of the values I have:
Yeah, it seems as if the line between 3 and 3.1 is changed when you tweak the other variables as well.
And the error-log should mean that’s when you actual aren’t leaning at all, and the:
rigidbody.transform.rotation.eulerAngles.z
is 0.
I really don’t understand why it’s acting the way it is.
I’m terribly sorry, but I’ll leave this be for now, untill tomorrow. It’s past 4.30 am here in Sweden.
A huge thank you for helping me out.