oneill86 Geschrieben 16. April 2018 Melden Share Geschrieben 16. April 2018 Hallo, ich bin was Unity angeht ein Neuanfänger und möchte auch nur ein kleines Projekt verwirklichen. Ich habe aus dem Asset Store eine kleine Draisine heruntergeladen. Die wenn man die Taste "W" betätigt. Schneller wird und wenn man die Taste "S" drückt langsamer wird. So weit so gut. Nun möchte ich das dass Fahrzeug aber von alleine wieder langsamer wird wenn keine Eingabe stattfindet. Wie löse ich das? Hier der Script der Motion.cs Zitat using System.Collections; using System.Collections.Generic; using UnityEngine; public class HandcarMotion : MonoBehaviour { int wheelSpeed = 0; // Speed of wheels; "0" - start speed; public int stepSpeed; //Step of change speed; public GameObject wheels1; //first wheels; public GameObject wheels2; //second wheels; public GameObject handcar; //all handcar; public Animator anim; public GUIStyle style = new GUIStyle (); void Start (){ anim.speed = 0; //Start speed of animation lever and reducer } void Update () { if (Input.GetKeyDown(KeyCode.W)){ // if you will press key "W" then speed rotation of wheels and animation of lever and reducer will be increased wheelSpeed = wheelSpeed + stepSpeed; anim.speed = wheelSpeed/4.5f; } if ((Input.GetKeyDown (KeyCode.S)) && (wheelSpeed > 0)){ // if you will press key "S" then speed rotation of wheels and animation of lever and reducer will be reduced wheelSpeed = wheelSpeed - stepSpeed; anim.speed = wheelSpeed/4.5f; } wheels1.transform.Rotate (wheelSpeed, 0, 0); //Rotation of first wheels wheels2.transform.Rotate (wheelSpeed, 0, 0); //Rotation of second wheels handcar.transform.Translate (0, 0, wheelSpeed*Time.deltaTime); //Moving handcar } void OnGUI(){ GUI.Label (new Rect (140, Screen.height-170, Screen.width/5, 120),"Key 'W' - Schneller",style); //Help on screen GUI.Label (new Rect (140, Screen.height-100, Screen.width/5, 120),"Key 'S' - Langsamer",style); } } Vielen Dank im Voraus Zitieren Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
Jomnitech Geschrieben 21. April 2018 Melden Share Geschrieben 21. April 2018 bin zwar auch Anfänger aber würde jetzt mal sagen: if (Input.GetKeyDown(KeyCode.W)) { wheelSpeed = wheelSpeed + stepSpeed; anim.speed = wheelSpeed/4.5f; } else { if(wheelSpeed > 0) { wheelSpeed = wheelSpeed - stepSpeed; anim.speed = wheelSpeed/4.5f; } } oder alternativ eine dritte Abfrage: if ((!Input.GetKey (KeyCode.S)) && (!Input.GetKey(KeyCode.W))&&(wheelSpeed > 0)) { wheelSpeed = wheelSpeed - stepSpeed; anim.speed = wheelSpeed/4.5f; } Edit: keine Ahnung warum das jetzt so komisch formatiert wird, aber hoffe es ist leserlich. Zitieren Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.