Greetings Unity community. I am new to C# and help would be appreciated.
I am trying to write a script that lets you view a character model, but when you click on different parts of the model or different models in the scene, it sets them to be the new camera target.
I am doing that with two scripts:
MouseOrbitBlender.cs - this script is attached to the camera object (“hackCam”). It makes the camera rotate with mouse control around a target object. It’s called MouseOrbitBlender, because like blender it uses middle click to orbit around the target.
This script is a slight modification of a script that somebody here posted before.
OnRightClick.cs- this script is attached to all objects that are potential focus targets. So when I click on objects with it, it tells the camera script to change it’s target value to be the object that the script is attached to. I am aware that it should be called onleftclick, but its not on my priorities to change that
My problem is not that the scripts dont work at all, but in that they don’t do what they’re supposed to.
Problems:
Attaching the OnRightClick script to a cube, a sphere and a cilinder. I dublicated sphere to test if it works to click on clones.
The startTarget is set to sphere in the inspector
- clicking on cube changes the target to cube, however doesn’t let me click on any of the other objects any more
- clicking on cilinder changes the target to cube, it doesn’t let me click on any of the other objects any more
effects. - to be clickable, objects need to be set as coliders. That throws of the camera abit, when it colides with the objects. I need to somehow tell the camera to ignore the other coliders in the scene as it rotates around its target.
The scripts:
Camera that orbits around a target
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control/Mouse Orbit with zoom")]
public class MouseOrbitBlender : MonoBehaviour {
//user defined variables
//calls for a transform variable type and calls that "target"
public Transform target;
//the target now target
public Transform startTarget;
//startObject target
public float distance = 5.0f;
public float xSpeed = 120.0f;
public float ySpeed = 120.0f;
public float yMinLimit = -20f;
public float yMaxLimit = 80f;
public float distanceMin = .5f;
public float distanceMax = 15f;
//internal variables (no Public)
float x = 0.0f;
float y = 0.0f;
// Use this for initialization
void Start () {
target = startTarget;
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
void LateUpdate () {
if (target) {
if ( Input.GetMouseButton (2)){
//if middle click is held the following happens
x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
}
if ( Input.GetMouseButton (1)){
// implement moving Vertically- might need more work!!
}
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);
RaycastHit hit;
if (Physics.Linecast (target.position, transform.position, out hit)) {
distance -= hit.distance;
}
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
//Jan
public void setTarget(Transform newTarget )
{
target = newTarget;
print("new X:"+target.position.x);
print("new Y:"+target.position.y);
}
//Jan test access:
//public int getTarget()
//{
// return 5;
// }
}
Click to make a target script
using UnityEngine;
using System.Collections;
public class onRightClick : MonoBehaviour {
public RaycastHit hit;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
var cam = GameObject.Find("hackCam");
if (Input.GetMouseButtonDown(0)) //LEFT CLICK
//1 is right click, 2 is middle click, 0 is left click
{
//creates a ray and its equal to the mouse
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
//this if checks, a detection of hit in an GameObject with the mouse on screen
//ROTATE OBJECT
if(Physics.Raycast(ray,out hit)){
//WORKING:
MouseOrbitBlender cameraObject = (MouseOrbitBlender) cam.GetComponent("MouseOrbitBlender");
//this prints the clicked object
print("object:"+gameObject.name);
//this tests if we have access to getTarget function in camera object:
//print(cameraObject.getTarget());
//this prints the new target coordinates
//print("target X:"+transform.position.x);
//print("target Y:"+transform.position.y);
//this sets new variable directly to a component above classes
//cameraObject.target = transform;
//this sets variable through a class- WORKS:
//Problems- clicking on cilinder makes it the cube
//clicking on sphere doesnt work (going home)
cameraObject.setTarget(transform);
}
}
}
}
How badly am I messing this up? Is there a way to do this more efficiently or fix it?
Thanks in advance for any help. I just want to improve on an old camera script.