51 lines
1.6 KiB
C#
51 lines
1.6 KiB
C#
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;
|
|
private Quaternion offsetRotation;
|
|
|
|
// 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 (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))
|
|
{
|
|
simpleShoot.StartShoot();
|
|
audio.Play();
|
|
}
|
|
}
|
|
}
|