- Home /
How can I do the character parallel to geometry level
I have a character (Sonic) so I wanted him to walk while being to be parallel to the geometry level :
but now it's :
I use third person controller with rigidbody and capsule collider. I have tried to use Locomotion System : https://www.assetstore.unity3d.com/en/#!/content/7135; but it dont work on Unity 5. I know an asset who can do that but it's paid(PGDK) : https://www.assetstore.unity3d.com/en/#!/content/28476
I hope you can help me. Thanks
I would suggest a raycast downwards. You can get the normal to the collider the raycast hit. Then, rotate your character so that its up vector is parallel to the normal
Can you give me a script please in JS or CSharp for this because I use Playmaker visual scripting
I have a question : what do the raycast physics ? I read the documentation on that but I don't understand
It wall draw a line ( like a laser beam). That light will send you the information that 'm I hit with something or not.
Answer by Cherno · Sep 11, 2015 at 09:42 AM
public LayerMask terrainLayer;
public bool grounded = false;
private Quaternion rotCur;
private Vector3 posCur;
void Update() {
Ray ray = new Ray(transform.position, -transform.up);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 1f, terrainLayer) == true) {
rotCur = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
posCur = new Vector3(transform.position.x, hit.point.y, transform.position.z);
grounded = true;
}
else {
grounded = false;
}
if(grounded == true) {
transform.position = Vector3.Lerp(transform.position, posCur, Time.deltaTime * 5);
transform.rotation = Quaternion.Lerp(transform.rotation, rotCur, Time.deltaTime * 5);
}
else {
transform.position = Vector3.Lerp(transform.position, transform.position - Vector3.up * 1f, Time.deltaTime * 5);
if(transform.rotation.eulerAngles.x > 15) {
turnVector.x -= Time.deltaTime * 1000;
}
else if(transform.rotation.eulerAngles.x < 15) {
turnVector.x += Time.deltaTime * 1000;
}
rotCur.eulerAngles = Vector3.zero;
transform.rotation = Quaternion.Lerp(transform.rotation, rotCur, Time.deltaTime);
}
}