- Home /
Simple Camera Movement?! (javascript)
Oh I'm so confuzzled. I've tried a couple of OPC inputs, but I've come up short. I am trying to create a script for cubes, so when you click the in-game cube with your mouse, it lerps the camera to the position, of that cube, with a fixed height from the object.
I'm assuming I'd have to use the OnMouseDown, since the script is attached to the gameobject, but I'm a lil' confused on how to auto set the rotate information from the game cube automaticly, without having to assign the transform in the editor for each cube to itself, or do I even have to use this method? It seems SO simple, so annoying I can't get it done.
Answer by aldonaletto · Aug 21, 2012 at 01:31 AM
It would be better to have a single script attached to the camera, and use raycast to find which cube was clicked:
var height: float = 2; // height above the cube var speed: float = 5; // control lerp speed private var curCube: Transform;
function Update(){ // Lerp the camera to the cube currently selected, if any: if (curCube){ transform.position = Vector3.Lerp(transform.position, curCube.position + Vector3.up height, speed Time.deltaTime); } // select a cube if mouse button pressed: if (Input.GetMouseButtonDown(0)){ var ray = camera.ScreenPointToRay(Input.mousePosition); var hit: RaycastHit; // cubes must have a "Cube" tag to be recognized! if (Physics.Raycast(ray, hit) && hit.transform.tag == "Cube"){ curCube = hit.transform; } } }
Your answer
Follow this Question
Related Questions
How do I move the camera to another object in the scene on mouse down? 1 Answer
Top down shooter, large scale jitter problem. 0 Answers
Setting a cube to be exactly size of intersecting camera view plane 1 Answer
shooting mechanism similar to sniper games and time crisis 1 Answer
Transform.LookAt - wrong vector 4 Answers