Hi there,
I have a small script for spaceship. All movements are working fine, but it’s not really smooth. I would like my movement a bit ‘bouncy’, that the spaceship is not stopping rotation instantly when releasing controller.
Any suggestions how to fix this? I’ve read about Lerp or addforce options. But I don’t understand how to use it in my setup.
Snippet of my code:
void Update()
{
// get input movement
roll = Input.GetAxis("Roll") * (Time.fixedDeltaTime * RotationSpeed);
pitch = Input.GetAxis("Pitch") * (Time.fixedDeltaTime * RotationSpeed);
yaw = Input.GetAxis("Yaw") * (Time.fixedDeltaTime * RotationSpeed);
}
void FixedUpdate()
{
Quaternion AddRot = Quaternion.identity;
AddRot.eulerAngles = new Vector3(-pitch, yaw, -roll);
rigidbody.rotation *= AddRot;
}
Thanks in advance!!
Here’s a cascade of posts containing various infos. Each approach will have a different “feel” to the controls.
Lerping to smooth things out:
Hello and welcome! Aircraft banking is something that always makes me smile.
Here’s some thoughts on the matter. I believe the best way to accomplish what you want is to reorganize your airplane a little bit to have an extra GameObject in its hierarchy that is dedicated only to banking.
If you prefab looks like this:
AirplanePrefab
VisibleAirplaneGeometry
Then revamp it to look like this:
AirplanePrefab
RollPivot
VisibleAirplaneGeometry
Now what you want to do is change the R…
Generally you want a lowpass filter of some kind. Lerp is an easy way to do that and has nothing to do with Quaternions.
Another way to do that is a time-smoothed average. You can get as complicated or as simple as you want with this.
The basic idea is you have an accumulator that is your current input signal.
When taking new input, if you just assign to that accumulator, you will be exactly where you are, no filtering.
If instead you take X fraction of the accumulator and add it to (1 - X) …
In the case of input smoothing (implementing your own input filtering):
You can use GetAxisRaw and trivially recreate the smoothing of GetAxis on a single axis basis with Mathf.Lerp, or on a dual-axis basis with Vector2.Lerp().
You just need one storage variable and the notion of how snappy you want the smoothing.
float axis; // the "accumulator"
const float Snappiness = 3.0f; // adjust to suit
and then in Update():
// raw input:
float x = Input.GetAxisRaw( "Horizontal");
// smooth it
axis = Mathf.Lerp( axis, x, Snappiness * Time.deltaTime);
// TODO: now use…
Smoothing movement between discrete values:
The problem is the choice of integer values for lanes. They can only either be lane 0, 1 or 2. They can’t be in between round numbers. It needs to be a floating point value so that it can be between lanes.
This type of question comes up so universally I have finally just made a sample package to demonstrate the basic concept. Read my first post above and see if you can correlate each part of the code to the steps indicated.
Code located here: SmoothMovement.cs · GitHub
6430100–719246–SmoothMo…
The code: Smooth movement - Pastebin.com
Try this, maybe:
public float blend = 0.9f;
float oldRoll;
void Update()
{
// get input movement
roll = Input.GetAxis("Roll") * (Time.fixedDeltaTime * RotationSpeed);
roll = blend * roll + (1 - blend) * oldRoll;
oldRoll = roll;
}
Thanks guys! Will try this. I’ll let you know…
Hi guys, your posts lead me to the right direction. Thanks again.
You’re welcome! Glad you got something out of it.