So we’re following the Mastery Coding videos and have reached where it shows a VR ray cast changing color when you press a certain button. However we are running into 2 issues, one we can’t seemingly get the raycast to detect the object, and two we can’t stop the recoil animation from continually firing. we have the objects on the same layer and as far as we can tell all is okay in the scene. Here is the script.
public class GunScript : MonoBehaviour
{
LineRenderer line;
float rayLength = 10;
[SerializeField]
LayerMask layerMask;
private Animator gunFireAnim;
// Start is called before the first frame update
void Start()
{
line = GetComponent<LineRenderer>();
gunFireAnim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.G) || OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger))
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, rayLength, layerMask))
{
line.startColor = Color.blue;
gunFireAnim.SetBool("shootGun", true);
}
else
{
line.startColor = Color.grey;
gunFireAnim.SetBool("shootGun", false);
}
}
if (Input.GetKeyDown(KeyCode.G) || OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger))
{
line.startColor = Color.white;
}
line.SetPosition(0, transform.position);
line.SetPosition(1, transform.position + (transform.forward * rayLength));
}
}