Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Is there anyway that I can apply the script to use with HTC vive VR?

Discussion in 'Scripting' started by sarathzz, Jun 17, 2018.

  1. sarathzz

    sarathzz

    Joined:
    May 2, 2018
    Posts:
    6
    Hello,

    I got these 2 code which attempt to replicate Gravity Rush's gravity shifting powers and zero gravity (credit: Rafsan Ratul). The point is im very newbie for unity and scripting so i have no idea how to apply it for use in vr.

    (maybe sound stupid) I tried to moved the localgravity to [cameraRig] -(vr) as a children and delete their oldcamera but the result is i can be able to walk and change the gravity by using keyboard controller not in the vr controller. So i think it need to be change in the script...have any idea?

    here are the codes:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Controller : MonoBehaviour {
    6.  
    7.     public GameObject playerObject;
    8.     public RaycastHit hit;
    9.  
    10.     private ZeroG zeroGravity;
    11.     private playerNormalMovement playerController;
    12.     private Rigidbody myRB;
    13.     private AudioSource[] zeroGAudio;
    14.     private Vector3 velocityState = Vector3.zero;
    15.     private Vector3 angularVelocityState = Vector3.zero;
    16.     private Camera camera;
    17.  
    18.     private bool zeroGEnabled = false;
    19.  
    20.     // Use this for initialization
    21.     void Start () {
    22.         Cursor.lockState = CursorLockMode.Locked;
    23.         zeroGravity = playerObject.GetComponent<ZeroG>();
    24.         playerController = playerObject.GetComponent<playerNormalMovement>();
    25.         myRB = playerObject.GetComponent<Rigidbody>();
    26.         zeroGAudio = playerObject.GetComponents<AudioSource>();
    27.         camera = Camera.main;
    28.     }
    29.    
    30.     // Update is called once per frame
    31.     void Update () {
    32.  
    33.         if(playerController.reached == true)
    34.         {
    35.             StartCoroutine(FadeOut(3, 0.2f));
    36.             camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, 60, 0.1f);
    37.         }
    38.  
    39.         //goto target location
    40.         if(Input.GetButtonDown("Fire1") && zeroGEnabled)
    41.         {
    42.             Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
    43.             Physics.Raycast(ray, out hit);
    44.             Debug.DrawRay(hit.point, hit.normal, Color.cyan, 5.0f);
    45.             DisableZeroG();
    46.             playerController.reached = false;
    47.             zeroGAudio[3].Play();
    48.             zeroGAudio[3].volume = 1.0f;
    49.             StartCoroutine(playerController.JumpToWall(hit.point, hit.normal));
    50.  
    51.         }
    52.        
    53.         //enable zero gravity
    54.         if(Input.GetKeyDown(KeyCode.Z))
    55.         {
    56.             if(zeroGEnabled == false)
    57.             {
    58.                 enableZeroG();
    59.                 enableZeroGAudio();
    60.  
    61.             }
    62.             else
    63.             {
    64.                 DisableZeroG();
    65.             }
    66.         }
    67.  
    68.     }
    69.  
    70.     private void enableZeroG()
    71.     {
    72.         zeroGEnabled = true;
    73.         playerController.enabled = false;
    74.         //Destroy(myRB);
    75.         //playerObject.AddComponent<Rigidbody>();
    76.         velocityState = myRB.velocity;
    77.         angularVelocityState = myRB.angularVelocity;
    78.         myRB.velocity = Vector3.zero;
    79.         myRB.angularVelocity = Vector3.zero;
    80.         zeroGravity.enabled = true;
    81.  
    82.     }
    83.  
    84.     private void enableZeroGAudio()
    85.     {
    86.         zeroGAudio[0].Play();
    87.         zeroGAudio[0].volume = 8.0f;
    88.         zeroGAudio[1].PlayDelayed(.2f);
    89.         zeroGAudio[1].volume = 1.0f;
    90.     }
    91.  
    92.     private void DisableZeroG()
    93.     {
    94.         DisableZeroGAudio();
    95.         zeroGEnabled = false;
    96.         zeroGravity.enabled = false;
    97.         //Destroy(myRB);
    98.         //playerObject.AddComponent<Rigidbody>();
    99.         playerController.enabled = true;
    100.         myRB.velocity = velocityState;
    101.         myRB.angularVelocity = angularVelocityState;
    102.     }
    103.  
    104.     private void DisableZeroGAudio()
    105.     {
    106.         if(zeroGAudio[0].isPlaying)
    107.             StartCoroutine(FadeOut(0, 0.1f));
    108.  
    109.         StartCoroutine(FadeOut(1, 0.1f));
    110.     }
    111.  
    112.     public IEnumerator FadeOut(int track, float speed)
    113.     {
    114.         float audioVolume = zeroGAudio[track].volume;
    115.  
    116.         while(zeroGAudio[track].volume >= speed)
    117.         {
    118.             audioVolume -= speed;
    119.             zeroGAudio[track].volume = audioVolume;
    120.             yield return new WaitForSeconds(0.1f);
    121.         }
    122.     }
    123.  
    124.    
    125. }


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(Rigidbody))]
    6. [RequireComponent(typeof(BoxCollider))]
    7. public class playerNormalMovement : MonoBehaviour {
    8.  
    9.     public float moveSpeed = 12;
    10.     public float turnSpeed = 90;
    11.     public float lerpSpeed = 10;
    12.     public float gravity = 50; //gravity acceleration
    13.     public float jumpSpeed = 10;
    14.     public float deltaGrounded = 0.2f; //player is grounded upto this distance
    15.     public float jumpRage = 2; //range to detect target wall
    16.     public float mouseSensitivty = 5.0f;
    17.  
    18.     private Vector3 surfaceNormal; //current surface normal
    19.     private Vector3 myNormal; //player's normal
    20.     private float distGround; //distance between player position to ground
    21.     private float verticalRotation = 0; //for mouse look
    22.     private Ray ray;
    23.     private RaycastHit hit;
    24.     private Transform gravityCenter;
    25.  
    26.     private Rigidbody myRB;
    27.     private BoxCollider myCollider;
    28.     private Camera camera;
    29.  
    30.     public bool isJumping = false;
    31.     public bool isGrounded = false;
    32.     public bool reached = false;
    33.  
    34.     // Use this for initialization
    35.     void Start () {
    36.  
    37.         myRB = GetComponent<Rigidbody>();
    38.         myCollider = GetComponent<BoxCollider>();
    39.         camera = Camera.main;
    40.  
    41.         myNormal = transform.up;
    42.         myRB.useGravity = false;
    43.         myRB.freezeRotation = true; //disable physics rotation
    44.         distGround = myCollider.bounds.extents.y - myCollider.center.y;
    45.  
    46.      
    47.     }
    48.  
    49.  
    50.     void FixedUpdate()
    51.     {
    52.         myRB.AddForce(-gravity * myRB.mass * myNormal);
    53.     }
    54.  
    55.     // Update is called once per frame
    56.     void Update () {
    57.  
    58.         //jump to wall or simple jump
    59.  
    60.         if (isJumping) return; //abort update while jumping
    61.  
    62.  
    63.  
    64.         if (Input.GetButtonDown("Jump") && isGrounded)
    65.         {
    66.  
    67.             Debug.DrawRay(hit.point, hit.normal, Color.red, 2.0f);
    68.             myRB.AddForce(hit.normal * 1000);
    69.             /*
    70.             ray = new Ray(transform.position, transform.forward);
    71.             if(Physics.Raycast(ray, out hit, jumpRage))
    72.             {
    73.                 StartCoroutine(JumpToWall(hit.point, hit.normal));
    74.             }
    75.             else if(isGrounded) //player is grounded. do normal jump
    76.             {
    77.                
    78.             }
    79.             */
    80.  
    81.         }
    82.  
    83.         //movement code
    84.         transform.Rotate(0, Input.GetAxis("Mouse X") * turnSpeed * Time.deltaTime, 0);
    85.         //camera y movement
    86.         verticalRotation -= Input.GetAxis("Mouse Y") * mouseSensitivty;
    87.         verticalRotation = Mathf.Clamp(verticalRotation, -60f, 60.0f) ;
    88.         camera.transform.localRotation = Quaternion.Euler(verticalRotation, 0, 0);
    89.  
    90.         //update surface normal and isGrounded
    91.         ray = new Ray(transform.position, -myNormal); //cast ray downwards
    92.         if(Physics.Raycast(ray, out hit))
    93.         {
    94.             isGrounded = hit.distance <= distGround + deltaGrounded;
    95.             surfaceNormal = hit.normal;
    96.         }
    97.         else
    98.         {
    99.             isGrounded = false;
    100.             surfaceNormal = Vector3.up;
    101.         }
    102.  
    103.         myNormal = Vector3.Lerp(myNormal, surfaceNormal, lerpSpeed * Time.deltaTime);
    104.         Vector3 myForward = Vector3.Cross(transform.right, myNormal);
    105.         Quaternion targetRot = Quaternion.LookRotation(myForward, myNormal);
    106.         transform.rotation = Quaternion.Lerp(transform.rotation, targetRot, lerpSpeed * Time.deltaTime);
    107.         transform.Translate(0, 0, Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime);
    108.            
    109.  
    110.        }
    111.  
    112.     public IEnumerator JumpToWall(Vector3 point, Vector3 normal)
    113.     {
    114.         Vector3 initialPos = camera.transform.localPosition;
    115.        
    116.         camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, 150, 0.2f);
    117.         isJumping = true;
    118.         myRB.isKinematic = true;
    119.         Vector3 orgPos = transform.position;
    120.         Quaternion orgRot = transform.rotation;
    121.         Vector3 destPos = point + normal * (distGround + 0.5f); //will jump to 0.5 above wall
    122.         Vector3 myForward = Vector3.Cross(transform.right, normal);
    123.         Quaternion destRot = Quaternion.LookRotation(myForward, normal);
    124.  
    125.         for(float t = 0.0f; t < 1.0; )
    126.         {
    127.             camera.transform.localPosition = initialPos + Random.insideUnitSphere * 0.7f;
    128.             t += Time.deltaTime;
    129.             transform.position = Vector3.Lerp(orgPos, destPos, t);
    130.             transform.rotation = Quaternion.Slerp(orgRot, destRot, t);
    131.             yield return null;
    132.         }
    133.        
    134.         myNormal = normal;
    135.         myRB.isKinematic = false;
    136.         isJumping = false;
    137.         reached = true;
    138.     }
    139.  
    140.  
    141.  
    142.  
    143. }


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. // Thanks a lot to PushyPixels@Youtube
    6.  
    7. [RequireComponent(typeof(Rigidbody))]
    8. public class ZeroG : MonoBehaviour {
    9.  
    10.     public float force = 100.0f;
    11.     public float lookForce = .5f;
    12.     public ForceMode forceMode;
    13.  
    14.     private Rigidbody myRB;
    15.  
    16.     void Start()
    17.     {
    18.         myRB = GetComponent<Rigidbody>();
    19.         transform.position += transform.up + new Vector3(0,1,0);
    20.     }
    21.  
    22.    
    23.     void FixedUpdate () {
    24.         Vector3 forceDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")).normalized;
    25.         myRB.AddTorque(transform.up * lookForce * Input.GetAxis("Mouse X"), ForceMode.VelocityChange);
    26.         myRB.AddTorque(-transform.right * lookForce * Input.GetAxis("Mouse Y"), ForceMode.VelocityChange);
    27.         myRB.AddForce(transform.rotation*forceDirection, forceMode);
    28.     }
    29. }


    Thanks in advance!