- Home /
Question by
TestAccVr · May 16 at 09:51 AM ·
c#vrlocomotionlocomotion system
Swining Arm Locomotion
Hi, I have a script that moves the player in front when he waves his hands. I would like to add a line to it, which will allow to start the movement only if the player starts waving with the left hand. Any ideas how to do this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class WalkInPlaceLocomotion : MonoBehaviour
{
[SerializeField] CharacterController characterController;
[SerializeField] GameObject leftHand, rightHand;
Vector3 previousPosLeft, previousPosRight, direction;
Vector3 gravity = new Vector3(0, -9.8f, 0);
[SerializeField] InputActionReference leftcontrollerReference;
[SerializeField] InputActionReference rightcontrollerReference;
public GameObject ForwardDirection;
[SerializeField] float speed = 4;
// Start is called before the first frame update
void Start()
{
SetPreviousPos();
}
// Update is called once per frame
void Update()
{
//Calculate the velocity of the player hand movement
Vector3 leftHandVelocity = leftHand.transform.position - previousPosLeft;
Vector3 rightHandVelocity = rightHand.transform.position - previousPosRight;
float totalVelocity = +leftHandVelocity.magnitude * 0.8f + rightHandVelocity.magnitude * 0.8f;
if(totalVelocity >= 0.05f
&& leftcontrollerReference.action.IsPressed()
&& rightcontrollerReference.action.IsPressed()
&& Time.timeSinceLevelLoad > 1f)//If true Player has swing their hand
{
//getting the direction that the player is facing
direction = Camera.main.transform.forward;
//move the player using character controller
characterController.Move(speed * Time.deltaTime * Vector3.ProjectOnPlane(direction, Vector3.up));
}
//Applying gravity
characterController.Move(gravity * Time.deltaTime);
SetPreviousPos();
}
void SetPreviousPos()
{
previousPosLeft = leftHand.transform.position;
previousPosRight = rightHand.transform.position;
}
}
Comment