- Home /
snap the character to hit.normal.x of the ground
Rigidbody2D myrig;
public float movespeed=5f;
void Start () {
myrig = GetComponent<Rigidbody2D> ();
}
void Update () {
//geting input
float move = Input.GetAxis ("Horizontal");
//can move in da horizontal axis
myrig.velocity= new Vector2 (move*movespeed, myrig.velocity.y);
RaycastHit2D slopehit = Physics2D.Raycast (transform.position, -Vector3.up, 1);
//gizmo stuff
Vector3 slopdebug = transform.TransformDirection (-Vector3.up) * 1;
Debug.DrawRay (transform.position, slopdebug,Color.blue);
//if hit something , then rotate to the hit.normal angle
if (slopehit !=null){
transform.rotation = Quaternion.FromToRotation (Vector3.up, slopehit.normal);
}
}
Hi guy , i have simple character , it's can moving horizontal axis with "A" and "D",
and i have raycast to the bottom of the character.
when raycast hit something then , rotate the character to the normal.x of the hit object , which is great.
but the problem is the character does not snap to the surface when it's moving left and right of the surface.
this the video of my problem. https://www.youtube.com/watch?v=FLK3mmsPBvc&feature=youtu.be
Your answer
Follow this Question
Related Questions
2D Movement Mechanics for Platformer Game 0 Answers
I have a problem with Layermask Unity2D 0 Answers
How solve the 2d collider penetration problem ? 2 Answers
[2Dplatformer][Problem] When the player closes to the enemy, the enemy pass through the colliders 0 Answers
How do I move a character WITHOUT acceleration/deceleration? 1 Answer