Page 2 sur 2

Re: Runner à étage

Publié : 11 Août 2019 10:29
par bloodfang
Ok je poste le script entier, y'a des parties qui n'ont bien sure rien à voir.

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);


    }
}
Voilà le seul et unique script sur mon personnage.

Re: Runner à étage

Publié : 11 Août 2019 10:44
par Max
En reprenant ton script et en ne gardant que la partie liée au déplacement du player,

Code : Tout sélectionner

    public Rigidbody2D rb;
    public float maxSpeed = 4.0f;
    public float speed = 70.0f;
    public float maxjump = 10.0f;

    private bool isGrounded = false;

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
         if (Input.GetMouseButtonDown(0) && isGrounded)
        {
            Jump();
        }
    }
    void Jump()
    {
        rb.velocity += new Vector2(0, maxjump);
        isGrounded = false;
    }

    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 OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.CompareTag("ground"))
        {
            isGrounded = true;
        }
    }

Re: Runner à étage

Publié : 11 Août 2019 13:03
par bloodfang
Waow ok j'aurai jamais reussis à trouver ca tout seul merci !

J'ai fait quelques testes ca a l'aire de fonctionner. La camera ne suivais toujours pas en revanche. Mais je viens de découvrir Cinemachine ca a l'air de fonctionner aussi du coup. Donc ca voudrais dire que j'ai plus qu'à créer un petit monde avec des sprites, l'importer sous android and c'est tout bon :).

Merci à toi pour ton aide en tout cas !

Re: Runner à étage

Publié : 11 Août 2019 13:19
par Max
Pas de soucis ;)
Si tu penses que ce soucis est réglé, passe le sujet en [RESOLU] (édition du titre du message initial).