Commented Button2Midi.cs.

This commit is contained in:
Niall Moody 2020-12-08 16:58:37 +00:00
parent 0d508db542
commit 92e64c7020
8 changed files with 422 additions and 388 deletions

View file

@ -954,7 +954,7 @@ GameObject:
m_Component: m_Component:
- component: {fileID: 1780628284} - component: {fileID: 1780628284}
m_Layer: 0 m_Layer: 0
m_Name: Bang Button m_Name: Midi2Pd Button
m_TagString: Untagged m_TagString: Untagged
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0

View file

@ -1,49 +1,54 @@
// GameManager.cs - Script used to let the player quit by pressing Escape. // GameManager.cs - Script used to let the player quit by pressing Escape.
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Copyright (c) 2018 Niall Moody // Copyright (c) 2018 Niall Moody
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal // of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights // in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is // copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions: // furnished to do so, subject to the following conditions:
// //
// The above copyright notice and this permission notice shall be included in // The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software. // all copies or substantial portions of the Software.
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE. // SOFTWARE.
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
/// Script used to let the player quit by pressing Escape (in a build). /// Script used to let the player quit by pressing Escape (in a build).
public class GameManager : MonoBehaviour { public class GameManager : MonoBehaviour
{
/// Used to ensure we don't create another instance of GameManager if we
/// return to the MainScene. /// Used to ensure we don't create another instance of GameManager if we
private static bool instantiated = false; /// return to the MainScene.
private static bool instantiated = false;
/// We use this to ensure our GameManager doesn't get destroyed when we switch scenes.
void Awake () { /// We use this to ensure our GameManager doesn't get destroyed when we switch scenes.
if(!instantiated) { void Awake ()
DontDestroyOnLoad(this.gameObject); {
instantiated = true; if(!instantiated)
} {
} DontDestroyOnLoad(this.gameObject);
instantiated = true;
/// Listen for the user pressing Escape to quit. }
void Update() { }
if(Input.GetButton("Quit")) {
Application.Quit(); /// Listen for the user pressing Escape to quit.
} void Update()
} {
} if(Input.GetButton("Quit"))
{
Application.Quit();
}
}
}

View file

@ -26,7 +26,8 @@ using UnityEngine;
/// Script to send a random MIDI note to a PD patch when the player enters and /// Script to send a random MIDI note to a PD patch when the player enters and
/// leaves a collision volume. /// leaves a collision volume.
public class Button2Midi : MonoBehaviour { public class Button2Midi : MonoBehaviour
{
/// The PD patch we're going to communicate with. /// The PD patch we're going to communicate with.
public LibPdInstance pdPatch; public LibPdInstance pdPatch;
@ -35,17 +36,29 @@ public class Button2Midi : MonoBehaviour {
/// note off message when the player exits the collision volume. /// note off message when the player exits the collision volume.
private int note; private int note;
/// We send a bang when the player steps on the button (enters the collision /// We send a MIDI note when the player steps on the button (enters the
/// volume). /// collision volume).
void OnTriggerEnter(Collider other) { void OnTriggerEnter(Collider other)
{
//First, pick a random note. This should pick from an octave starting at
//middle C.
note = 60 + Mathf.FloorToInt(Random.value * 12.0f); note = 60 + Mathf.FloorToInt(Random.value * 12.0f);
//Now send our MIDI note on message to our PD patch.
//SendMidiNoteOn's 3 arguments are: channel, note number, velocity
pdPatch.SendMidiNoteOn(0, note, 127); pdPatch.SendMidiNoteOn(0, note, 127);
} }
/// We send a different bang when the player steps off the button (leaves /// We send a note off message when the player steps off the button (leaves
/// the collision volume). /// the collision volume).
void OnTriggerExit(Collider other) { void OnTriggerExit(Collider other)
{
//Send a note off message to our patch when the player leaves the
//button's collision volume.
//Note that there is no dedicated 'SendMidiNoteOff' function. To send a
//note off message we have to send a 'note on' message with a velocity
//of 0.
pdPatch.SendMidiNoteOn(0, note, 0); pdPatch.SendMidiNoteOn(0, note, 0);
} }
} }

View file

@ -1,106 +1,112 @@
// PlayerMovement.cs - Simple first person movement script. // PlayerMovement.cs - Simple first person movement script.
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Copyright (c) 2018 Niall Moody // Copyright (c) 2018 Niall Moody
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal // of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights // in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is // copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions: // furnished to do so, subject to the following conditions:
// //
// The above copyright notice and this permission notice shall be included in // The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software. // all copies or substantial portions of the Software.
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE. // SOFTWARE.
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
/// Simple first person movement script, included so we don't need to include /// Simple first person movement script, included so we don't need to include
/// any Standard Assets (they're far heavier than we need). /// any Standard Assets (they're far heavier than we need).
public class PlayerMovement : MonoBehaviour { public class PlayerMovement : MonoBehaviour
/// Used to implement mouselook on the vertical axis. {
public Camera playerCamera; /// Used to implement mouselook on the vertical axis.
public Camera playerCamera;
/// Used to set the player's rotation around the y-axis.
private float playerRotation; /// Used to set the player's rotation around the y-axis.
/// Used to implement mouselook on the vertical axis. private float playerRotation;
private float viewY; /// Used to implement mouselook on the vertical axis.
private float viewY;
/// Used to let the player jump.
private float jumpAmount; /// Used to let the player jump.
private float jumpAmount;
/// We use this to hide the mouse cursor.
void Start () { /// We use this to hide the mouse cursor.
Cursor.visible = false; void Start()
} {
Cursor.visible = false;
/// This is where we move the Player object and Camera. }
void Update () {
//Get our current WASD speed. /// This is where we move the Player object and Camera.
Vector3 strafe = new Vector3(Input.GetAxis("Horizontal") * 10.0f, 0.0f, 0.0f); void Update()
float forwardSpeed = Input.GetAxis("Vertical") * 10.0f; {
//Get our current WASD speed.
//Get our current mouse/camera rotation. Vector3 strafe = new Vector3(Input.GetAxis("Horizontal") * 10.0f, 0.0f, 0.0f);
playerRotation = Input.GetAxis("Mouse X") * 6.0f; float forwardSpeed = Input.GetAxis("Vertical") * 10.0f;
viewY = Input.GetAxis("Mouse Y") * 4.0f;
//Get our current mouse/camera rotation.
//Don't let the player rotate the camera more than 90 degrees on the playerRotation = Input.GetAxis("Mouse X") * 6.0f;
//y-axis. viewY = Input.GetAxis("Mouse Y") * 4.0f;
viewY = Mathf.Clamp(viewY, -90.0f, 90.0f);
//Don't let the player rotate the camera more than 90 degrees on the
//Rotate the camera up/down. //y-axis.
playerCamera.transform.Rotate(new Vector3(-viewY, 0.0f, 0.0f)); viewY = Mathf.Clamp(viewY, -90.0f, 90.0f);
//Rotate player (clamped so they can't move so fast they make themselves //Rotate the camera up/down.
//sick). playerCamera.transform.Rotate(new Vector3(-viewY, 0.0f, 0.0f));
Mathf.Clamp(playerRotation, -5.0f, 5.0f);
transform.Rotate(0.0f, playerRotation, 0.0f); //Rotate player (clamped so they can't move so fast they make themselves
//sick).
//Jump player. Mathf.Clamp(playerRotation, -5.0f, 5.0f);
CharacterController controller = GetComponent<CharacterController>(); transform.Rotate(0.0f, playerRotation, 0.0f);
Vector3 jumpVector = Vector3.zero;
//Jump player.
//If the player is holding the jump button down, AND they're not yet CharacterController controller = GetComponent<CharacterController>();
//jumping AND on the ground, OR they are jumping but they've not reached Vector3 jumpVector = Vector3.zero;
//the top of the jump, increase their jumpAmount and move them
//accordingly on the y-axis. //If the player is holding the jump button down, AND they're not yet
if(Input.GetButton("Jump")) { //jumping AND on the ground, OR they are jumping but they've not reached
if(((jumpAmount <= 0.0f) && controller.isGrounded) || //the top of the jump, increase their jumpAmount and move them
((jumpAmount > 0.0f) && (jumpAmount < 1.0f))) { //accordingly on the y-axis.
jumpAmount += Time.deltaTime * 5.0f; if(Input.GetButton("Jump"))
{
jumpVector.y = 4.0f + ((1.0f - jumpAmount) * 20.0f); if(((jumpAmount <= 0.0f) && controller.isGrounded) ||
} ((jumpAmount > 0.0f) && (jumpAmount < 1.0f)))
} {
//Otherwise, if they're on the ground but their jumpAmount is not 0, jumpAmount += Time.deltaTime * 5.0f;
//reset it.
else if((jumpAmount > 0.0f) && controller.isGrounded) { jumpVector.y = 4.0f + ((1.0f - jumpAmount) * 20.0f);
jumpAmount = 0.0f; }
} }
//Otherwise, if they're on the ground but their jumpAmount is not 0,
//Move player. //reset it.
Vector3 moveDirection = Vector3.zero; else if((jumpAmount > 0.0f) && controller.isGrounded)
{
//Set the player's direction based on the direction of the mouse and jumpAmount = 0.0f;
//which WASD keys they're pressing. }
moveDirection = transform.rotation * ((Vector3.forward * forwardSpeed) + strafe);
moveDirection.y = jumpVector.y; //Move player.
Vector3 moveDirection = Vector3.zero;
//Apply gravity to the player's y-axis.
moveDirection.y -= 6.0f; //Set the player's direction based on the direction of the mouse and
//which WASD keys they're pressing.
//Finally, apply the updated direction to the player's Controller (this moveDirection = transform.rotation * ((Vector3.forward * forwardSpeed) + strafe);
//will figure out any collisions with the ground, other objects, etc.). moveDirection.y = jumpVector.y;
controller.Move(moveDirection * Time.deltaTime);
} //Apply gravity to the player's y-axis.
} moveDirection.y -= 6.0f;
//Finally, apply the updated direction to the player's Controller (this
//will figure out any collisions with the ground, other objects, etc.).
controller.Move(moveDirection * Time.deltaTime);
}
}

View file

@ -1,69 +1,72 @@
// CircleMotion.cs - Script used to move an object in a circle. // CircleMotion.cs - Script used to move an object in a circle.
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Copyright (c) 2018 Niall Moody // Copyright (c) 2018 Niall Moody
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal // of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights // in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is // copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions: // furnished to do so, subject to the following conditions:
// //
// The above copyright notice and this permission notice shall be included in // The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software. // all copies or substantial portions of the Software.
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE. // SOFTWARE.
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
/// Script used to move an object in a circle. /// Script used to move an object in a circle.
public class CircleMotion : MonoBehaviour { public class CircleMotion : MonoBehaviour
{
/// The Transform of the GameObject we're going to move in a circle.
public Transform objectToMove; /// The Transform of the GameObject we're going to move in a circle.
/// The radius of the circle we're going to move the object in. public Transform objectToMove;
[Range(0.1f, 50.0f)] /// The radius of the circle we're going to move the object in.
public float radius = 10.0f; [Range(0.1f, 50.0f)]
public float radius = 10.0f;
/// Where we are in the circle right now.
private float circleIndex; /// Where we are in the circle right now.
/// The circle's centre position. private float circleIndex;
private Vector3 centrePos; /// The circle's centre position.
private Vector3 centrePos;
/// Used to setup centrePos.
void Start () { /// Used to setup centrePos.
//These lines calculate the centre of the circle we're going to move the void Start()
//object in. We assume the developer has placed the object at the {
//12 o'clock position of the circle, so the centre position is its //These lines calculate the centre of the circle we're going to move the
//position - the circle's radius on the z-axis. //object in. We assume the developer has placed the object at the
centrePos = objectToMove.transform.position; //12 o'clock position of the circle, so the centre position is its
centrePos.z -= radius; //position - the circle's radius on the z-axis.
} centrePos = objectToMove.transform.position;
centrePos.z -= radius;
/// Move the object along its path. }
void Update () {
Vector3 pos = centrePos; /// Move the object along its path.
void Update()
//Update circleIndex to move the object further around the circle. {
circleIndex += 0.01f; Vector3 pos = centrePos;
if(circleIndex > (2.0f * Mathf.PI))
circleIndex -= 2.0f * Mathf.PI; //Update circleIndex to move the object further around the circle.
circleIndex += 0.01f;
//We calculate the object's position by feeding circleIndex into the if(circleIndex > (2.0f * Mathf.PI))
//Sin function for it's x-axis, and Cos for it's z-axis. circleIndex -= 2.0f * Mathf.PI;
pos.x += Mathf.Sin(circleIndex) * radius;
pos.z += Mathf.Cos(circleIndex) * radius; //We calculate the object's position by feeding circleIndex into the
//Sin function for it's x-axis, and Cos for it's z-axis.
//Finally, apply our updated position to the object's Transform. pos.x += Mathf.Sin(circleIndex) * radius;
objectToMove.transform.position = pos; pos.z += Mathf.Cos(circleIndex) * radius;
}
} //Finally, apply our updated position to the object's Transform.
objectToMove.transform.position = pos;
}
}

View file

@ -1,40 +1,42 @@
// Teleport.cs - Simple script used to teleport the player between scenes. // Teleport.cs - Simple script used to teleport the player between scenes.
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Copyright (c) 2018 Niall Moody // Copyright (c) 2018 Niall Moody
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal // of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights // in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is // copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions: // furnished to do so, subject to the following conditions:
// //
// The above copyright notice and this permission notice shall be included in // The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software. // all copies or substantial portions of the Software.
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE. // SOFTWARE.
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.SceneManagement; using UnityEngine.SceneManagement;
/// Simple script used to teleport the player between scenes. /// Simple script used to teleport the player between scenes.
public class Teleport : MonoBehaviour { public class Teleport : MonoBehaviour
{
/// The name of the scene to load when the player walks into the associated portal.
public string sceneToLoad; /// The name of the scene to load when the player walks into the associated portal.
public string sceneToLoad;
/// This gets called when the player walks into the associated portal.
void OnTriggerEnter(Collider other) { /// This gets called when the player walks into the associated portal.
//This loads our new scene. void OnTriggerEnter(Collider other)
SceneManager.LoadSceneAsync(sceneToLoad); {
} //This loads our new scene.
} SceneManager.LoadSceneAsync(sceneToLoad);
}
}

View file

@ -1,52 +1,55 @@
// Button2Bang.cs - Script to send a bang to a PD patch when the player enters // Button2Bang.cs - Script to send a bang to a PD patch when the player enters
// and leaves a collision volume. // and leaves a collision volume.
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Copyright (c) 2018 Niall Moody // Copyright (c) 2018 Niall Moody
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal // of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights // in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is // copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions: // furnished to do so, subject to the following conditions:
// //
// The above copyright notice and this permission notice shall be included in // The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software. // all copies or substantial portions of the Software.
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE. // SOFTWARE.
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
/// Script to send a bang to a PD patch when the player enters and leaves a /// Script to send a bang to a PD patch when the player enters and leaves a
/// collision volume. /// collision volume.
public class Button2Bang : MonoBehaviour { public class Button2Bang : MonoBehaviour
{
/// The PD patch we're going to communicate with.
public LibPdInstance pdPatch; /// The PD patch we're going to communicate with.
public LibPdInstance pdPatch;
/// We send a bang when the player steps on the button (enters the collision
/// volume). /// We send a bang when the player steps on the button (enters the collision
void OnTriggerEnter(Collider other) { /// volume).
//To send a bang to our PD patch, the patch needs a named receive object void OnTriggerEnter(Collider other)
//(in this case, named VolumeUp), and then we can just use the {
//SendBang() function to send a bang to that object from Unity. //To send a bang to our PD patch, the patch needs a named receive object
// //(in this case, named VolumeUp), and then we can just use the
//See the BangExample.pd patch for details. //SendBang() function to send a bang to that object from Unity.
pdPatch.SendBang("VolumeUp"); //
} //See the BangExample.pd patch for details.
pdPatch.SendBang("VolumeUp");
/// We send a different bang when the player steps off the button (leaves }
/// the collision volume).
void OnTriggerExit(Collider other) { /// We send a different bang when the player steps off the button (leaves
pdPatch.SendBang("VolumeDown"); /// the collision volume).
} void OnTriggerExit(Collider other)
} {
pdPatch.SendBang("VolumeDown");
}
}

View file

@ -1,65 +1,67 @@
// Proximity2Float.cs - Script to send a float to a PD patch determined by the // Proximity2Float.cs - Script to send a float to a PD patch determined by the
// player's proximity to a specific GameObject. // player's proximity to a specific GameObject.
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Copyright (c) 2018 Niall Moody // Copyright (c) 2018 Niall Moody
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal // of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights // in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is // copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions: // furnished to do so, subject to the following conditions:
// //
// The above copyright notice and this permission notice shall be included in // The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software. // all copies or substantial portions of the Software.
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE. // SOFTWARE.
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
/// Script to send a float to a PD patch determined by the player's proximity to /// Script to send a float to a PD patch determined by the player's proximity to
/// a specific GameObject. /// a specific GameObject.
public class Proximity2Float : MonoBehaviour { public class Proximity2Float : MonoBehaviour
{
// The Pd patch we'll be communicating with.
public LibPdInstance pdPatch; // The Pd patch we'll be communicating with.
// We'll use the transform of the red sphere to judge the player's proximity. public LibPdInstance pdPatch;
public Transform sphereTransform; // We'll use the transform of the red sphere to judge the player's proximity.
public Transform sphereTransform;
/// All our calculations for this class take place in MonoBehaviour's
/// Update() function. /// All our calculations for this class take place in MonoBehaviour's
void Update () { /// Update() function.
//Get the distance between the sphere and the main camera. void Update()
float proximity = Vector3.Distance(sphereTransform.position, Camera.main.transform.position); {
//Get the distance between the sphere and the main camera.
//We want proximity to be in the range 0 -> 1. float proximity = Vector3.Distance(sphereTransform.position, Camera.main.transform.position);
//Since our blue circle has a diameter of 15, its radius will be 7.5,
//hence the following scaling. //We want proximity to be in the range 0 -> 1.
proximity /= 7.5f; //Since our blue circle has a diameter of 15, its radius will be 7.5,
//hence the following scaling.
//We also want the pitch to increase as we get closer to the sphere, proximity /= 7.5f;
//so we invert proximity.
proximity = 1.0f - proximity; //We also want the pitch to increase as we get closer to the sphere,
//so we invert proximity.
if(proximity < 0.0f) proximity = 1.0f - proximity;
proximity = 0.0f;
if(proximity < 0.0f)
//Send our frequency value to the PD patch. proximity = 0.0f;
//Like in Button2Bang.cs/ButtonExample.pd, all we need to be able to
//send floats to our PD patch is a named receive object in the patch (in //Send our frequency value to the PD patch.
//this case, named proximity). We can then use the SendFloat() function //Like in Button2Bang.cs/ButtonExample.pd, all we need to be able to
//to send our float value to that named receive object. //send floats to our PD patch is a named receive object in the patch (in
// //this case, named proximity). We can then use the SendFloat() function
//See the FloatExample.pd patch for details. //to send our float value to that named receive object.
pdPatch.SendFloat("proximity", proximity); //
} //See the FloatExample.pd patch for details.
} pdPatch.SendFloat("proximity", proximity);
}
}