Search Unity

Question Movement for wmr

Discussion in 'VR' started by natef, Nov 8, 2020.

  1. natef

    natef

    Joined:
    Mar 25, 2020
    Posts:
    1
    I've been working on a VR game and just been using steamvr's teleport. but when I test my script I just fly into oblivion. I don't really know what is wrong with it, and I can't code well so I come for help.

    This is the code;
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class movement : MonoBehaviour {
    5.     public float MoveSpeed = 10f;
    6.     public float TurnSpeed = 2f;
    7.     public float Gravity = 20f;
    8.     public float Jump = 10f;
    9.     float GravityEffect;
    10.     Vector2 InputAxes;
    11.     float Angle;
    12.     float GroundDistance;
    13.     Quaternion TargetRotation;
    14.     Transform MainCamera;
    15.     CharacterController Player;
    16.     void Start() {
    17.         Player = GetComponent<CharacterController> ();
    18.         GroundDistance = Player.bounds.extents.y;
    19.         MainCamera = Camera.main.transform;
    20.     }
    21.     void Update() {
    22.         GetInput ();
    23.         if (Mathf.Abs (InputAxes.x) < 1 && Mathf.Abs (InputAxes.y) < 1) return;
    24.         CalculateDirection ();
    25.         Rotate ();
    26.         ApplyGravity ();
    27.         Vector3 Movement = transform.forward * TurnSpeed * Time.deltaTime;
    28.         Movement.y += GravityEffect;
    29.         Player.Move (Movement);
    30.         PlayerJump ();
    31.         Debug.Log ("gravity: " + GravityEffect.ToString());
    32.    
    33.     }
    34.     void GetInput() {
    35.         InputAxes.x = Input.GetAxisRaw ("Horizontal");
    36.         InputAxes.y = Input.GetAxisRaw ("Vertical");
    37.     }
    38.     void CalculateDirection() {
    39.         Angle = Mathf.Atan2 (InputAxes.x, InputAxes.y);
    40.         Angle = Mathf.Rad2Deg * Angle;
    41.         Angle += MainCamera.eulerAngles.y;
    42.     }
    43.     void Rotate() {
    44.         TargetRotation = Quaternion.Euler (0, Angle, 0);
    45.         transform.rotation = Quaternion.Slerp (transform.rotation, TargetRotation, TurnSpeed * Time.deltaTime);
    46.     }
    47.    
    48.     void PlayerJump() {
    49.         if (IsGrounded ()) {
    50.             if (Input.GetButtonDown ("Jump")) {
    51.                 GravityEffect -= Jump * Time.deltaTime;
    52.                 Debug.Log ("jump");
    53.             }
    54.         }
    55.     }
    56.     void ApplyGravity() {
    57.         if (!IsGrounded ()) {
    58.             GravityEffect -= Gravity * Time.deltaTime;
    59.         } else {
    60.             GravityEffect = 0f;
    61.         }
    62.     }
    63.     bool IsGrounded() {
    64.         return Physics.Raycast (transform.position, -Vector3.up, GroundDistance + 0.1f);
    65.     }
    66. }