Pohyb unity 2D

Příklady kódu

2
0

pohyb unity 2D

// This movement is used for platformer type games
bool jump = false;
float MovementSmoothness = 0.1f;
public float JumpForce;
public PlayerSpeed;
Rigidbody2D rb;

void Start(){
  rb = GetComponent<Rigidbody2D>();
}
void Update()
{
  	horizontal = Input.GetAxisRaw("Horizontal");
  	if(Input.GetKeyDown(KeyCode.W) jump = true;
}

private void FixedUpdate()
{
  	if(jump){
      rb.AddForce(new Vector2(0f, JumpForce));
      jump = false;
    }
  	if(Input.GetKey(KeyCode.LeftShift))// If shift is pressed, sprint
            targetvelocity =  new Vector2(horizontal * PlayerSpeed * 2 * Time.deltaTime, rb.velocity.y);
    else // else go normally
            targetvelocity =  new Vector2(horizonal * PlayerSpeed * Time.deltaTime, rb.velocity.y);
  	// this makes movement much smoother ans satisfying
  	rb.velocity = Vector3.SmoothDamp(rb.velocity,targetvelocity, new Vector3(0,0,0), MovementSmoothness);
}

Související stránky

Související stránky s příklady

V jiných jazycích

Tato stránka je v jiných jazycích

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................