i need help with a script who when i playing and i stay in a range i can activate with a key a camera switcher this is the script, but this script not work, pleaSe help me!!
Vector3 camPosition;
Vector3 myPosition;
int range = 10;
if(Vector3.Distance(myPosition, camPosition) < range)
{
if(Input.GetKeyDown("1"))
{
cam1.enabled = true;
cam2.enabled = false;
}
if(Input.GetKeyDown("2")){
cam1.enabled = false;
cam2.enabled = true;
}
}
There are a lot of unknowns about what you want to do here, and there is either code missing, or you don’t understand how to structure C# classes. Here is something that compiles that may be in the direction of where you are heading:
using UnityEngine;
using System.Collections;
public class Switcher : MonoBehaviour {
public Vector3 camPosition;
public Camera cam1;
public Camera cam2;
float range = 10.0f;
void Update() {
if(Vector3.Distance(transform.position, camPosition) < range)
{
if(Input.GetKeyDown("1"))
{
cam1.enabled = true;
cam2.enabled = false;
}
if(Input.GetKeyDown("2")){
cam1.enabled = false;
cam2.enabled = true;
}
}
}
}
‘cam1’ and ‘cam2’ need to be initialized in the Inspector by dragging the two cameras onto the variable, and ‘camPosition’ needs to be assigned in the inspector. I’m not sure what ‘camPosition’ represents. If it is a reference to the position of one of the cameras, then the code should handle things a bit differently.
public Vector3 camPosition;
Vector3 myPosition;
public gameObject cam1;
public gameObject cam2;
int range = 10;`
void Start(){
cam2.SetActive(false);
}
void Update(){
if(Vector3.Distance(myPosition, camPosition) < range){
if(Input.GetKeyDown(keycode.Alpha1){
cam1.SetActive(false);
cam2.SetActive(true);
{
if(Input.GetKeyDown(keycode.Alpha2){
cam1.SetActive(true);
cam2.SetActive(false);
{
}
}
this should work. you need to assign the camPosition in the Inspector window inside unity.
and the cam1 and cam2 also need to be assigned in the inspector. After all the public variables have been assigned then this script should be good to go!
hope this helps you
-Harry
this script is written in C#
ignore half the script going green, don’t know what happened there XD
THANKS all i go to test the scripts!!! 