Re: Runner à étage
Publié : 11 Août 2019 10:29
Ok je poste le script entier, y'a des parties qui n'ont bien sure rien à voir.
Voilà le seul et unique script sur mon personnage.
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacBehavior : MonoBehaviour
{
public Rigidbody2D rb;
public float vitesse;
public float maxjump;
private bool isGrounded = false;
public float maxSpeed;
public Vector2 speed;
// Start is called before the first frame update
void Start()
{
SetVelocity(vitesse, 0);
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0) && isGrounded)
{
Jump();
}
}
void Jump()
{
rb.velocity += new Vector2(0, maxjump);
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
void OnCollisionExit2D(Collision2D col)
{
if (col.gameObject.CompareTag("Ground"))
{
isGrounded = false;
}
}
void SetVelocity(float xVelocity, float yVelocity)
{
rb.velocity = new Vector2(xVelocity, yVelocity);
}
void FixedUpdate()
{
rb.AddForce(Vector2.right * speed);
// limitation de la vélocité à maxSpeed...
if (Mathf.Abs(rb.velocity.x) > maxSpeed)
{
rb.velocity = new Vector2(Mathf.Sign(rb.velocity.x) * maxSpeed, rb.velocity.y);
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.CompareTag("Obstacle"))
{
StartCoroutine(ObstacleFind());
}
}
IEnumerator ObstacleFind()
{
yield return new WaitForSeconds(0.1f);
SetVelocity(vitesse / 2, 0);
yield return new WaitForSeconds(0.5f);
SetVelocity(vitesse, 0);
}
}