VR/Assets/iamagun.cs

103 lines
3.4 KiB
C#
Raw Normal View History

2023-12-07 19:24:27 +01:00
using System.Collections;
using UnityEngine;
public class iamagun : MonoBehaviour
{
public SimpleShoot simpleShoot;
public OVRInput.Button shootButton;
public Transform offset;
2023-12-07 22:42:58 +01:00
public float kickRotationAmount = 10f; // Amount of X-axis rotation during kick
public float kickDuration = 0.1f; // Duration of the kick animation
2023-12-07 19:24:27 +01:00
private AudioSource audio;
private Vector3 offsetPosition;
2023-12-07 21:35:13 +01:00
private Quaternion offsetRotation;
2023-12-07 19:24:27 +01:00
2023-12-07 22:11:07 +01:00
// Vibration variables
private float vibrationDelay = 0.05f; // Delay before the vibration starts
private float vibrationDuration = 0.5f; // Duration of the vibration
private float vibrationIntensity = 1.0f; // Intensity of the vibration
2023-12-07 22:42:58 +01:00
// Flag to check if the kick animation is active
private bool isKicking = false;
2023-12-07 19:24:27 +01:00
// Start is called before the first frame update
void Start()
{
audio = GetComponent<AudioSource>();
2023-12-07 21:35:13 +01:00
// Pre-calculate the offset position and rotation based on the offset transform
2023-12-07 19:24:27 +01:00
offsetPosition = offset.position;
2023-12-07 21:35:13 +01:00
offsetRotation = offset.rotation;
2023-12-07 19:24:27 +01:00
}
// Update is called once per frame
void Update()
{
2023-12-07 22:42:58 +01:00
if (!isKicking && OVRInput.IsControllerConnected(OVRInput.Controller.RTouch))
2023-12-07 19:24:27 +01:00
{
// Get the local position and rotation of the controller
Vector3 controllerLocalPosition = OVRInput.GetLocalControllerPosition(OVRInput.Controller.RTouch);
Quaternion controllerLocalRotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.RTouch);
2023-12-07 21:35:13 +01:00
// Apply the pre-calculated offset position and rotation to the local position and rotation
2023-12-07 19:24:27 +01:00
Vector3 finalPosition = controllerLocalPosition + controllerLocalRotation * offsetPosition;
2023-12-07 21:35:13 +01:00
Quaternion finalRotation = controllerLocalRotation * offsetRotation;
2023-12-07 19:24:27 +01:00
2023-12-07 21:35:13 +01:00
// Set the gun's position and rotation
transform.position = finalPosition;
transform.rotation = finalRotation;
2023-12-07 19:24:27 +01:00
}
if (OVRInput.GetDown(shootButton))
{
simpleShoot.StartShoot();
audio.Play();
2023-12-07 22:11:07 +01:00
// Trigger delayed and intense vibration feedback
StartCoroutine(TriggerVibration());
2023-12-07 22:42:58 +01:00
// Kick the gun forward
StartCoroutine(KickGun());
2023-12-07 19:24:27 +01:00
}
}
2023-12-07 22:11:07 +01:00
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);
}
2023-12-07 22:42:58 +01:00
IEnumerator KickGun()
{
// Set the kick animation flag to true
isKicking = true;
// Remember the initial rotation of the gun
Quaternion initialRotation = transform.rotation;
// Rotate the gun on the X-axis
transform.Rotate(Vector3.right, -kickRotationAmount);
// Wait for the kick duration
yield return new WaitForSeconds(kickDuration);
// Reset the rotation of the gun
transform.rotation = initialRotation;
// Set the kick animation flag to false
isKicking = false;
}
}