Rotating in Z-Axis while still moving using rigidbody2D.velocity,Rotating in z axis while still moving using rigidbody2d
I am working on a 2d game movement, in which i want my character to move according to the slope angle, like this: 
but while press move ....it end up like this 
every time when i press move, the the rotation in z axis resets to 0....... iam stuck in this problem for a day now. pls help me figure it out as iam starting to learn unity
my code:
 void Start()
 {
     _rb = GetComponent<Rigidbody2D>();
     if(_rb == null)
     {
         Debug.LogError("The RigidBody2D is NULL");
     }
     _Anim = GetComponent<Animator>();
     if(_Anim == null)
     {
         Debug.LogError("The Animator is NULL");
     }
 }
 void Update()
 {
     RaycastHit2D[] hits = new RaycastHit2D[2];
     int h = Physics2D.RaycastNonAlloc(transform.position, -Vector2.up, hits);
     if (h > 1)
     {
         //Debug.Log(hits[1].normal);
         angle = Mathf.Abs(Mathf.Atan2(hits[1].normal.x, hits[1].normal.y) * Mathf.Rad2Deg);
         Debug.Log(angle);            
         transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, angle);
     }
     if (MoveInput > 0 )
     {
         transform.eulerAngles = new Vector3(0, 0, 0);
     }
     else if(MoveInput < 0)
     {
         transform.eulerAngles = new Vector3(0, 180, 0);
     }
     IsGrounded = Physics2D.OverlapCircle(FeetPos.position, CircleRad, WhatIsGround);
     if (IsGrounded == true && Input.GetKeyDown(KeyCode.Space))
     {
         _Anim.SetTrigger("TakeOff");
         _rb.velocity = Vector2.up * JumpForce;
     }
     if(IsGrounded == false)
     {
         _Anim.SetBool("IsJumping", true);
     }
     else
     {
         _Anim.SetBool("IsJumping", false);
     }
 }
 private void FixedUpdate()
 {
     MoveInput = Input.GetAxis("Horizontal"); 
     _rb.velocity = new Vector2 (MoveInput * Speed, _rb.velocity.y);
     if(MoveInput != 0)
     {
         _Anim.SetBool("IsMoving", true);
     }
     else
     {
         _Anim.SetBool("IsMoving", false);
         
     }
     
 }
 
               } pls ignore my messy coding as iam a beginner....and thank u in advance
,
Answer by andzq · Oct 23, 2020 at 08:44 PM
  void Update()
  {
      if (MoveInput > 0 )
      {
          transform.eulerAngles = new Vector3(transform.eulerAngles.x, 0, transform.eulerAngles.z);
      }
      else if(MoveInput < 0)
      {
          transform.eulerAngles = new Vector3(transform.eulerAngles.x, 180, transform.eulerAngles.z);
      }
  }
 
               // replace the respective code snipped in your update with this
instead of setting eulerAngles to (0,0,0) or (0,180,0) it might worth trying to keep the x and z values and only change the 'y' value to set the view direction of your cube (:
iam having another problem here......it goes in the way i want when i climbing the slope, but when it comes to descending the slope....its not want i want 
what i want is to get the slope angle with negtive sign, so that i can rotate him in the opposite way. The angle iam getting now is around 26.....i want out angle to be -26 when descending the slope iam not good at explaining my problem. i hope u understand and have a solution for it....@andzq
hi again. is this what you´re looking for?
I made a quick video --->> DE$$anonymous$$OVIDEO
I was not sure what the rest of your code was before your "Start"-function so I just guessed. I also put some code snippets into separate functions but this is still like 95% your code just without code that was for the animator! $$anonymous$$ost notable change is that I separated the player into two objects. The parent object is an invisible cube with collider and the script whereas the model is a child of this object and is flipped depending on your input direction. Looks like this: I$$anonymous$$AGE TO HIERARCHY
In order for your script to work you will need to make sure your ground is on a layer that is checked in the "WhatIsGround" layermask and your player is NOT on that layer (:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player : $$anonymous$$onoBehaviour
 {
     public Layer$$anonymous$$ask WhatIsGround;
 
     [Space]
     public Transform player$$anonymous$$odel;
 
     [Space]
     public float Speed = 5;
     public float JumpForce = 5;
 
     [Space]
     public float angle;
     public float $$anonymous$$oveInput;
     public bool IsGrounded;
     Rigidbody2D _rb;
 
     void Start()
     {
         _rb = GetComponent<Rigidbody2D>();
         if (_rb == null)
         {
             Debug.LogError("The RigidBody2D is NULL");
         }
     }
     void Update()
     {
         AdjustPlayerToSlope();
         Jump();
         SetPlayer$$anonymous$$odelDirection();
     }
 
     private void FixedUpdate()
     {
         $$anonymous$$oveInput = Input.GetAxis("Horizontal");
         _rb.velocity = new Vector2($$anonymous$$oveInput * Speed, _rb.velocity.y);
     }
 
     void AdjustPlayerToSlope()
     {
         RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up, 1.5f, WhatIsGround);
         if (hit.collider != null)
         {
             IsGrounded = true;
             angle = -Vector2.SignedAngle(hit.normal, Vector2.up);
             transform.eulerAngles = new Vector3(0, 0, angle * $$anonymous$$athf.Sign(transform.eulerAngles.y));
         }
         else
         {
             IsGrounded = false;
         }
     }
 
     void SetPlayer$$anonymous$$odelDirection()
     {
         if ($$anonymous$$oveInput > 0)
         {
             player$$anonymous$$odel.localScale = new Vector3(1, 1, 1);
         }
         else if ($$anonymous$$oveInput < 0)
         {
             player$$anonymous$$odel.localScale = new Vector3(-1, 1, 1);
         }
     }
 
     void Jump()
     {
         if (IsGrounded == true && Input.GetKeyDown(KeyCode.Space))
         {
             _rb.velocity = Vector2.up * JumpForce;
         }
     }
 }
 
                 Answer by sugumarvishnu2 · Oct 24, 2020 at 04:08 AM
Thank u sooo much that really helped ......iam sooo stupid, can't even figure out this simple mistake ....lol
Your answer
 
             Follow this Question
Related Questions
Object can't get to the target point 0 Answers
How do I keep enemies (rigidbodies) from pushing each other? 1 Answer
Limited Rotation in a 2D Platformer 1 Answer
Rigidbody2D.velocity Won't Move Object 3 Answers
2D Top Down, Enemy facing player slow rotation issue. Vector gets messed up after collision. 3 Answers