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 /
  • Help Room /
avatar image
0
Question by Timze · Jul 26, 2017 at 02:13 PM · camera2dscripting problem2d-platformertargets

Same script on two objects, only first one works

So, I have two gameobjects (triggers which both have "targets") which both have same script on them. . . When I place one object with that script, it works just normally but after I place the second one the first one still works normally but the second one works strangely... The second works mostly but the target it looks doesn't change. (it just keeps targeting the "default target") It seems like "movingCameraToTarget" is both false and true on the second object which might cause this all but I have no idea why?

To test this out. 1. Attach the script below to a gameobject and fill the variables (Default target is your player, target is the target you want to look, rest can be what you like... ) 2. Add 2D collider with "Is Trigger" enabled.

Right after the Update is the "smoothCamera.lookAt = targetLookAt.transform;" which is not working correctly on the second object.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LookAtObject : MonoBehaviour
 {
     private GameObject player;
     private Rigidbody2D RB2D;
 
     SmoothCamera smoothCamera;
     public Transform defaultLookAt;                         // Default LookAt transform (Player)
     public Transform targetLookAt;                          // What we want to LookAt?
     public float speed;                                     // The speed of the LookAt movement
     public float cameraSize;                                // Orthographic camera size
 
     public float freezeTime = 3;                            // For how long we want the Player to be frozen
     public bool allowFreezeMovement = true;                 // Tick to allow Player freezing while "movingCameraToTarget" is true
     public bool useOnlyOneTime;                             // Useable for only 1 time? 
 
     private bool oneTimeUsed = false;
     private bool freezeMovement = false;                    // Freeze Player movement while "movingCameraToTarget" is true
     public bool movingCameraToTarget;                       // Is the camera moving to target?
 
 
 
     void Start()
     {
         player = GameObject.FindGameObjectWithTag("Player");
         RB2D = player.GetComponent<Rigidbody2D>();
 
         smoothCamera = FindObjectOfType<SmoothCamera>();
 
         if (allowFreezeMovement == true)
         {
             freezeMovement = true;
         }
         else
         {
             freezeMovement = false;
         }
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.name == "Player" && oneTimeUsed == false && targetLookAt.gameObject.activeInHierarchy == true)
         {
             movingCameraToTarget = true;
             if (freezeMovement == true)
             {
                 StartCoroutine("FreezeMovement");
             }
         }
     }
     void OnTriggerExit2D(Collider2D other)
     {
         if (other.name == "Player")
         {
             movingCameraToTarget = false;
         }
     }
 
     void Update()
     {
         Debug.Log(movingCameraToTarget);
         // Moving camera to "targetLookAt" using "speed" as speed variable
         if( movingCameraToTarget == true)                                                        
         {
             smoothCamera.lookAt = targetLookAt.transform;
             
             Debug.Log("Wanting to look at: " + targetLookAt); // Testing what we want to look at...
 
             smoothCamera.smoothSpeed = speed;
 
             if (smoothCamera.GetComponentInChildren<Camera>().orthographicSize > cameraSize)
             {
                 smoothCamera.GetComponentInChildren<Camera>().orthographicSize = smoothCamera.GetComponentInChildren<Camera>().orthographicSize - 2 * Time.deltaTime;
             }
             if (smoothCamera.GetComponentInChildren<Camera>().orthographicSize < cameraSize)
             {
                 smoothCamera.GetComponentInChildren<Camera>().orthographicSize = smoothCamera.GetComponentInChildren<Camera>().orthographicSize + 2 * Time.deltaTime;
             }
         }
 
         // Move camera back to default ( Target,Speed,Camera size)
         if(movingCameraToTarget == false)                                                  
         {
 
             // What to look after camera ain't moving to target??? 
             smoothCamera.lookAt = defaultLookAt;
 
 
             // Handling the speed of camera returning to the player
             if (smoothCamera.smoothSpeed > 0.025f)
             {
                 smoothCamera.smoothSpeed = smoothCamera.smoothSpeed - 0.01f * Time.deltaTime;
             }
             if (smoothCamera.smoothSpeed < 0.025)
             {
                 smoothCamera.smoothSpeed = smoothCamera.smoothSpeed + 0.01f * Time.deltaTime;
             }
 
 
             // Handling the camerasize while returning to the player
             if (smoothCamera.GetComponentInChildren<Camera>().orthographicSize > smoothCamera.wantedCameraSize)
             {
                 smoothCamera.GetComponentInChildren<Camera>().orthographicSize = smoothCamera.GetComponentInChildren<Camera>().orthographicSize - 1 * Time.deltaTime;
             }
             if (smoothCamera.GetComponentInChildren<Camera>().orthographicSize < smoothCamera.wantedCameraSize)
             {
                 smoothCamera.GetComponentInChildren<Camera>().orthographicSize = smoothCamera.GetComponentInChildren<Camera>().orthographicSize + 1 * Time.deltaTime;
             }
         }
     }
 
     IEnumerator FreezeMovement()
     {
         movingCameraToTarget = true;
         RB2D.constraints = RigidbodyConstraints2D.FreezeAll;
         yield return new WaitForSeconds(freezeTime);
         RB2D.constraints = RigidbodyConstraints2D.FreezeRotation;
         movingCameraToTarget = false;
 
         if (allowFreezeMovement == true)
         {
             yield return freezeMovement = true;
         }
         if (useOnlyOneTime == true)
         {
             oneTimeUsed = true;
             movingCameraToTarget = false;
         }
     }
 }


Below is camera controller which is also needed. 1. Make empty gameobject and insert the code below to it. 2. Add your MainCamera as a child of it and then it should work.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SmoothCamera : MonoBehaviour
 {
     Camera cam;
     public Transform lookAt;
     public float wantedCameraSize = 10f;
     public float smoothSpeed;
     public bool smooth = true;
     private Vector3 offset = new Vector3(0, 0, -8.0f);
 
 
     void Start()
     {
         cam = GetComponentInChildren<Camera>();
 
         cam.orthographicSize = wantedCameraSize;
     }
 
     void LateUpdate()
     {
         Debug.Log("Currently looking at: " + lookAt); // Testing what we are looking at...
 
         Vector3 desiredPosition = lookAt.transform.position + offset;       
 
         if (smooth)
         {
             transform.position = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
         }
         else
         {
             transform.position = desiredPosition;
         }
     }
 }

Hopefully someone got time to help me out. Thanks in advance.

Comment
Add comment · Show 1
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 Timze · Jul 24, 2017 at 08:39 AM 0
Share

Ok, I found out that the script keeps checking all those "movingCameraToTarget " booleans and for this reason always stays false and true same time, except when you enter the original one. Then it works for some reason like it should... Disabling the rest of lookAtObjects makes others work just fine. Still no idea of fix thought...

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Timze · Jul 30, 2017 at 12:22 PM

I ended up doing a workaround by having only one "lookAtObject" active at a time. works fine now... Quess I accept my own answer then. :D

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

181 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

Related Questions

Cannot delay player respawn. 2 Answers

2D box collider always detecting player,2D Box colliders detecting player when he is not there 1 Answer

My character won't stop crouching, they dont stop crouchin 2 Answers

2D movement with gravity shifting 0 Answers

OnTriggerEnter getting called more than once 0 Answers


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