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);
}
}
}
}