Skip to content

Commit 96395c9

Browse files
committed
Example script to navigate a 'camera' using a Flystick joystick
1 parent 6dfcbd5 commit 96395c9

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

Examples/ExampleCameraNavigator.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/* ART DTRACK Plugin for Unity Game Engine: ExampleCameraNavigator.cs
2+
*
3+
* Example event listener routine to navigate a camera using a Flystick joystick.
4+
* For use in own scripts.
5+
*
6+
* Copyright (c) 2023 Advanced Realtime Tracking GmbH & Co. KG
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in the
15+
* documentation and/or other materials provided with the distribution.
16+
* 3. Neither the name of copyright holder nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
using System;
33+
using UnityEngine;
34+
35+
namespace DTrack.Examples
36+
{
37+
38+
39+
public class ExampleCameraNavigator : MonoBehaviour
40+
{
41+
private const float speedLoc = 4.0f; // maximum speed to move in meter per second
42+
private const float speedRot = 90.0f; // maximum speed to rotate in degree per second
43+
44+
private float joystickX = 0.0f;
45+
private float joystickY = 0.0f;
46+
47+
48+
// Receive event on changed Flystick joystick values.
49+
//
50+
// Event is invoked every time, when one of the joystick values changed
51+
//
52+
// joystickX : horizontal joystick value (-1.0 .. 1.0)
53+
// joystickY : vertical joystick value (-1.0 .. 1.0)
54+
55+
public void OnJoystickChanged( float joystickX, float joystickY )
56+
{
57+
this.joystickX = joystickX;
58+
this.joystickY = joystickY;
59+
}
60+
61+
62+
void FixedUpdate()
63+
{
64+
if ( this.joystickY != 0.0f )
65+
{
66+
float ds = this.joystickY * speedLoc * Time.fixedDeltaTime;
67+
68+
this.transform.position += ds * Vector3.ProjectOnPlane( this.transform.rotation * Vector3.forward, Vector3.up );
69+
}
70+
71+
if ( this.joystickX != 0.0f )
72+
{
73+
float da = this.joystickX * speedRot * Time.fixedDeltaTime;
74+
75+
this.transform.rotation = Quaternion.Euler( 0.0f, da, 0.0f ) * this.transform.rotation;
76+
}
77+
}
78+
}
79+
80+
81+
} // namespace DTrack.Examples
82+

Examples/ExampleCameraNavigator.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)