- Home /
The question is answered, right answer was accepted
Camera target on the GameObject which collision
I have almost 10 gameobject and they would collision one by one.how can the camera target on A when A collision,and target on B when B collision,CDE...etc.
Would setting the look rotation of the camera's transform be sufficient?
yes but how can the camera get the position of the collision
you could deter$$anonymous$$e the collision position in OnCollisionEnter by reading collision.contacts[i].point. And to detect what GameObject we collide - use collision.collider.gameObject and it`s transform, if needed.
But in your case each time gameobjects collides, there are 2 objects collides in the same time. What object the camera must look to? And if it should look to contact point - should it follow some object after that, or should stare at empty point where collision was? If it should follow - what exactly object (of two collided) to follow?
like do$$anonymous$$oes collide,how can the camera follow their collision position
Answer by pako · Dec 15, 2014 at 11:39 AM
You can use the 2 scripts below to achieve what you want. You must attach the "Player" script to each GameObject that you want the camera to look at when it collides. You must attach the "CameraBehavior" script on the main camera, or another camera appropriately tagged.
Bear in mind that a single collision between two GameObjects, will result in both the two GameObjects asking the camera to look at them. However, they will be so close that even if the camera looks at each one in succession the effect will not be really noticeable. Nevertheless, you can improve the scripts, so that if a GameObject collides with another which has already called the camera's CameraLookAt method, it will not itself call that method. Or something similar, depending on what you want.
using UnityEngine;
public class CameraBehavior : MonoBehaviour {
//// Use this for initialization
//void Start () {
//}
//// Update is called once per frame
//void Update () {
//}
public void CameraLookAt(Transform target)
{
transform.LookAt(target);
}
}
using UnityEngine;
public class Player : MonoBehaviour
{
private CameraBehavior cameraBehavior;
// Use this for initialization
void Start ()
{
//Get a reference the CameraBehavior component (skript) of the "Main Camera" GameObject (tagged "Main Camera" by default)
//If another camera is to be used, it must be tagged appropriately,
//and it's tag used below insteaed of "Main Camera"
cameraBehavior = GameObject.FindGameObjectWithTag("Main Camera").GetComponent<CameraBehavior>();
}
//// Update is called once per frame
//void Update () {
//}
void OnCollisionEnter(Collision collision)
{
//Pass the Transform of the current GameObject to the CameraLookAt method
cameraBehavior.CameraLookAt(transform);
}
}
Alternative script for smooth camera rotation:
using UnityEngine;
public class CameraBehavior : MonoBehaviour
{
public float RotationSpeed = 2f;
private Transform target; //set this every time there is a new target
private bool hasNewTarget;
private Vector3 relativePos;
private Quaternion targetRotation;
//// Use this for initialization
//void Start () {
//}
// Update is called once per frame
void Update()
{
if (hasNewTarget)
{
relativePos = target.position - transform.position;
targetRotation = Quaternion.LookRotation(relativePos);
}
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, RotationSpeed * Time.deltaTime);
}
public void CameraLookAt(Transform newTarget)
{
target = newTarget;
hasNewTarget = true;
}
}
perfectly! and how can the camera keep the same distance that camera can move smoothly
I don't understand. Camera keep the same distance with what? You are moving the camera? how are you moving the camera?
$$anonymous$$aybe you should consider posting a different question for this.
I mean when the camera look at A,how can it smoothly look at B
I updated my answer with an alternative CameraBehavior script for smooth camera rotation. However, smoothing (or easing) of movement can get quite complex. I gave you a simple solution and hopefully it will work as you want. You may need to experiment with RotationSpeed value.
If you need a more complex type of smooth rotation, maybe you should consider looking at the asset store. Here are the asset store results when you search for "camera":
Follow this Question
Related Questions
Camera re-target 2 Answers
Change the object the camera looks at? 1 Answer
Moving Camera With 2 Players 3 Answers
Restrict Scaling of Object due to camera position and angle. 1 Answer
Target Box not bounding targets 1 Answer