Disable function in another script
I have two scripts, one in a camera and another on a prefab. The drag N drop script uses mousewheel to rotate the object that has mouse held down on it while the camera also uses mousewheel to scroll in and out, so it zooms and rotates at the same time. I want to disable the camera while player rotates the object. I've tried using GetComponent and been getting a headache.
Things to note: The objects are prefabs that are instantiated that the player can pick up and rotate freely.
The camera is the standardassets fps camera.
Drag N Drop using UnityEngine; using System.Collections; using UnityStandardAssets.Characters.FirstPerson;
public class DragNDrop : MonoBehaviour {
private Vector3 screenPoint;
private Vector3 offset;
private bool isPickedup;
public GameObject targettedObject;
public GameObject colorcanvas;
private bool canvasisopen;
public bool isrotate;
public float lerpSpeed;
public Quaternion fromRotation;
public Quaternion toRotation;
public float xDeg, yDeg;
public int speed = 200;
public float friction;
RaycastHit hit;
//public Camera maincam;
//void OnMouseDown()
//{
// //Vector3 mousePosition = new Vector3(Input.mousePosition.x, 0, Input.mousePosition.z);
// //Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
// //transform.position = objPosition;
// Vector3 mouse = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z);
// offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(mouse);
//}
void Start()
{
//colorcanvas = GetComponentInChildren<GameObject>()
}
void Update()
{
//isrotate=maincam.GetComponent<CameraController>().IsRotate;
//if (Input.GetMouseButton(1) && Input.GetKey(KeyCode.Q))
//{
// xDeg -= Input.GetAxis("Mouse X") * speed * friction;
// yDeg += Input.GetAxis("Mouse Y") * speed * friction;
// Quaternion fromRotation = transform.rotation;
// Quaternion toRotation = Quaternion.Euler(yDeg, xDeg, 0);
// transform.rotation = Quaternion.Lerp(fromRotation, toRotation, Time.deltaTime * 5.0f);
//}
//if (canvasisopen = false && Input.GetMouseButtonDown(2))
//{
// Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// RaycastHit hit;
// Physics.Raycast(ray, out hit);
// targettedObject = hit.transform.gameObject;
// colorcanvas.SetActive(true);
// canvasisopen = true;
//}
//if(canvasisopen = true && Input.GetMouseButtonDown(2) )
//{
// colorcanvas.SetActive(false);
// canvasisopen = false;
//}
Debug.Log(isrotate);
}
void OnTriggerEnter(Collider other){
this.gameObject.transform.position = new Vector3(0, 0, 0);
}
void OnMouseDrag()
{
Plane plane = new Plane(Vector3.up, new Vector3(0, 0, 0));
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hit);
float distance;
if (plane.Raycast(ray, out distance) && hit.transform.tag != "WallArea")
{
transform.position = new Vector3 (ray.GetPoint(distance).x,0,ray.GetPoint(distance).z);
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
isrotate = true;
transform.Rotate(0, Input.GetAxis("Mouse ScrollWheel") * speed, 0);
}
else if(Input.GetAxis("Mouse ScrollWheel") < 0)
{
isrotate = true;
transform.Rotate(0, -Input.GetAxis("Mouse ScrollWheel") * -speed, 0);
}
else{
isrotate = false;
}
}
if (Input.GetKeyDown(KeyCode.Delete))
{
Destroy(this.gameObject);
}
//if (Input.GetKeyDown(KeyCode.T))
//{
// GetComponent<Renderer>().material.color = new Color(0, 0, 0);
//}
}
}
CameraController using UnityEngine; using System.Collections; namespace UnityStandardAssets.Characters.FirstPerson { public class CameraController : MonoBehaviour {
int cameraVelocity = 10;
int zoomspeed = 100;
public float minX = -360.0f;
public float maxX = 360.0f;
public float minY = -45.0f;
public float maxY = 45.0f;
public float sensX = 300.0f;
public float sensY = 300.0f;
float rotationY = 0.0f;
float rotationX = 0.0f;
public GameObject _player;
public int rotationstart=90;
public float dragSpeed = 2;
private Vector3 dragOrigin;
public float turnSpeed = 2.0f; // Speed of camera turning when mouse moves in along an axis
public float panSpeed = 2.0f; // Speed of the camera when being panned
public float zoomSpeed = 2.0f; // Speed of the camera going back and forth
private Vector3 mouseOrigin; // Position of cursor when mouse dragging starts
private bool isPanning; // Is the camera being panned?
private bool isRotating; // Is the camera being rotated?
private bool isZooming; // Is the camera zooming?
private bool haspressed;
public DragNDrop _object;
public bool isrot;
public GameObject[] furniture;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
furniture = GameObject.FindGameObjectsWithTag("GroundFurniture");
if ((Input.GetKey(KeyCode.A)))
{
transform.Translate((Vector3.left * cameraVelocity) * Time.deltaTime);
}
if ((Input.GetKey(KeyCode.D)))
{
transform.Translate((Vector3.right * cameraVelocity) * Time.deltaTime);
}
if ((Input.GetKey(KeyCode.W)))
{
transform.Translate((Vector3.up * cameraVelocity) * Time.deltaTime);
}
if ((Input.GetKey(KeyCode.S)))
{
transform.Translate((Vector3.down * cameraVelocity) * Time.deltaTime);
}
if (Input.GetMouseButton(1))
{
mouseOrigin = Input.mousePosition;
rotationX += Input.GetAxis("Mouse X") * sensX * Time.deltaTime;
rotationY += Input.GetAxis("Mouse Y") * sensY * Time.deltaTime;
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
if (_object.isrotate == false)
{
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
transform.Translate(Vector3.forward * zoomspeed * Time.deltaTime);
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
transform.Translate(Vector3.back * zoomspeed * Time.deltaTime);
}
}
if (Input.GetKeyDown(KeyCode.Z))
{
_player.SetActive(false);
}
}
}
}
Answer by meat5000 · May 04, 2016 at 07:59 AM
From what I read, your problem is that you have a headache.
You need to fill out more information on the actual problem.
To access a Camera you must realise that a Camera is not a GameObject but a Component on a GameObject. In the situations where the camera shortcuts dont seem to be working, use GetComponent to access the Camera component on the Camera's GameObject. I have to admit, I find the shortcuts dont work sometimes but once you have a reference to a camera in your scene it seems you can access all the cameras through that structure.
Now to the problem:
If you rotate the camera by script you can easily add a bool switch/flag to control whether the function processes any code or not.
void MyRotationFunc()
{
if (isActivated)
{
//Rotate
}
}
"isActivated" bool can then be easily controlled from anywhere using GetComponent.
You can put this clause on the function's call rather than inside the function if you choose, but I have a habit of writing generic functions I can fire from anywhere, so I'd want each call to perform the same despite where is was invoked from.
Edit : Here's how I think you want your scripts to work.
Camera Controller (Just the bits I added to your script)
//Omitted Code
public bool cameraFreezeRot = false;
void Update()
{
//Omitted Code
if (!cameraFreezeRot)
{
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
transform.Translate(Vector3.forward * zoomspeed * Time.deltaTime);
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
transform.Translate(Vector3.back * zoomspeed * Time.deltaTime);
}
}
//Omitted Code
}
DragNDrop:
using UnityEngine;
using System.Collections;
using UnityStandardAssets.Characters.FirstPerson;
public class DragNDrop : MonoBehaviour
{
public bool isrotate;
public int speed = 200;
RaycastHit hit;
private CameraController myCam; //ADDED
void Start()
{
myCam = GameObject.FindWithTag("MainCamera").GetComponent<CameraController>(); //ADDED
}
void OnTriggerEnter(Collider other)
{
this.gameObject.transform.position = new Vector3(0, 0, 0);
}
void OnMouseDrag()
{
myCam.cameraFreezeRot = true; //ADDED
Plane plane = new Plane(Vector3.up, new Vector3(0, 0, 0));
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hit);
float distance;
if (plane.Raycast(ray, out distance) && hit.transform.tag != "WallArea")
{
transform.position = new Vector3 (ray.GetPoint(distance).x,0,ray.GetPoint(distance).z);
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
isrotate = true;
transform.Rotate(0, Input.GetAxis("Mouse ScrollWheel") * speed, 0);
}
else if(Input.GetAxis("Mouse ScrollWheel") < 0)
{
isrotate = true;
transform.Rotate(0, -Input.GetAxis("Mouse ScrollWheel") * -speed, 0);
}
else
{
isrotate = false;
}
}
if (Input.GetKeyDown(KeyCode.Delete))
{
Destroy(this.gameObject);
}
}
void OnMouseUp() //ADDED
{
myCam.cameraFreezeRot = false;
}
}
Sorry if I was a bit vague on the problem but i've tried your method and created a bool in the camera script. But now I want to access the component from my prefab. Do point out if I'm messing up or something.
public bool cameraactive;
// Use this for initialization
void Start()
{
cameraactive = true;
}
if (cameraactive == true)
{
if (Input.GetAxis("$$anonymous$$ouse ScrollWheel") > 0)
{
transform.Translate(Vector3.forward * zoomspeed * Time.deltaTime);
}
if (Input.GetAxis("$$anonymous$$ouse ScrollWheel") < 0)
{
transform.Translate(Vector3.back * zoomspeed * Time.deltaTime);
}
}
^ is saying that the camera will be on all the time except when the player has selected an object and is using the scroll wheel to rotate the object.
public Camera camera;
void Start()
{
camera= GetComponent<Camera>
cameraActive = camera.GetComponent<CameraController>().cameraactive;
^ Still incomplete but I want to access the camera bool and disable it in the prefab so that I can use my prefabs rotate function without the camera zoom in. Original problem was because both used scroll wheel input, prefab for rotation, camera for zoom in. I wanted to disable camera when player selected prefab.
Down here is where I want to set it to false so it deactivates.
void On$$anonymous$$ouseDrag()
{
cameraActive = false;
Plane plane = new Plane(Vector3.up, new Vector3(0, 0, 0));
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hit);
float distance;
if (plane.Raycast(ray, out distance) && hit.transform.tag != "WallArea")
{
transform.position = new Vector3(ray.GetPoint(distance).x, 0, ray.GetPoint(distance).z);
if (Input.GetAxis("$$anonymous$$ouse ScrollWheel") > 0)
{
cameraActive = false;
transform.Rotate(0, Input.GetAxis("$$anonymous$$ouse ScrollWheel") * speed, 0);
}
else if (Input.GetAxis("$$anonymous$$ouse ScrollWheel") < 0)
{
transform.Rotate(0, -Input.GetAxis("$$anonymous$$ouse ScrollWheel") * -speed, 0);
}
}
$$anonymous$$y headache is gone, thanks to you. Thank you very much.