[Daydream][C#]How would I make a simple directional controller with Google Daydream Controller?
I'm creating a game that uses the Daydream controller as a lightsource (via the gyro) and using the touchpad of the controller as a 4-way direction pad or scroller. I'm thinking of something similar to the Adventure Daydream sample but not exactly the same.
Upon looking at the Controller API basics I think I'll need to create a controller script that uses GvrController.IsTouching to track the position of the users finger on the touchpad, so if the users finger is on the top area of the touchpad then the player will move forward and so on...
// Example: check the position of the user's finger on the touchpad
if (GvrController.IsTouching) {
Vector2 touchPos = GvrController.TouchPos;
// Do something.
}
Will this idea work? Would it be simple to create button parameters on the Daydream touchpad (ei if the finger is in position y=top and x=0 then player is touching button "forward")?
Here is some sample prototype code that I used:
if ( GvrController.IsTouching )
{
// normalize -1.0 to +1.0
var moveVector = 2 * GvrController.TouchPos - Vector2.one;
// reverse y-axis (so up is positive). note: change -0.5f to -1.0f for full speed backwards
moveVector.y = $$anonymous$$athf.Clamp(-moveVector.y, -0.5f, 1.0f);
// we will multiply the magnitude and direction later
var magnitude = moveVector.magnitude;
// deadZone is a float value that allows ignoring the center of the touchpad*
if ( magnitude < deadZone )
moveVector = Vector2.zero;
var direction = moveVector.normalized;
// renormalize (without the deadzone) from -1 to +1
magnitude = (magnitude - deadZone) / (1.0f - deadZone);
// maxPlayerSpeed is another float
moveVector = direction * magnitude * Time.deltaTime * maxPlayerSpeed;
// store this if you want to remove any world-space y-axis movement
var oldY = character.transform.position.y;
character.transform.position += playerCamera.transform.right * moveVector.x;
character.transform.position += playerCamera.transform.forward * moveVector.y;
character.transform.position = new Vector3(
character.transform.position.x,
oldY,
character.transform.position.z
);
}
deadzone info here: http://www.third-helix.com/2013/04/12/doing-thumbstick-dead-zones-right.html
Answer by Edwardayy · Jul 12, 2016 at 03:12 AM
I've tried to put something together but I'm messing up somewhere. I'm not 100% familiar with C# so I'm having trouble putting it together. Can anyone help me out? using UnityEngine; using System.Collections; using Gvr;
public class PlayerController : MonoBehaviour {
// Update is called once per frame
void Update () {
if (GvrController.IsTouching) {
Vector2 touchPos = GvrController.TouchPos;
var forward = touchPos.y (0, .5f);
var backward = touchPos.y (1, .5f);
var left = touchPos.x (-1, 0f);
var right = touchPos.x (1, .5f);
}
}
}
I'm getting missing definitions errors and I'm missing something fundamental. Any help would be nice.
Your answer
Follow this Question
Related Questions
Any ideas in relate to a cube rolling? thank you! 0 Answers
Rigidbody velocity and AddForce overrides,Rigidbody.velocity overrides the Force 0 Answers
Help on how to code crouching? 0 Answers
Remember position for a simple player controller (Left/Right)? 0 Answers
Some problems with handmade basic player controller 0 Answers