Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Dec 15, 2014 at 02:42 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by squeamishj · Dec 15, 2014 at 03:40 AM · cameratargetcamera-lookcollison

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VesuvianPrime · Dec 15, 2014 at 03:34 AM 0
Share

Would setting the look rotation of the camera's transform be sufficient?

avatar image squeamishj · Dec 15, 2014 at 09:32 AM 0
Share

yes but how can the camera get the position of the collision

avatar image hav_ngs_ru · Dec 15, 2014 at 10:30 AM 0
Share

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?

avatar image squeamishj · Dec 15, 2014 at 10:51 AM 0
Share

like do$$anonymous$$oes collide,how can the camera follow their collision position

1 Reply

  • Sort: 
avatar image
0
Best Answer

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;
     }
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image squeamishj · Dec 15, 2014 at 01:52 PM 0
Share

perfectly! and how can the camera keep the same distance that camera can move smoothly

avatar image pako · Dec 15, 2014 at 02:40 PM 0
Share

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.

avatar image squeamishj · Dec 16, 2014 at 05:25 AM 0
Share

I mean when the camera look at A,how can it smoothly look at B

avatar image pako · Dec 16, 2014 at 10:54 AM 0
Share

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":

https://www.assetstore.unity3d.com/en/#!/search/camera

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges