Rotating controller to match slope of ground - solved

I’m having some issues… I’m trying to rotate my animation to match the ground below. When he gets near odd pieces of terrain, such a small little ledges or sudden angle changes, he completely freaks out and will rotate in odd ways.

How can I get my object to rotate to match the surface below effectively? this is a 3d character in a 2d game.

var newSlopeComp= Vector3.Angle(transform.TransformDirection(Vector3.up) , surfaceNormal);
if (newSlopeComp > 50)
{
	newSlopeComp = slopeCompensation;
}
slopeCompensation = Mathf.Lerp(slopeCompensation, newSlopeComp , Time.time );
if (surfacePoint.x > groundPoint.x){ //we need to flip the slope if its a slope going the other way
	slopeCompensation = slopeCompensation * -1;
}

if (horizontal < 0)
{
	transform.localEulerAngles.y = 180;
	transform.RotateAround (runAnimationChest.transform.position,Vector3.forward, -slopeCompensation);	

}
if (horizontal > 0)
{
	transform.localEulerAngles.y = 0;
	transform.RotateAround (runAnimationChest.transform.position,Vector3.forward,  slopeCompensation);	
}

The above code will rotate him to the surface, and will rotate him from his chest, so its pretty smooth, but i would prefer that the game object doesnt jump to some ridiculous angle, im trying to smoothly rotate the charcter so that as he goes up a slope he slowly rotates from being straight up and down to a slight angle.

63808--2341--$picture_6_191.png

huzzah! here’s the final i got is solved

{

	slopeCompensation= Vector3.Angle(Vector3.up , floorNormal);
	Debug.DrawRay (transform.position, transform.TransformDirection(Vector3.up) , Color.green);
	Debug.DrawRay (transform.position, floorNormal, Color.blue);
	if (surfacePoint.x > groundPoint.x){ //we need to flip the slope if its a slope going the other way
		slopeCompensation = slopeCompensation * -1;
	}



	if (horizontal < 0)
	{
			runAnimationRoot.transform.localEulerAngles.y = 180;
	}
	if (horizontal > 0)
	{
			runAnimationRoot.transform.localEulerAngles.y = 0;
	}

	target = Quaternion.Euler (-slopeCompensation, transform.eulerAngles.y,transform.eulerAngles.z);
	transform.rotation = Quaternion.Lerp(transform.rotation, target,Time.deltaTime * SlopeRotationSmooth);

So someone asked me to explain this script a little better. I’ll keep adding until its clear-

This script should be attached to a body you wish to move aross the ground. The floorNormal is a vector that is normal from a vector3 that points at the floor. This this information you can rotate the main transform to the right angle. if you find things arent flipping the right way, you may have a different coordinate x/y than i do, in which case try switching the eulerAngles.z/y/x until the position is right.

=)

Thanks for the reply,
can I ask you some more details?

  1. For example, should I take the last script you posted and put it into the Update() function of an object (or do I need both scripts you posted)?

  2. Can you please explain there variables?
    floorNormal
    surfacePoint
    groundPoint
    runAnimationRoot
    SlopeRotationSmooth

In my project, I should match the slope of the ground for a cubic object.

Thanks,
Simone

Yes, if we need the definitions of those variables to be able to follow and make sense of your code