Unitycode

From Medien Wiki
Revision as of 14:33, 31 May 2016 by TVischer (talk | contribs) (Created page with "==Jump Script listening to Osc commands== using UnityEngine; using System.Collections; public class jump : MonoBehaviour { public float jumpForce; public AudioClip[] audioCl...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Jump Script listening to Osc commands

using UnityEngine; using System.Collections;

public class jump : MonoBehaviour {

public float jumpForce; public AudioClip[] audioClip;

public float moveForward;

private Rigidbody myRigidbody;

public static bool canJump = false;

public string RemoteIP = "127.0.0.1";

// the port IanniX is listening on (we send messages to) public int RemotePort = 1234;

// the port that Unity is listening only listening port matters for receiving public int LocalPort = 12001;

private Osc osc; private Udp udp; //->might use these instead! Animator myAnimator; OscMessageHandler AllMessageHandler;

// define the audio clips public AudioClip clipGround; public AudioClip clipHandschuh; public AudioClip clipSuccess; public AudioClip clipFailure;

private AudioSource audioGround; private AudioSource audioHandschuh; private AudioSource audioSuccess; private AudioSource audioFailure;

GameObject handschuh;

private bool succeeded;

void OnCollisionEnter (Collision other){ if (other.gameObject.tag == "ground") { canJump = true; Debug.Log ("collision with ground"); audioGround.Play();

if (handschuh.transform.position.x < transform.position.x) { if (!succeeded) { audioSuccess.Play (); succeeded = true; } if (handschuh.transform.position.x + 4 < transform.position.x) { Application.LoadLevel (0); } }

} else if (other.gameObject.tag == "boxhandschuh") { audioHandschuh.Play (); } }

// Use this for initialization void Start () {

myRigidbody = GetComponent<Rigidbody> (); myAnimator = GetComponent<Animator> ();

// Verbindungen über Udp und Osc laden udp = (Udp) GetComponent("Udp"); osc = (Osc) GetComponent("Osc");

udp.init(RemoteIP, RemotePort, LocalPort); osc.init(udp);

osc.SetAllMessageHandler(AllMessageHandler);

handschuh = GameObject.FindGameObjectWithTag("boxhandschuh");

}

void Awake(){ //from http://answers.unity3d.com/questions/240468/how-to-play-multiple-audioclips-from-the-same-obje.html audioGround = AddAudio(clipGround, false, false, 0.2f); audioHandschuh = AddAudio(clipHandschuh, false, false, 0.4f); audioSuccess = AddAudio(clipSuccess, false, false, 0.8f); audioFailure = AddAudio(clipFailure, false, false, 0.8f); }


// Update is called once per frame void Update () {

if (Input.GetKeyDown (KeyCode.Space) || Osc.MessageWas) {

Osc.MessageWas = false;

if (canJump == true) { myRigidbody.velocity = new Vector3 (0, jumpForce, 0); canJump = false;

} }

if (transform.position.y < -3) { audioFailure.Play (); } if (transform.position.y < -7) { //SceneManager.LoadScene (0); Application.LoadLevel (0); } }


void OnDisable() { Debug.Log("Closing UDP socket"); osc.Cancel(); osc = null; }

public AudioSource AddAudio (AudioClip clip, bool loop, bool playAwake, float vol) {

AudioSource newAudio = gameObject.AddComponent<AudioSource>();

newAudio.clip = clip; newAudio.loop = loop; newAudio.playOnAwake = playAwake; newAudio.volume = vol;

return newAudio;

}


}