VR/Assets/iamagun.cs

51 lines
1.6 KiB
C#
Raw Normal View History

2023-12-07 19:24:27 +01:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class iamagun : MonoBehaviour
{
public SimpleShoot simpleShoot;
public OVRInput.Button shootButton;
public Transform offset;
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
// 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()
{
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);
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();
}
}
}