Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Jun 22, 2020 at 05:43 PM by Vandal16.
avatar image
0
Question by Vandal16 · Jun 02, 2020 at 04:31 PM · camera2d game

Making main camera focus on trigger

Hello, I have a main camera that follows my player all the time, what I want to do is when the player gets to a specific position it triggers the camera to focus on a different area and then smoothly go back to the player (ex.: after 3sec). I tried to do the "Cinemachine 2D tips and tricks" "Group Camera" option but unfortunately i did not managed to do it by myself. The way I kind want it to look like is like this: https://blogs.unity3d.com/wp-content/uploads/2018/07/cinemachine2d8.gif
Hope I explained well with my English :D

Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
1

Answer by AndyMcGardeen · Jun 02, 2020 at 05:09 PM

I would try something like this:

 public float smoothTime = 0.3f;
 private Vector3 velocity = Vector3.zero;
 
 // If something
 StartCoroutine(Focus(target, player))
 //...
 
 IEnumerator Focus (Vector3 targetPos, Vector3 playerPos)
 {
     while (Vector3.Distance(cam.position, targetPos) > 0.001f)
     {
         cam.position = Vector3.SmoothDamp(cam.position, targetPos, ref velocity, smoothTime);
         yield return new WaitForEndOfFrame();
     }
 
     yield return new WaitForSecods(3.0f);
 
     while (Vector3.Distance(cam.position, playerPos) > 0.001f)
     {
         cam.position = Vector3.SmoothDamp(cam.position, playerPos, ref velocity, smoothTime);
         yield return new WaitForEndOfFrame();
     }
 }
Comment
Add comment · Show 3 · 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 Vandal16 · Jun 03, 2020 at 03:14 PM 0
Share

Should I attach this script to the main camera or player?

avatar image AndyMcGardeen Vandal16 · Jun 04, 2020 at 06:35 PM 0
Share

Attach it where you think is best. I made a more explicit version, that looks more alike the gif. You should change how the coroutine is triggerd tho, maybe using onTriggerEnter: https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnTriggerEnter2D.html

 using System.Collections;
 using UnityEngine;
 
 public class GroupView : $$anonymous$$onoBehaviour
 {
     public Transform cam;
     public Transform player;
     public Transform target;
     public Vector3 camRelPos = Vector3.forward * 4.0f; //Camera position relative to the player
 
     public float timeToWait = 3.0f; //Time to wait before camera returns to player
     public float smoothTime = 0.3f; //How quickly will the camera reach group view
 
     private Vector3 velocity = Vector3.zero;
 
     private float fieldOfView;
 
     private void Awake()
     {
         fieldOfView = cam.GetComponent<Camera>().fieldOfView;
     }
 
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.E)) //Whatever triggers the group view...
         {
             //***$$anonymous$$ake the camera stop following the player***
             StartCoroutine(Focus());
         }
     }
 
     bool isFocusing = false;
     IEnumerator Focus ()
     {
         if (!isFocusing)
         {
             isFocusing = true;
 
             Vector3 groupPos = GetGroupPosition(target.position, player.position);
             float t = 0;
 
             while (Vector3.Distance(cam.position, groupPos) > 0.02f)
             {
                 groupPos = GetGroupPosition(target.position, player.position);
                 cam.position = Vector3.SmoothDamp(cam.position, groupPos, ref velocity, smoothTime);
 
                 yield return new WaitForFixedUpdate();
             }
 
             while (t < timeToWait)
             {
                 groupPos = GetGroupPosition(target.position, player.position);
                 cam.position = Vector3.SmoothDamp(cam.position, groupPos, ref velocity, smoothTime);
                 t += Time.deltaTime;
 
                 yield return new WaitForFixedUpdate();
             }
 
             while (Vector3.Distance(cam.position, player.position + camRelPos) > 0.02f)
             {
                 cam.position = Vector3.SmoothDamp(cam.position, player.position + camRelPos, ref velocity, smoothTime);
                 yield return new WaitForFixedUpdate();
             }
 
             isFocusing = false;
         }
     }
 
     Vector3 GetGroupPosition(Vector3 targetPos, Vector3 playerPos)
     {
         return new Vector3
             (playerPos.x + (targetPos.x - playerPos.x) / 2,
             playerPos.y + (targetPos.y - playerPos.y) / 2,
             1.0f + (90 / fieldOfView) * $$anonymous$$athf.Abs(targetPos.x - playerPos.x) / 2); //***Should this be negative?****
     }
 }
avatar image Vandal16 AndyMcGardeen · Jun 05, 2020 at 02:49 PM 0
Share

In the script you call "$$anonymous$$ake the camera stop following the player" but how do I do that, I use Cinemachine to follow my player. (2D game btw)

Follow this Question

Answers Answers and Comments

225 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to create a multilayered 2D effect? 1 Answer

Unity 3D Render Camera Bug!!! 1 Answer

Scale game vertically only 0 Answers

Cinemachine camera shake on button press 0 Answers

Camera Smooth - Player moves little bit back when jump 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