I want to have a system where I click on a GameObject and the camera move the point where the GameObject I clicked is.
My problem is that the camera starts moving but never stops.
any help? please and thank you!
void update()
{
if (Input.GetButtonDown("Fire1"))
{
Collider target = CastRayToWorld();
if(target != null)
{
GameObject objT = target.gameObject;
targetPosition = objT.transform.position;
Debug.Log ("x " + targetPosition.x + "y " + targetPosition.y);
defaultPosition = transform.position;
targetPosition = (targetPosition - defaultPosition);
}
}
//camera movement. still not working
if (transform.position != targetPosition){
Camera.main.transform.Translate((targetPosition)* 0.8f * Time.deltaTime);
}
}
I would use a raycast on it. When you click on the gameobject store it and when the camera rotates to it and the raycast hits it and the tags match stop the camera movement.
There’s a couple of problems with your code. Assuming that the CastRayToWorld method is functioning - you end up with a targetPosition vector that you are attempting to use as both a direction and a position in your last if statement, which is very unlikely to every be false, hence your camera keeps moving.
What you’re going to want to do is something like this (assuming this is attached to the camera):
public float CameraSpeed = 5.0f; //or anything greater than 0
private Transform _target;
void Update()
{
if(Input.GetButtonDown("Fire1"))
{
Collider targetObject = CastRayToWorld();
if(targetObject != null)
{
_target = targetObject.transform;
Debug.Log("x " + _target.position.x + "y " + _target.position.y);
}
}
//camera movement. should now be working
if(_target != null)
{
var difference = _target.position - transform.position;
var velocity = difference.normalized * CameraSpeed;
var movement = velocity * Time.deltaTime;
if(movement.sqrMagnitude >= difference.sqrMagnitude)
{
transform.position = _target.position;
//you can set _target = null if you want the camera to stop moving once it reaches it
}
else
{
transform.Translate(movement);
}
}
}
the only other thing that i think you should look at is if you are experiencing small jittering of the camera once you get to your location.
that is usually caused by the need to set a range to your target that is near the distance the camera can move in one movement… since if your target is less than your current velocity you may overshoot your target position and with the vector being changed every frame try to correct and have this keep happening.
to fix just set an acceptable range from target, and check for distance to target and then set movement.
I did notice the check of the difference of sqrMagnitude, however I have had troubles with this before, and since Mai is having issues with the camera still moving I thought it should be brought up as something to be aware of.
Hmmm - the only way I can imagine you were having trouble with comparing vector square magnitudes would be if you subsequently attempted to translate the moving object instead of simply setting its position, which may cause jittering due to float precision errors. Simply setting the position avoids that along with the overhead of introducing and checking for an acceptable range between an object and its target.
If you can recall the specific case that was giving you trouble I’d love to dissect it as I’m convinced this approach eliminates that concern : P