VR/Assets/iamagun.cs

119 lines
3.9 KiB
C#

using System.Collections;
using UnityEngine;
public class iamagun : MonoBehaviour
{
public SimpleShoot simpleShoot;
public OVRInput.Button shootButton;
public Transform offset;
public float kickRotationAmount = 10f; // Amount of X-axis rotation during kick
public float kickDuration = 0.1f; // Duration of the kick animation
private new AudioSource audio;
private Vector3 offsetPosition;
private Quaternion offsetRotation;
// Vibration variables
private float vibrationDelay = 0.1f; // Delay before the vibration starts
private float vibrationDuration = 0.5f; // Duration of the vibration
private float vibrationIntensity = 1.0f; // Intensity of the vibration
// Flag to check if the kick animation is active
private bool isKicking = false;
private bool isInHand = false;
// Start is called before the first frame update
void Start()
{
audio = GetComponent<AudioSource>();
// Pre-calculate the offset position and rotation based on the offset transform
offsetPosition = offset.position;
offsetRotation = offset.rotation;
}
// Update is called once per frame
void Update()
{
if (!isInHand) return;
if (OVRInput.IsControllerConnected(OVRInput.Controller.RTouch))
{
// Get the local position and rotation of the controller
Vector3 controllerLocalPosition = OVRInput.GetLocalControllerPosition(OVRInput.Controller.RTouch);
Quaternion controllerLocalRotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.RTouch);
// Apply the pre-calculated offset position and rotation to the local position and rotation
Vector3 finalPosition = controllerLocalPosition + controllerLocalRotation * offsetPosition;
Quaternion finalRotation = controllerLocalRotation * offsetRotation;
// Set the gun's position and rotation
transform.position = finalPosition;
transform.rotation = finalRotation;
}
if (OVRInput.GetDown(shootButton) && !isKicking)
{
simpleShoot.StartShoot();
audio.Play();
// Trigger delayed and intense vibration feedback
StartCoroutine(TriggerVibration());
// Kick the gun forward
StartCoroutine(KickGun());
}
}
IEnumerator TriggerVibration()
{
// Wait for the specified delay before starting the vibration
yield return new WaitForSeconds(vibrationDelay);
// Trigger vibration with the specified intensity
OVRInput.SetControllerVibration(1, vibrationIntensity, OVRInput.Controller.RTouch);
// Wait for the specified duration
yield return new WaitForSeconds(vibrationDuration);
// Stop the vibration
OVRInput.SetControllerVibration(0, 0, OVRInput.Controller.RTouch);
}
IEnumerator KickGun()
{
// Set the kick animation flag to true
isKicking = true;
// Remember the initial rotation of the gun
Quaternion initialRotation = transform.rotation;
// Calculate the target rotation after the kick
Quaternion targetRotation = initialRotation * Quaternion.Euler(-kickRotationAmount, 0f, 0f);
// Progress variable for smooth interpolation
float elapsedTime = 0f;
while (elapsedTime < kickDuration)
{
// Interpolate the rotation smoothly
transform.rotation = Quaternion.Slerp(initialRotation, targetRotation, elapsedTime / kickDuration);
// Increment the elapsed time
elapsedTime += Time.deltaTime;
yield return null;
}
// Ensure the final rotation is exactly the target rotation
transform.rotation = targetRotation;
// Set the kick animation flag to false
isKicking = false;
}
}