- Home /
Acceleration Tilt
So I am trying to recreate the character controller in overgrowth based on the GDC talk one of the devs gave: http://www.gdcvault.com/play/1020583/Animation-Bootcamp-An-Indie-Approach
I am trying to implement the acceleration tilt part. I have working calculations of the proper tilt vector, which is a vector from the object's base which tilts (from Vector3.up) in the direction of the acceleration. However, I don't know how to actually have the gameobject tilt towards the vector direction while maintaining a set forward direction.
the code is really unclean, sorry
Vector3 velocity = rb.velocity;
Vector3 a = (rb.velocity - lastVelocity) / Time.fixedDeltaTime;
lastVelocity = rb.velocity;
Vector3 direction = new Vector3 (a.x, 0, a.z);
Vector3 axis = Quaternion.AngleAxis(90, Vector3.up) * direction;
targetRot = Vector3.up;
if (Mathf.Abs(velocity.x) > 0.2f || Mathf.Abs(velocity.z) > 0.2f)
{
Vector3 vel = new Vector3(velocity.x, 0, velocity.z);
targetForward = vel;
}
if (Mathf.Abs(direction.x) > 0.2f || Mathf.Abs(direction.z) > 0.2f)
{
targetRot = Quaternion.AngleAxis(maxTiltAngle, axis) * targetRot;
}
com.transform.rotation = Quaternion.Slerp(com.transform.rotation, Quaternion.LookRotation(targetForward, targetRot), Time.fixedDeltaTime*5); //It's this line that is the problem
I know now that LookRotation isn't gonna work, because it only tilts around the forward vector, but I could not find another stable way to do it. Is there any way of setting both Transform.forward and Transform.up while having one not overwrite the other?
Answer by MrNegaBlox · Jun 17, 2017 at 01:21 PM
Okay, so I just looked into quaternions, and I just found a solution, so I'm putting it here for those who wants to learn to do this as well. Basically, with quaternions, the xyz components define an axis, and the w component is the amount of rotations around that axis, and multiplying quaternions combine rotations as if it were one after the other.
So I did this:
Quaternion.LookRotation(targetForward)*new Quaternion(axis.x, axis.y, axis.z, maxTiltAngle*10f)
So basically this does 2 things, first it makes the gameobject point in the forward direction, then I used another quaternion to have it rotate around an axis that is 90 degrees from the acceleration direction. the *10f is a wierd thing where the BIGGER the w component is, the smaller the rotation.
Then combine this with a slerp, and youve got it.
NOTE: however, it is still really finicky, pressing buttons rapidly make it snap back and forth. otherwise, it was the intended result.
Answer by toddisarockstar · Jun 17, 2017 at 03:32 AM
the video link isnt loading. if you are just trying to make a character tild forward. I think you are making this much more difficult than it needs to be.
you don't need transform.forward. All you should need to do is just change the x axis of the character's rotation!!!
here would be the raw idea:
float xxx;
float speed;
void Start(){
speed=10f;
xxx=0f;
}
void Update () {
if (your velocity is greater than whatever) {
if (xxx < 30f) {//max angle adjustment here
xxx += Time.deltaTime*speed;
}
} else {
if (xxx > 0f) {
xxx += -Time.deltaTime*speed;
}
}
transform.eulerAngles = new Vector3 (xxx,transform.eulerAngles.y, transform.eulerAngles.z);
}
I would reccomend making the speed variable get bigger according the the acceleration and possibly your max angle too to make things look smooth. but this is the basic untested idea.