Switching Camera Target

Hi everyone,

I’m kinda new to scripting in Unity, and i would like to make a solar system kind of scene.

My scene setup is a camera and a sun, and 3 planets orbiting. I have attached to the camera a SmoothLookAt script. What I want to do is, when i click on a planet, the target of the click becomes the target of my SmoothLookAt Script.

I’ ve tried a script attached to the planet and a OnMouseDown function, kinda like :

function OnMouseDown (){
	MyCam.LookAt(Venus);
}

but i only get a console error…

Could someone explain to me how you would do that ? (Should the code go in the camera script, the planet object script ?..)

Thanks a lot for any help

You should set the “target” variable of SmoothLookAt script:

Camera.main.GetComponent("SmoothLookAt").target = gameObject.transform;

To optimize performance you should store “gameObject.transform” into a private variable inside the Start function of your script and assign it instead of using directly gameObject.transform

Hello,

I am a n00b scripter as well. I thought I could use your thread as a challenge for me. So, I made the script you need. I tested it out and everything works good. The only problem is, don’t put the objects too close to each other, but its in space so that’s okay I hope. And the camera jumps quickly to the object it has to look at. Other than those things, this is what you need. All you need to do its copy and paste this onto a new javascript document, put the script on the camera. I made 4 targets for you because thats what you said you needed. Hope everything works, if you have problems let me know.

HOW IT WORKS:

This script will cast a ray towards to location of the mouse cursor and detect if there is a GameObject over the cursor. If the ray is disrupted by an object, it will make the camera look at that object. Also I put some debug logs in there for you. When you click on the object, it will say in the console which object you selected.

var target1: Transform;
var target2: Transform;
var target3: Transform;
var target4: Transform;


function Update () {

    if (Input.GetMouseButton(0)) {
        var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        var hit: RaycastHit;

        if (Physics.Raycast(ray, hit)) {
            if (hit.transform == target1) {
		transform.LookAt(target1);
		Debug.Log ("LookingatTarget1");

            }
			
	if (Physics.Raycast(ray, hit)) {
            if (hit.transform == target2) {
                transform.LookAt(target2);
		Debug.Log ("LookingatTarget2");
            }
			
	if (Physics.Raycast(ray, hit)) {
            if (hit.transform == target3) {
                transform.LookAt(target3);
		Debug.Log ("LookingatTarget3");
            }

	if (Physics.Raycast(ray, hit)) {
            if (hit.transform == target4) {
                transform.LookAt(target4);
		Debug.Log ("LookingatTarget4");
            }
        } else {
            print("Nothing Selected");
        }
	}
	}
	}
    }
}

Thanks again for the quick answer. I will test that tomorrow as soon as i will be free from the Sunday family meal :smile:

Hi,

just tested the code you gave me and put in in my code with a smooth look at but it doesnt seem to do anything (even in the console log). Do i need to declare the GameObject by name ? Do the deplacement code of the cam block something ?

Here is what it look like :

static var MyCam : GameObject; //déclare la caméra pour tout les scripts
//var target : Transform;
//var damping = 6.0;
//var smooth = true;

var target1: Transform;
var target2: Transform;
var target3: Transform;
var target4: Transform;

var Venus : GameObject;


function Start () {	
	MyCam = GameObject.Find ("Main Camera");
	Venus = GameObject.Find ("VENUS");
}


function UpdateWorld() {		
	transform.Translate((Vector3.forward*(GlobalUI_Command.mySlider)) * Time.deltaTime);
	transform.Translate(((Vector3.up*(GlobalUI_Command.mySlider)) * Time.deltaTime)/5);
	transform.Translate(((Vector3.right*(GlobalUI_Command.mySlider)) * Time.deltaTime)/2);
}



/*function LateUpdate () {
	if (target) {
		if (smooth)
		{
			// Look at and dampen the rotation
			var rotation = Quaternion.LookRotation(target.position - transform.position);
			transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
		}
		else
		{
			// Just lookat
		    transform.LookAt(target);
		}
	}
}
*/


function Update() {
	UpdateWorld();
	
    if (Input.GetMouseButton(0)) {
        var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        var hit: RaycastHit;

        if (Physics.Raycast(ray, hit)) {
            if (hit.transform == target1) {
		transform.LookAt(target1);
		Debug.Log ("LookingatTarget1");

            }
			
	if (Physics.Raycast(ray, hit)) {
            if (hit.transform == target2) {
                transform.LookAt(target2);
		Debug.Log ("LookingatTarget2");
            }
			
	if (Physics.Raycast(ray, hit)) {
            if (hit.transform == target3) {
                transform.LookAt(target3);
		Debug.Log ("LookingatTarget3");
            }

	if (Physics.Raycast(ray, hit)) {
            if (hit.transform == target4) {
                transform.LookAt(target4);
		Debug.Log ("LookingatTarget4");
            }
        } else {
            print("Nothing Selected");
        }
	}
	}
	}
    
}

}

Thanks for any help.

PS: The deplacement code make a link to a MySlider var which control the speed of the cam with an horizontal slider on my GUI script.

up

Hi,

What you need to do is apply the script to your camera. There is no need for any other scripts. Just drag and drop the script on to the camera, then drag and drop your planets into the targets, which you will see as a variable. Then just click on the planets and it should work fine.