Pull OVRCameraRig over RTS map via touch controller grabbing gesture
Currently developing a little prototype RTS with the Oculus VR devkit. I want to implement camera movement via "grabbing" onto the world space with a touch controller and move the OVRCameraRig object on the X- and Z-axes. For the prototype purpose, I rather decided to go with a simpler approach, namely just pull the map around, since it's not that big, and not much is happening on it.
So far I got this script:
public class MapMover : MonoBehaviour
{
private bool movementEnabled;
Transform map;
Transform leftController;
Vector3 offset;
public bool IsMovementEnabled { get { return movementEnabled; } }
private void Awake()
{
leftController = GameObject.Find("LeftHandAnchor").transform;
}
public void BeginMovement()
{
offset = transform.position - leftController.position;
movementEnabled = true;
}
public void EndMovement()
{
movementEnabled = false;
}
private void LateUpdate()
{
if (movementEnabled)
{
transform.position = new Vector3(
(leftController.position.x * 1.1f + offset.x),
transform.position.y,
(leftController.position.z * 1.1f + offset.z)
);
}
}
}
BeginMovement()
is called when I press the Hand Trigger on the left Touch controller. My current results are unproductive: The first BeginMovement()
call is sufficient, but the movement of the map is very minimal, since the controller models are tiny and their position clamps around -1.0f
and 1.0f
. The second call of BeginMovement()
makes the map do a jump to either side before going back to the minimal movement.
My question is, what exactly am I missing? Is there a simpler approach to this?
Answer by scarcloud · Apr 15, 2018 at 01:22 PM
I have changed my approach to the problem and came up with a basic solution that supports my needs:
public class MapMover : MonoBehaviour
{
private bool movementEnabled;
private float factor = 45.0f;
Transform leftController;
Vector3 lastControllerPosition;
Vector3 currentControllerPosition;
private void Awake()
{
leftController = GameObject.Find("LeftHandAnchor").transform;
currentControllerPosition = leftController.transform.position;
}
public void BeginMovement() { movementEnabled = true; }
public void EndMovement() { movementEnabled = false; }
private void Update()
{
lastControllerPosition = currentControllerPosition;
currentControllerPosition = leftController.transform.position;
}
private void LateUpdate()
{
if (movementEnabled) {
Vector3 distance = currentControllerPosition - lastControllerPosition;
transform.Translate(distance.x * factor, 0, distance.z * factor);
}
}
}
Basic function explained:
The script is a component of the map object and is initialized with the current position of the left controller's transform.position
. Each frame, this value is moved to the last position, while the current position is retrieved anew. During the LateUpdate()
, upon meeting the condition of being moved, the difference between current and last position is calculated, and the map object is translated by the result's X- and Z-values multiplied by a factor (after extensive testing, a factor of 45.0f seems comfortable for moving around, but you should definitely test it out to meet your needs).
I hope this will help someone.