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.
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...
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