Skip to main content
Up and Coming
August 1, 2014

Crouch function - how to implement?

Hey there,

most demos have little nice details but are missing a crouch function to make exploring more fun with DK2 -
so I would like to test it in Unity.

Could somebody please tell me how to get a "Crouch" function in js to the OVR Controller?
Is it hard to implement? I tried this code I found on the internet. I see that all scripts in OVR folder are C++ - is it compatible?

It would just need to scale down the OVRPlayer Controller when pressing a key and go back again to the original size, when stop pressing the key.


I found this code - but it doesn't work.

#pragma strict

var walkSpeed : float = 7; //Regular speed
var crouchSpeed : float = 3; //Speed while crouching
var sprintSpeed : float = 13; // Speed while sprinting

private var charMotor : CharacterMotor;
private var charController : CharacterController;
private var theTransform : Transform;
private var charHeight : float; //Initial height

function Start ()
{
charMotor = GetComponent(CharacterMotor);
theTransform = transform;
charController = GetComponent(CharacterController);
charHeight = charController.height;
}

function Update ()
{
var h = charHeight;
var speed = walkSpeed;

if (charController.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift"))
{
speed = sprintSpeed;
}

if (Input.GetKey("c"))
{
h = charHeight*0.5;
speed = crouchSpeed;
}

charMotor.movement.maxForwardSpeed = speed; // Setting the max speed
var lastHeight = charController.height; //Stand up/crouch smoothly
charController.height = Mathf.Lerp(charController.height, h, 5*Time.deltaTime);
theTransform.position.y += (charController.height-lastHeight)/2; //Fix vertical position
}


thanks!

AD
    This topic has been closed for replies.

    1 reply

    ad2003Author
    Up and Coming
    August 2, 2014
    mmhh...is this a stupid idea or why does nobody reply? :)
    My first unity steps: In A Distance | download/post: http://goo.gl/N90bUU A video collection of oculus rift related stuff: http://oculus.quitebeyond.de