Skip to content

Instantly share code, notes, and snippets.

@ttruty
Created January 9, 2023 22:13
Show Gist options
  • Save ttruty/1c04f1ac2ea55cf04e2fec5ecae77d7b to your computer and use it in GitHub Desktop.
Save ttruty/1c04f1ac2ea55cf04e2fec5ecae77d7b to your computer and use it in GitHub Desktop.
Arm swinging locomotion for VR in Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwingingArmMotion : MonoBehaviour
{
// Game Objects
[SerializeField] private GameObject LeftHand;
[SerializeField] private GameObject RightHand;
[SerializeField] private GameObject MainCamera;
[SerializeField] private GameObject ForwardDirection;
//Vector3 Positions
[SerializeField] private Vector3 PositionPreviousFrameLeftHand;
[SerializeField] private Vector3 PositionPreviousFrameRightHand;
[SerializeField] private Vector3 PlayerPositionPreviousFrame;
[SerializeField] private Vector3 PlayerPositionCurrentFrame;
[SerializeField] private Vector3 PositionCurrentFrameLeftHand;
[SerializeField] private Vector3 PositionCurrentFrameRightHand;
//Speed
[SerializeField] private float Speed = 70;
[SerializeField] private float HandSpeed;
void Start()
{
PlayerPositionPreviousFrame = transform.position; //set current positions
PositionPreviousFrameLeftHand = LeftHand.transform.position; //set previous positions
PositionPreviousFrameRightHand = RightHand.transform.position;
}
// Update is called once per frame
void Update()
{
// get forward direction from the center eye camera and set it to the forward direction object
float yRotation = MainCamera.transform.eulerAngles.y;
ForwardDirection.transform.eulerAngles = new Vector3(0, yRotation, 0);
// get positons of hands
PositionCurrentFrameLeftHand = LeftHand.transform.position;
PositionCurrentFrameRightHand = RightHand.transform.position;
// position of player
PlayerPositionCurrentFrame = transform.position;
// get distance the hands and player has moved from last frame
var playerDistanceMoved = Vector3.Distance(PlayerPositionCurrentFrame, PlayerPositionPreviousFrame);
var leftHandDistanceMoved = Vector3.Distance(PositionPreviousFrameLeftHand, PositionCurrentFrameLeftHand);
var rightHandDistanceMoved = Vector3.Distance(PositionPreviousFrameRightHand, PositionCurrentFrameRightHand);
// aggregate to get hand speed
HandSpeed = ((leftHandDistanceMoved - playerDistanceMoved) + (rightHandDistanceMoved - playerDistanceMoved));
if(Time.timeSinceLevelLoad > 1f)
{
transform.position += ForwardDirection.transform.forward * HandSpeed * Speed * Time.deltaTime;
}
// set previous position of hands for next frame
PositionPreviousFrameLeftHand = PositionCurrentFrameLeftHand;
PositionPreviousFrameRightHand = PositionCurrentFrameRightHand;
// set player position previous frame
PlayerPositionPreviousFrame = PlayerPositionCurrentFrame;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment