Search Unity

Resolved Once my player enters a portal the player can't jump anymore

Discussion in 'VR' started by thezoeyteh, Jul 30, 2021.

  1. thezoeyteh

    thezoeyteh

    Joined:
    Dec 16, 2020
    Posts:
    4
    My player is supposedly able to jump but after entering a portal they aren't able to jump anymore.
    Here's my portal code:

    Code (CSharp):
    1.   using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.  
    5.     public class PortalTeleporter : MonoBehaviour
    6.     {
    7.         public Transform player;
    8.         public Transform reciever;
    9.  
    10.         private bool playerIsOverlapping = false;
    11.  
    12.         // Update is called once per frame
    13.         void Update () {
    14.             if (playerIsOverlapping)
    15.             {
    16.                 Vector3 portalToPlayer = player.position - transform.position;
    17.                 float dotProduct = Vector3.Dot(transform.up, portalToPlayer);
    18.  
    19.                 // If this is true: The player has moved across the portal
    20.                 if (dotProduct < 0f)
    21.                 {
    22.                     // Teleport him!
    23.                     float rotationDiff = -Quaternion.Angle(transform.rotation, reciever.rotation);
    24.                     rotationDiff += 180;
    25.                     player.Rotate(Vector3.up, rotationDiff);
    26.  
    27.                     Vector3 positionOffset = Quaternion.Euler(0f, rotationDiff, 0f) * portalToPlayer;
    28.                     player.position = reciever.position + positionOffset;
    29.  
    30.                     playerIsOverlapping = false;
    31.                 }
    32.             }
    33.         }
    34.  
    35.         void OnTriggerEnter (Collider other)
    36.         {
    37.             if (other.gameObject.tag == "Player")
    38.             {
    39.                 playerIsOverlapping = true;
    40.                 print("Collided");
    41.             }
    42.         }
    43.  
    44.         void OnTriggerExit (Collider other)
    45.         {
    46.             if (other.gameObject.tag == "Player")
    47.             {
    48.                 playerIsOverlapping = false;
    49.                 print("Exited");
    50.             }
    51.         }
    52.  
    53.     }
    And here is my jumping code:

    Code (CSharp):
    1.     using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.     using UnityEngine.InputSystem;
    5.     using UnityEngine.XR.Interaction.Toolkit;
    6.  
    7.     [RequireComponent (typeof (Rigidbody))]
    8.  
    9.     public class Jumping : MonoBehaviour
    10.     {
    11.         [SerializeField] private InputActionReference jumpActionReference;
    12.         [SerializeField] private float jumpForce = 500.0f;
    13.  
    14.         private XRRig _xrRig;
    15.         private CapsuleCollider _collider;
    16.         private Rigidbody _body;
    17.  
    18.         private bool IsGrounded => Physics.Raycast(
    19.             new Vector2(transform.position.x, transform.position.y + 2.0f),
    20.             Vector3.down, 2.0f);
    21.  
    22.         void Start(){
    23.             _body = GetComponent<Rigidbody>();
    24.             _xrRig = GetComponent<XRRig>();
    25.             _collider= GetComponent<CapsuleCollider>();
    26.             jumpActionReference.action.performed += OnJump;
    27.         }
    28.  
    29.         void Update(){
    30.             var center =_xrRig.cameraInRigSpacePos;
    31.             _collider.center = new Vector3(center.x, _collider.center.y, center.z);
    32.             _collider.height = _xrRig.cameraInRigSpaceHeight;
    33.  
    34.         }
    35.  
    36.         private void OnJump(InputAction.CallbackContext obj){
    37.             if (!IsGrounded) return;
    38.             _body.AddForce(Vector3.up * jumpForce);
    39.             print("Jumped");
    40.         }
    41.     }
     
  2. NevinAF

    NevinAF

    Joined:
    May 12, 2021
    Posts:
    36
    If your portals exit is at a different height than the entrance, the collider on the XRRig will have the incorrect center.y.

    Assuming you want the Y value to be relative to the floor, and always touching the floor, use this in the update function instead:

    Code (CSharp):
    1. void Update(){
    2.     var center =_xrRig.cameraInRigSpacePos;
    3.     _collider.height = _xrRig.cameraInRigSpaceHeight;
    4.     _collider.center = new Vector3(center.x, _collider.height.y / 2, center.z);
    5. }
    Remember that "_xrRig.cameraInRigSpaceHeight" returns eye height, so you might want to add a little padding (fixed) for the head.
     
  3. thezoeyteh

    thezoeyteh

    Joined:
    Dec 16, 2020
    Posts:
    4
    Thank you for your help this worked for me!
     
    NevinAF likes this.