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

Simple Player Reset Script Not Working

Discussion in 'Editor & General Support' started by chathaway102, Jun 22, 2019.

  1. chathaway102

    chathaway102

    Joined:
    Jun 20, 2019
    Posts:
    13
    Hello everyone. I recently started development on a VR climbing game and was trying to make a simple reset line of code so I can just press a button to reset the player to a specific position.

    Here is the excerpt of code:
    Code (CSharp):
    1.         if (devicel.GetTouchDown(SteamVR_Controller.ButtonMask.ApplicationMenu) || devicer.GetTouchDown(SteamVR_Controller.ButtonMask.ApplicationMenu))
    2.         {
    3.             Body.transform.position = new Vector3(0.004999995f, -0.01099998f, -0.01800001f);
    4.         }
    I have tried doing

    Code (CSharp):
    1.  
    2. Body.transform.position = checkpoint.transform.position
    3.  
    where checkpoint are the coordinates in the above script. But neither of these "solutions" worked for me. Any ideas on what I did wrong? I really just do not understand. (Note, I am not getting an error, and I have tried to send debug text through the GetTouchDown, but it doesn't even show the debug log text!)

    Here is the entire script it is from, if needed:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GripManager : MonoBehaviour
    5. {
    6.     public Rigidbody Body;
    7.  
    8.     public Pull left;
    9.     public Pull Right;
    10.  
    11.     public GameObject checkpoint;
    12.  
    13.     // Update is called once per frame
    14.     void Start()
    15.     {
    16.         Body.constraints = RigidbodyConstraints.FreezePositionY;
    17.     }
    18.     void FixedUpdate()
    19.     {
    20.         var devicer = SteamVR_Controller.Input((int)Right.controller.index);
    21.         var devicel = SteamVR_Controller.Input((int)left.controller.index);
    22.  
    23.         bool isGripped = left.cangrip || Right.cangrip;
    24.  
    25.         if (isGripped)
    26.         {
    27.             if (left.cangrip && devicel.GetTouch(SteamVR_Controller.ButtonMask.Trigger))
    28.             {
    29.                 Body.useGravity = false;
    30.                 Body.isKinematic = true;
    31.                 Body.transform.position += (left.prevpos - left.transform.localPosition);
    32.                 Body.constraints = RigidbodyConstraints.None;
    33.  
    34.             }
    35.             else if (left.cangrip && devicel.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
    36.             {
    37.                 Body.useGravity = true;
    38.                 Body.isKinematic = false;
    39.                 Body.constraints = RigidbodyConstraints.None;
    40.                 Body.velocity = (left.prevpos - left.transform.localPosition) / Time.deltaTime;
    41.             }
    42.  
    43.             if (Right.cangrip && devicer.GetTouch(SteamVR_Controller.ButtonMask.Trigger))
    44.             {
    45.                 Body.useGravity = false;
    46.                 Body.isKinematic = true;
    47.                 Body.constraints = RigidbodyConstraints.None;
    48.                 Body.transform.position += (Right.prevpos - Right.transform.localPosition);
    49.  
    50.             }
    51.             else if (Right.cangrip && devicer.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
    52.             {
    53.                 Body.useGravity = true;
    54.                 Body.isKinematic = false;
    55.                 Body.velocity = (Right.prevpos - Right.transform.localPosition) / Time.deltaTime;
    56.                 Body.constraints = RigidbodyConstraints.None;
    57.             }
    58.  
    59.         }
    60.  
    61.  
    62.         else
    63.         {
    64.             Body.useGravity = true;
    65.             Body.isKinematic = false;
    66.  
    67.         }
    68.  
    69.         if (devicel.GetTouchDown(SteamVR_Controller.ButtonMask.ApplicationMenu) || devicer.GetTouchDown(SteamVR_Controller.ButtonMask.ApplicationMenu))
    70.         {
    71.             Body.transform.position = new Vector3(0.004999995f, -0.01099998f, -0.01800001f);
    72.         }
    73.  
    74.         left.prevpos = left.controller.transform.localPosition;
    75.         Right.prevpos = Right.controller.transform.localPosition;
    76.     }
    77. }
    78.  
     
  2. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    It looks like you have "Body" defined as a Rigidbody. Which doesn't make much sense if you're trying to move it via it's transform. You'd almost have to go Body.parent.transform or Body.gameobject.transform or something like that.

    I have a feeling you're not referencing the gameobject transform of either the "Body" or perhaps the "Checkpoint" as well. Although to me it seems to be mostly a problem with the way you're referencing the "Body" in the code.

    If you want to move something by it's transform you have to make sure you're referencing the gameobject you want to move correctly (the gameobject itself, not just it's rigidbody).
     
  3. chathaway102

    chathaway102

    Joined:
    Jun 20, 2019
    Posts:
    13
    Still not working. I referenced the camera rig as a game object, but still nothing. I also just tried using
    if (devicer.GetTouchDown(SteamVR_Controller.ButtonMask.ApplicationMenu))

    to run a debug log, and still nothing. Is there perhaps something wrong with this one line of code?
    Also, devicer is referenced:
    var devicer = SteamVR_Controller.Input((int)Right.controller.index);