Problem with Raycast in C#

Updated on October 24, 2018 in Unity
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 on October 23, 2018

Sooo i am tottally dont understand why my camera doesnt change when i am pressing a keycode button..Can someone explain me why, please?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 public class SwitchCamera : MonoBehaviour 
 {
 public KeyCode keycode;
 public Camera camera1;
 public Camera camera2;
 public float range;
 public void Update()
 {
 if (Input.GetKeyDown(keycode)) 
 {
 RaycastHit hit;
 if (Physics.Raycast(camera1.transform.position, camera1.transform.forward, out hit, range))
 { 
 if (hit.transform.name == "PC")
 {
 camera1.enabled = !camera1.enabled;
 camera2.enabled = !camera2.enabled;
 }
 }
 }
 }
 }
  • Liked by
Reply
0 on October 24, 2018

Input.GetKeyDown only triggers one frame when pressing the button. If you want the hold button instead use: Input.GetKey

  • Liked by
Reply
Cancel