Click an object to move the camera

Hii,

in my scene i have placed a chair, i added a message to check when i click on this object

so it prints prefect

print(“You hit an object”);

now i want to change the camera position when it hits the object

so my code goes -------

var tagName : String; // allow the designer to make a tag in the inspector
var rayDistance : float = 0; // length of the ray for our raycast

function Update () {

if (Input.GetMouseButtonDown(0))
{
print (“yes the button works”);
var hit : RaycastHit;
var ray : Ray = camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (ray,hit,rayDistance))
{
if (hit.transform.tag == tagName)
{

print(“You hit an object”);

var position = Vector3(8,1.8,0);
camera.transform.position = position;

}

}

}

}

error::

MissingComponentException: There is no ‘Camera’ attached to the “playerObject” game object, but a script is trying to access it.
You probably need to add a Camera to the game object “playerObject”. Or your script needs to check if the component is attached before using it.
clickOnGameObject.Update () (at Assets/clickOnGameObject.js:19)

what to do ?? shall i have to do something with FPS > Main Camera ??

thanks in advance

plz help some 1

You probably need to add a Camera to the game object “playerObject”. Or your script needs to check if the component is attached before using it.

or you change

camera.transform.position = position;

to:

Camera.main.transform.position = position;

oh, my FPS looses control by using the 2nd option…, i dont know how to attach cam to an object and i dont wanna do that aswell, coz if i’ll have 20 objects then i may have to attach 20 cameras…is it so ?

I want to take my FPS camera there, i do not understand the meaning of this line " Or your script needs to check if the component is attached before using it. " . …please help me the script way

and when i added the camera to the “Game Object” , i m getting this error -

Assets/clickOnGameObject.js(19,8): BCE0020: An instance of type ‘UnityEngine.Component’ is required to access non static member ‘transform’.

plz guide sir , i m waiting for d reply

still waiting

Its called lets play hidden camera. For some dumb reason, if you destroy the original Main Camera. it forgets what it was. Even if you make another one and call it the same thing.

To combat this, they gave us Current camera. I have as yet to see it work.

Just in case it wasnt bad enough, they said, “OK, well if you want cameras. lets give you all of them.” and we were like WTF… just:

function FindMyDamnedCamera(){
	if(Camera.main) return Camera.main.transform;
	if(Camera.current) return Camera.current.transform;
	return Camera.allCameras[0].transform;
}

var tagName : String; // allow the designer to make a tag in the inspector
var rayDistance : float = 0; // length of the ray for our raycast

function Update () {

if (Input.GetMouseButtonDown(0))
{
print (“yes the button works”);
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (ray,hit,rayDistance))
{
if (hit.transform.tag == tagName)
{
var position = Vector3(8,1.8,0);

Camera.main.transform.position = position;

print(“You hit an object”);

}

}

}

}

function FindMyDamnedCamera(){
if(Camera.main) return Camera.main.transform;
if(Camera.current) return Camera.current.transform;
return Camera.allCameras[0].transform;
}

still it leaves FPS, i changed the Main Camera to Main Camera1 but i dont know what is it happening…

here is my unity package: Free large file hosting. Send big files the easy way!

For one, please use code tags when posting code.

Just putting a function in the code does nothing, you have to reference the code…

var cam=FindMyDamnedCamera();
cam.position=position;

function FindMyDamnedCamera(){
if(Camera.main) return Camera.main.transform;
if(Camera.current) return Camera.current.transform;
return Camera.allCameras[0].transform;
}

Just tag the camera as “MainCamera” :wink:

@ersaurabh101:
What do you mean be “it leaves FPS”?

You need to understand what FPS is and what your controller does. It has a Camera attached to it and when you move the mouse or the keyboard, it moves the controller along with the camera. Now if you chose to change the position of the camera, then u can do what you have been doing so far. In your case, I think you want the FPS object (along with the character, camera, guns etc.) to spawn elsewhere in which case, you just need to call the transform on the FPS object as a whole instead of trying to tear the camera apart and positioning it else where.

Oh, yes diabloroxx , this is the problem…now i have deleted the main camera…so i m only left with the FPS camera , please note i have tagged it to Main camera…

current code says:

Now i am left with just a thing

my FPS [its body] goes to the position, its supposed to go…but cam goes somewhere else…see this video for more clarity…
why cam is not going, what do i do …

Clean simple, works.

var tagName : String = "Untagged";
var rayDistance : float = 100;
var cam : Camera;
var teleportObject : GameObject;

function Start(){
	if (cam == null) cam = Camera.main;
	if (teleportObject == null) teleportObject= gameObject;
}

function Update () {
	if (Input.GetMouseButtonDown(0))
	{
		var hit : RaycastHit;
		var ray : Ray = cam.ScreenPointToRay(Input.mousePosition);
		if (Physics.Raycast (ray,hit,rayDistance))
		{
			if(hit.transform.tag == tagName)
			{
				transform.position = hit.transform.position + Vector3.up*10;	
			}	
		}
	}
}

just a noob question

shall i just copy this in my js file and play ? or i need to change FPS name, tags, plz note i also deleted the main cam…

shall i start fresh or this will work in current project ?

This is a .js file - so make a new one, give it a decent name (e.g. teleport) and c&p the code.

Both the camera, tags and the object to be teleported (etc) can be changed in the editor (under the scripts properties on whatever game object you attach it to), I just put down some general examples in the start function that should work in most cases.

sir i simply made a plane, added 2 boxes in it, i kept them untagged, i creaded a js …copy ur code in it, attached it to an empty game object…saved the scene …played it …and nothing happened…what am i missing, shall i delete the main camera ?

Did you set ‘teleportObject’ to the object you want teleported?

can u elaborate, this is a new term to me…what do u mean by teleportobject ?? and from where is it done ?

ohh now i added … u did not get me sir or may be i m not getting u…what i want is …when i click an object my fps[with body and camera] should go infront of some other object kept somewhere in the scene…

to elaborate whats happening with ur script - i have cube 1 and cube 2, i added teleport script to cube2…now when i click,cube2 goes some distance to y everytime…in the inspector of cube2 i see teleporter object field…what do i need to do with that ?? it wants some game object