Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

1

27.04.2016, 14:13

Unity / C# Trampolin Doppel Sprung bug?

Hi, ich habe ein Problem mit Unity, ich hab mir ein Trampolin erstellt und wenn der Spieler auf dem Trampolin steht macht er ein Doppel Sprung, aber wenn eine Kiste drauf ist springt er ganz normal. Wieso und wie kann ich das ändern das er nur einmal springt?

Spieler Script

C#-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour
{

    //Floats
    public float maxSpeed = 3;
    public float speed = 50f;
    public float jumpPower = 150f;
   

    //Booleans
    public bool grounded;
    public bool canDoubleJump;
    public bool knockFromRight;


    //Audio
    public AudioClip[] audioClip;

    //Stats
    public int curHealth;
    public int maxHealth = 5;

    //Referenzen
    public Rigidbody2D rb2d;
    private Animator anim;
    private GameMaster gm;


    void Start()
    {
        rb2d = gameObject.GetComponent<Rigidbody2D>();
        anim = gameObject.GetComponent<Animator>();
        gm = GameObject.FindGameObjectWithTag("GameMaster").GetComponent<GameMaster>();

        curHealth = maxHealth;
    }

    void Update()
    {

        anim.SetBool("Grounded", grounded);
        anim.SetFloat("Speed", Mathf.Abs(rb2d.velocity.x));





        if (Input.GetAxis("Horizontal") < -0.1f)
        {
            transform.localScale = new Vector3(-3.420575f, 3.420575f, 3.420575f);
        }

        if (Input.GetAxis("Horizontal") > 0.1f)
        {
            transform.localScale = new Vector3(3.420575f, 3.420575f, 3.420575f);
        }

        //Jump
        if (Input.GetButtonDown("Jump"))
        {
            if (grounded)
            {
                rb2d.AddForce(Vector2.up * jumpPower);
                canDoubleJump = true;
            }
            else
            {
                if (canDoubleJump)
                {
                    canDoubleJump = false;
                    rb2d.velocity = new Vector2(rb2d.velocity.x, 0);
                    rb2d.AddForce(Vector2.up * jumpPower / 1.75f);
                }
            }
        }
        //Jump End

      


        if (curHealth > maxHealth)
        {
            curHealth = maxHealth;
        }

        if (curHealth <= 0)
        {
            Die();
        }

    }
    //Update END

    void FixedUpdate()
    {

        Vector3 easeVelocity = rb2d.velocity;
        easeVelocity.y = rb2d.velocity.y;
        easeVelocity.z = 0.0f;
        easeVelocity.x *= 0.75f;



        float h = Input.GetAxis("Horizontal");

        //Fake Friction

        if (grounded)
        {
            rb2d.velocity = easeVelocity;
        }



        //Den Spieler bewegen
        rb2d.AddForce((Vector2.right * speed) * h);


        //Limit wie schnell der Spieler rennt
        if (rb2d.velocity.x > maxSpeed)
        {
            rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
        }

        if (rb2d.velocity.x < -maxSpeed)
        {
            rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
        }


        }

    //FixedUpdate END


    void OnTriggerEnter2D (Collider2D col)
    {
        if(col.CompareTag("Coin"))
        {
            Destroy(col.gameObject);
            gm.points += 50;

            PlaySound(0);
        }

    }
   

   



    void PlaySound(int clip)
     {
        AudioSource audio = GetComponent<AudioSource>();
        audio.clip = audioClip[clip];
        audio.Play();
     }

    public void Die()
    {
        //Restart
        Application.LoadLevel(2);
    }

    public void Damage(int dmg)
    {
        curHealth -= dmg;

        gameObject.GetComponent<Animation>().Play("Player_RedFlash");

        PlaySound(1);
    }



    //Knockback
    public IEnumerator Knockback(float knockDur, float knockbackPwr, Vector3 knockbackDir) 
    {
        float timer = 0;

        while(knockDur > timer)
        {
            timer += Time.deltaTime;

            rb2d.AddForce(new Vector3(knockbackDir.x * -100, knockbackDir.y * knockbackPwr, transform.position.z));
        }

        yield return null;

    }



}


Und hier das Script vom Trampolin

C#-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using UnityEngine;
using System.Collections;

public class Slimey : MonoBehaviour {

    bool onTop;
    public GameObject bouncer;

    public Vector2 velocity;


    Animator anim;

    void Start()
    {
        anim = gameObject.GetComponent<Animator>();
    }

    void Update()
    {
        
    }


    void OnCollisionStay2D(Collision2D col)
    {
        if(onTop)
        {
            anim.SetBool("isStepped", true);
            bouncer = col.gameObject;
        }


    }

    void OnTriggerEnter2D()
    {
        onTop = true;
    }
    void OnTriggerExit2D()
    {
        onTop = false;
        anim.SetBool ("isStepped", false);
    }
    void OnTriggerStay2D()
    {
        onTop = true;
    }

    public void Jump()
    {
        bouncer.GetComponent<Rigidbody2D>().velocity = velocity;

    }

}


Und hier ein Video wie das Aussieht
Danke im voraus :D

2

27.04.2016, 14:45

Ok, ich hab das Problem gelöst, es lag an der Animation Event 8|

Schorsch

Supermoderator

Beiträge: 5 145

Wohnort: Wickede

Beruf: Softwareentwickler

  • Private Nachricht senden

3

27.04.2016, 15:54

Ich muss gestehen ich habe dein Problem nicht mal richtig verstanden und kann es im Video auch nicht wirklich erkennen. Es wäre schön wenn du beim nächsten mal genauer erklären könnte was falsch läuft. Gut ist es immer wenn du ein mal erklärst was eigentlich passieren soll und was stattdessen aktuell passiert. Und vor allem auch wann es passiert. In deinem Beispiel also sowas wie "Ich würde gerne dass der Spieler auf einen Doppelsprung ausführt wenn er auf einem Trampolin springt. Aktuell ist es so dass....", wie gesagt ich bin mir in deinem Fall nicht mal sicher um was es genau ging aber ich denke du verstehst was ich meine. Dann kann dir beim nächsten mal sicher geholfen werden.
„Es ist doch so. Zwei und zwei macht irgendwas, und vier und vier macht irgendwas. Leider nicht dasselbe, dann wär's leicht.
Das ist aber auch schon höhere Mathematik.“

4

27.04.2016, 16:26

Danke Schorsch! Ich hab meinen Text beim Absenden selbst anscheinend nicht gelesen :P Aber ich werde es Korrigieren, danke :D

Werbeanzeige