Raycast issue with clicking the mouse button
Hello, I'm currently following one of unity's tutorials("Let's Try: Shooting with Raycasts") in using raycasts. The problem I'm facing is everytime I click the predefined mouse key and nothing happens. I have setup a debug for as the tutorial has demonstrated. Yet I don't get the same result as the tutorial had. Here is the following code.
if (Input.GetMouseButtonDown(1) && Time.time > nextFire) // Check is Button is pressed and when the button was last pressed. { nextFire = Time.time + fireRate; //Track the time of when the button is pressed and added to fireRate. This aquation is then stored in nextFire varible.
StartCoroutine("ShotEffect"); //Call the IEnumerator using the StartCoroutine method.
Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0f)); // A declared origin point for the array. It is positioned in the center of the camera's view.
RaycastHit hit; // Name a varible "hit" to RaycastHit. This will be used for storing what gameobject is collided by this raycast.
laserLine.SetPosition(0, gunEnd.position); //Set the lineRenderer position origin at the gunEnd position.
//Check to identify what the raycast has collided with.
if(Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange)) //Physics.Raycast return a boolean. Shoot the raycast from the rayOrigin. Then have the raycast travel forward in the center of the camera. Then return additional information when the ray hits something. Finally, the weaponRange will determine how far the ray will go.
{
laserLine.SetPosition(1, hit.point); // When ray collides with something, Set the 2nd ray point.
}
else
{
laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange)); // When the ray collides with nothing, Set the 2nd ray point back to the center of the camera.
}
}
private IEnumerable ShotEffect() { gunAudio.Play(); //Play audio file laserLine.enabled = true; //Turn on Line Renderer from laserLine. yield return shotDuration; //Wait until the time duration in shotDuration is expired. laserLine.enabled = false; //Turn off the Line Renderer. Debug.Log("Working"); }