Q
Q
Queen22020-05-21 22:02:17
Unity
Queen2, 2020-05-21 22:02:17

How to determine the properties of physics for a character?

I'm making a 2D platformer.
When creating a character, there was a problem: he loses his balance at the slightest collision.
What to do and how to establish physics?

My code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class control : MonoBehaviour {

  public float speed = 20f;
    public float horizontalSpeed;
  float speedX;
  public float verticalImpulse;
  Rigidbody2D rb;
  private bool faceRight = true;

  void Start () {
    rb = GetComponent<Rigidbody2D>();
  }
  
  
  void FixedUpdate () {
    float moveX = Input.GetAxis ("Horizontal");
      if (Input.GetKey(KeyCode.A))
      {
      	speedX = -horizontalSpeed;
      }	
      else if (Input.GetKey(KeyCode.D))
      {
      	speedX = horizontalSpeed;
      }
      if (Input.GetKeyDown(KeyCode.Space))
      {
      	rb.AddForce(new Vector2(0, verticalImpulse),ForceMode2D.Impulse);
      }
      transform.Translate(speedX, 0, 0);
      speedX = 0;

     if (moveX > 0 && !faceRight)
      {
           flip();
      }
      
      else if (moveX < 0 && faceRight)
      {
      	flip();
      }
      }
  void flip()
  {
    faceRight = !faceRight;
    transform.localScale = new Vector3 (transform.localScale.x *-1, transform.localScale.y, transform.localScale.z);
  }	
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
piffo, 2020-05-21
@Queen2

Try in the inspector, in the rigidbody check the freeze rotation z checkbox.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question