Answer the question
In order to leave comments, you need to log in
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?
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question