Hey, i have two scripts which maintain how my score will be multiplied in the game
using UnityEngine;
public class ObjectDetection : MonoBehaviour
{
public float raycastDistance = 1f;
public LayerMask obstacleLayer;
public score scoreScript;
void Start()
{
scoreScript = GameObject.FindGameObjectWithTag("score").GetComponent<score>();
}
void FixedUpdate()
{
// Get the direction in world space
Vector3 leftRayDirection = -Vector3.right;
Vector3 rightRayDirection = Vector3.right;
// Check both rays
bool obstacleOnLeft = CheckRay(leftRayDirection, Color.red);
bool obstacleOnRight = CheckRay(rightRayDirection, Color.green);
// Do something if there are obstacles on both sides
if (obstacleOnLeft && obstacleOnRight)
{
Debug.Log("Obstacle detected on both sides!");
scoreScript.ObstacleDetected();
}
else
{
scoreScript.NoObstacleDetected();
}
}
bool CheckRay(Vector3 rayDirection, Color debugColor)
{
// Cast a ray
RaycastHit hit;
if (Physics.Raycast(transform.position, rayDirection, out hit, raycastDistance, obstacleLayer))
{
Debug.Log("Something detected: " + hit.collider.name + " in direction: " + rayDirection);
// Do something if an object is detected
return true;
}
// Debug visualization
Debug.DrawRay(transform.position, rayDirection * raycastDistance, debugColor);
return false;
}
}
and the secound script is this one
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class score : MonoBehaviour
{
public AudioSource multiply;
public Animator anim;
public TextMeshProUGUI multiplierscore;
public TextMeshProUGUI scoree;
public float total;
public float less;
public float scoremultiplier = 1f;
public float multiplierduaration = 5f;
public float elapsedTimeWithoutObstacle = 0f;
public bool obstacleDetected = false;
private bool isCooldownActive = false;
public float obstacleScoreCooldown = 1.5f;
// Start is called before the first frame update
void Start()
{
multiply.Stop();
ResetMultiplier();
}
// Update is called once per frame
void Update()
{
total += Time.deltaTime;
less = (total/1)*scoremultiplier*11f;
scoree.text = less.ToString("0");
multiplierscore.text = "X"+scoremultiplier.ToString("0");
if (obstacleDetected && !isCooldownActive)
{
anim.SetTrigger("pop");
elapsedTimeWithoutObstacle = 0f;
scoremultiplier+=1;
multiply.Play();
isCooldownActive = true;
StartCoroutine(ObstacleCooldown());// Reset the timer
}
else if(!obstacleDetected)
{
elapsedTimeWithoutObstacle += Time.deltaTime;
// If a certain duration has passed without obstacles, reset the multiplier
if (elapsedTimeWithoutObstacle >= multiplierduaration)
{
ResetMultiplier();
}
}
if (less>PlayerPrefs.GetFloat("highscore",0))
{
PlayerPrefs.SetFloat("highscore",less);
}
}
public void ObstacleDetected()
{
obstacleDetected = true;
}
public void NoObstacleDetected()
{
obstacleDetected = false;
}
private void ResetMultiplier()
{
if (scoremultiplier>1f)
{
anim.SetTrigger("pop");
scoremultiplier = 1f;
}
elapsedTimeWithoutObstacle = 0f;
}
IEnumerator ObstacleCooldown()
{
yield return new WaitForSeconds(obstacleScoreCooldown);
isCooldownActive = false;
}
}
but the problem is that the score multiplier is not working android device but it is working in the unity editor, plese anybody help me.