- Home /
Wrong Camera axis after "zoom" to GameObject
void Update()
{
// If Alt+linke mouse, Zoom + center to GameObject (Cursor)
if(Input.GetMouseButton(0)&& Input.GetKey(KeyCode.LeftAlt)){
Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit)){
//Debug.Log("hit something at "+hit.distance+" Meter");
// Camera to GameObj,hit.point
Camera.main.transform.LookAt(hit.point);
// Camera move to GameObj, hit.distance
Camera.main.transform.Translate(Vector3.forward*(hit.distance-10));
}
else{
return;
}
}
}
Hi there, i´m pretty new to Unity and C# and just glad to get some script running...
But it does not quiete work, like it should. There are to main things:
The Camera too quikly "jumps" onto the clicked GameObject. Would be nicer, if it would just go there a bit slower.
And most important: After transforming the Cam to Object, the Cam-Axis are a bit strange. After some more clicks, the zoom is at minimum (but i did not zoom with wheel).
When i rotate the Cam, it´s y-Axis is just not straight like before. And it starts to get XYZ Rotation, which i don´t want.
The Cam should just center on a clicked GO and the move close to it (but maintain the right world axis)
Help anybody?
Look at Lerp functions for smooth movement. It is simple enough to Zero the rotations after the movement; you won't notice it in the game.
Thx, i tried to Zero Out with Camera.main.transform.Rotate (0,0,0); and it seems to work.
Can you give an example for lerp? Unity is a Bit Short nö some good example Code
How do i correct the zoom issue?
For lerp, here's the documentation and an example: LERP
Here's another, more generic, example for Lerp.
var $$anonymous$$oveSpeed : float;
function $$anonymous$$ove()
{
var StartPosition : Vector3 = transform.position;
var EndPosition : Vector3;//where the object should end up at
var t : float = 0.0;
while (t < 1.0)
{
t += Time.deltaTime * $$anonymous$$oveSpeed;
transform.position = Vector3.Lerp(StartPosition, EndPosition, t);
yield;
}
}
However, ins$$anonymous$$d of moving the camera towards an object, you can also use the camera's own "zoom" (actually it is it's field of view:
camera.fieldOfView +=5;//for zoo$$anonymous$$g in a bit
Also, take a look here for a complete camera script with moving and zoo$$anonymous$$g.
http://answers.unity3d.com/questions/549572/3d-rts-camera.html
U$$anonymous$$PF - the Zeroing-Out of the unwanted rotations (that i get, after the "lookAT") are not handled by my attempt.
How can i set the camera to rotate and move in "Wordl-Space" again, ins$$anonymous$$d of "around the LookAt´t Gameobject") ??
Your answer
Follow this Question
Related Questions
I am getting a problem with moving and rotating a camera! 1 Answer
Move and Rotate on a fixed axis? 1 Answer
Top-Down Camera Trouble 1 Answer
Space Camera Issue. 1 Answer
camera move x axis 2 Answers