x


Camera Point-and-click Movement

I'm a noob at scripting and haven't seen what I would like to be able to do. Or maybe I have and it hasn't dawned on me....

The idea is to use a script applied to many different cubes that allows a cube to be clicked. When clicked the camera should snap to a location directly in front of that cube. When clicked again, the camera should snap back to its initial location.

I've figured out how how to get a screen cross fade to work between 2 cameras; however, I need to be able to use just the main camera. Any help would be greatly appreciated.

Thanks.

more ▼

asked Jan 26 '10 at 04:05 PM

Kitty gravatar image

Kitty
79 7 7 17

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

This is the bit of code I think you want: EDIT: Hacked to solve your comment.

var myCamera : Transform;
var targetPosition : Vector3;
var zoomedIn : boolean;
var defaultPosition : Vector3;

function Update () {
    if(Input.GetMouseButtonDown(0)){
        if(zoomedIn){
            targetPosition = defaultPosition;
            zoomedIn = false;
        }
        else{

            var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            var hit : RaycastHit;
            if (Physics.Raycast (ray,hit, 50)) {
                Debug.Log(hit.point);
                targetPosition = hit.point;
                zoomedIn =true;
            }
        }
    }

    myCamera.position += 0.25*(targetPosition-myCamera.position);
}

From RaycastHit, you can detect which object you have hit, and also get the point of the hit, allowing you to move the camera.

more ▼

answered Jan 26 '10 at 04:24 PM

Brian Kehrer gravatar image

Brian Kehrer
2.8k 9 11 50

I thought that might work; however, it's not quite right.

I've got the project designed layed out kinda like the periodic table, so I'm trying to make it more of a learning activity program more than a gaming. The user should be allowed to click on an object and the camera move in to see certain information then be allowed to click on it again to see a full view of the layout.

Since there's more than one box of information, I remember seeing something about interpolation for the position and direction of the movement. Maybe something along there would be the best approach?

Jan 26 '10 at 05:43 PM Kitty

This just gets you the correct position or object that the user clicked on - interpolation would be different, ill post a quick sample

Jan 26 '10 at 06:09 PM Brian Kehrer

Be awesome if you could do so.

Jan 27 '10 at 03:31 PM Kitty

I am also trying to do something similar, but I'm having difficulty precisely controlling the camera movement. It's shaky in parts. I want the user to click on certain objects in 3D space and be "zip lined" to that location. It would be AMAZING if you could post a sample. Thanks!

Feb 07 '10 at 04:18 PM Melissa

I'm like Melissa. I like how it goes to where you click; however, I'm looking for something that'll make it go to the center of that object despite where the user clicks. I've put this project aside to work on other things, so I haven't had time to figure it out. Hopefully I'll pick back up on it later this week.

Feb 22 '10 at 04:33 PM Kitty
(comments are locked)
10|3000 characters needed characters left

I had help in another one of my questions, but here's the code that I'm working with applied to the camera that will offset from the object clicked.


#pragma strict

var myCamera : Transform;
var targetPosition : Vector3;
var zoomedIn : boolean;
var defaultPosition : Vector3;
var camOffset : Vector3;

function Start(){
    targetPosition = myCamera.position;
}

function Update () {
    if(Input.GetMouseButtonDown(0)){
        if(zoomedIn){
            targetPosition = defaultPosition;
            zoomedIn = false;
        }        
        else{
            var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            var hit : RaycastHit;
            if (Physics.Raycast (ray,hit, 50)) {
                //targetPosition = hit.point;
                var hitPosition = hit.transform.position;
                targetPosition = hitPosition+camOffset;
                zoomedIn =true;
            }        
        }    
    }    
    myCamera.position += 0.25*(targetPosition-myCamera.position);
}
more ▼

answered Mar 05 '10 at 02:53 PM

Kitty gravatar image

Kitty
79 7 7 17

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5092
x3014
x1535
x486

asked: Jan 26 '10 at 04:05 PM

Seen: 6071 times

Last Updated: Feb 17 '10 at 12:06 PM