├── Autowalk.cs └── config.png /Autowalk.cs: -------------------------------------------------------------------------------- 1 | /* 2 | This script was developed by Jupp Otto. It's free to use and there are no restrictions in modification. 3 | If there are any questions you can send me an Email: juppotto3@gmail.com 4 | This script moves your player automatically in the direction he is looking at. You can 5 | activate the autowalk function by pull the cardboard trigger, by define a threshold angle 6 | or combine both by selecting both of these options. 7 | The threshold is an value in degree between 0° and 90°. So for example the threshold is 8 | 30°, the player will move when he is looking 31° down to the bottom and he will not move 9 | when the player is looking 29° down to the bottom. This script can easally be configured 10 | in the Unity Inspector. 11 | How to get started with this script?: 12 | 0. haven't the Google VR SDK yet, follow this guide https://developers.google.com/vr/unity/get-started 13 | 1. Import the exampple package downloaded in step 0 (GoogleVRForUnity.unitypackage). 14 | 2. Load the GVRDemo scene. 15 | 3. Attach a Rigidbody to the "Player" GameObject. 16 | 4. Freeze X, Y and Z Rotation of the Rgidbody in the inspector. 17 | 5. Attach a Capsule Collider to the "Player" GameObject. 18 | 6. Attach the Autowalk script to the "Player" GameObject. 19 | 7. Configure the Script in the insector for example: 20 | - walk when triggered = true 21 | - speed = 3 22 | 8. Make sure your ground have a collider on it. (Otherwise you will fall through it) 23 | 9. Start the scene and have fun! 24 | */ 25 | 26 | using UnityEngine; 27 | using System.Collections; 28 | 29 | public class Autowalk : MonoBehaviour 30 | { 31 | private const int RIGHT_ANGLE = 90; 32 | 33 | // This variable determinates if the player will move or not 34 | private bool isWalking = false; 35 | 36 | Transform mainCamera = null; 37 | 38 | //This is the variable for the player speed 39 | [Tooltip("With this speed the player will move.")] 40 | public float speed; 41 | 42 | [Tooltip("Activate this checkbox if the player shall move when the Cardboard trigger is pulled.")] 43 | public bool walkWhenTriggered; 44 | 45 | [Tooltip("Activate this checkbox if the player shall move when he looks below the threshold.")] 46 | public bool walkWhenLookDown; 47 | 48 | [Tooltip("This has to be an angle from 0° to 90°")] 49 | public double thresholdAngle; 50 | 51 | [Tooltip("Activate this Checkbox if you want to freeze the y-coordiante for the player. " + 52 | "For example in the case of you have no collider attached to your CardboardMain-GameObject" + 53 | "and you want to stay in a fixed level.")] 54 | public bool freezeYPosition; 55 | 56 | [Tooltip("This is the fixed y-coordinate.")] 57 | public float yOffset; 58 | 59 | void Start() 60 | { 61 | mainCamera = Camera.main.transform; 62 | } 63 | 64 | void Update() 65 | { 66 | // Walk when the Cardboard Trigger is used 67 | if (walkWhenTriggered && !walkWhenLookDown && !isWalking && Input.GetButtonDown("Fire1")) 68 | { 69 | isWalking = true; 70 | } 71 | else if (walkWhenTriggered && !walkWhenLookDown && isWalking && Input.GetButtonDown("Fire1")) 72 | { 73 | isWalking = false; 74 | } 75 | 76 | // Walk when player looks below the threshold angle 77 | if (walkWhenLookDown && !walkWhenTriggered && !isWalking && 78 | mainCamera.transform.eulerAngles.x >= thresholdAngle && 79 | mainCamera.transform.eulerAngles.x <= RIGHT_ANGLE) 80 | { 81 | isWalking = true; 82 | } 83 | else if (walkWhenLookDown && !walkWhenTriggered && isWalking && 84 | (mainCamera.transform.eulerAngles.x <= thresholdAngle || 85 | mainCamera.transform.eulerAngles.x >= RIGHT_ANGLE)) 86 | { 87 | isWalking = false; 88 | } 89 | 90 | // Walk when the Cardboard trigger is used and the player looks down below the threshold angle 91 | if (walkWhenLookDown && walkWhenTriggered && !isWalking && 92 | mainCamera.transform.eulerAngles.x >= thresholdAngle && 93 | Input.GetButtonDown("Fire1") && 94 | mainCamera.transform.eulerAngles.x <= RIGHT_ANGLE) 95 | { 96 | isWalking = true; 97 | } 98 | else if (walkWhenLookDown && walkWhenTriggered && isWalking && 99 | mainCamera.transform.eulerAngles.x >= thresholdAngle && 100 | (Input.GetButtonDown("Fire1") || 101 | mainCamera.transform.eulerAngles.x >= RIGHT_ANGLE)) 102 | { 103 | isWalking = false; 104 | } 105 | 106 | if (isWalking) 107 | { 108 | Vector3 direction = new Vector3(mainCamera.transform.forward.x, 0, mainCamera.transform.forward.z).normalized * speed * Time.deltaTime; 109 | Quaternion rotation = Quaternion.Euler(new Vector3(0, -transform.rotation.eulerAngles.y, 0)); 110 | transform.Translate(rotation * direction); 111 | } 112 | 113 | if (freezeYPosition) 114 | { 115 | transform.position = new Vector3(transform.position.x, yOffset, transform.position.z); 116 | } 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JuppOtto/Autowalk-for-Google-VR-SDK-with-Unity/26ac539372f36dacbcd21efb4b4a063d4e0d30cb/config.png --------------------------------------------------------------------------------