I have a test ad setup in my unity game. I am calling the IntAd function from the GameManager Script after the game is over. But the problem is when I click the close button in the ad, the ad reappears again and again. Please help me how can stop IntAd function after showing the ad.
public class AdsManager : MonoBehaviour
{
public static AdsManager instance;
private string playStoreID = "000000";
private string intAd = "video";
private string rewardedAd = "rewardedVideo";
private string bannerAd = "bannerAd";
public bool isTargrtPlayStore;
public bool isTestAd;
public static int start;
void Awake()
{
DontDestroyOnLoad(this.gameObject);
if (instance == null)
{
instance = this;
}
else
{
Destroy(this.gameObject);
}
}
private void Start()
{
InitializeAdvertisement();
}
private void InitializeAdvertisement()
{
if (isTargrtPlayStore)
{
Advertisement.Initialize(playStoreID, isTestAd);
return;
}
}
public void IntAd()
{
if (Advertisement.IsReady(intAd))
{
Advertisement.Show(intAd);
}
}
}
Here is GameManager Script-
public class GameManager : MonoBehaviour
{
public static GameManager instance;
public bool gameOver;
void Awake()
{
if (instance == null){
instance = this;
}
}
// Start is called before the first frame update
void Start()
{
gameOver = false;
}
// Update is called once per frame
void Update()
{
}
public void StartGame()
{
UiManager.instance.GameStart();
UiManager.instance.StartScore1();
ScoreManager.instance.startScore();
GameObject.Find("PlatformSpawner").GetComponent<PlatformSpawner>().StartSpawningPlatforms();
//AdsManager.instance.BannerAd();
}
public void GameOver()
{
UiManager.instance.GameOver();
ScoreManager.instance.stopScore();
gameOver = true;
AdsManager.instance.IntAd();
}
}
code to call GameOver function
void Update()
{
if (!started)
{
if (Input.GetMouseButtonDown(0))
{
rb.velocity = new Vector3(speed, 0, 0);
started = true;
GameManager.instance.StartGame();
}
}
if (!Physics.Raycast(transform.position, Vector3.down, 1f))
{
gameOver = true;
rb.velocity = new Vector3(0, -25f, 0);
Camera.main.GetComponent<CameraFollow>().gameOver = true;
GameManager.instance.GameOver();
}
The code you have calling GameManager.GameOver() is inside of Update() so it's going to get called every frame, and if !Physics.Raycast(transform.position, Vector3.down, 1f) is always true when the game is over (and not just true once), then it'll end up calling GameManager.GameOver() every frame which lines up with the behaviour that you're seeing.
You could resolve this by adding !gameOver to your if statement, that way it'll only go into the if statement once at the end of your game, and then upon gamestart you could flip gameOver back to false.
if (!Physics.Raycast(transform.position, Vector3.down, 1f) && !gameOver)
{
gameOver = true;
rb.velocity = new Vector3(0, -25f, 0);
Camera.main.GetComponent<CameraFollow>().gameOver = true;
GameManager.instance.GameOver();
}
Just do an if statement with a bool named GameEnd. Set it to false. Play the ad only if GameEnd is false. Rite after the ad instantiates, set GameEnd to true.
if (!Physics.Raycast(transform.position, Vector3.down, 1f))
{
bool GameEnd = false;
gameOver = true;
rb.velocity = new Vector3(0, -25f, 0);
Camera.main.GetComponent<CameraFollow>().gameOver = true;
if(! GameEnd)
{
GameManager.instance.GameOver();
GameEnd = true;
}
}
Related
I am following one of the Udemy tutorials where it shows how to increase score after each second.
I have exactly done whatever he showed and I checked the code 10 times and the codes are the same but the score is not increasing.
Here is my GameManager.cs code:
public class GameManager : MonoBehaviour
{
public static GameManager instance;
public GameObject PlatformSpawner;
public bool gameStarted;
public bool gameEnd;
// Start is called before the first frame update
public Text scoreText;
int score = 0;
void Awake()
{
if (instance == null)
{
instance = this;
}
}
void Start()
{
}
// Update is called once per frame
void Update()
{
if(!gameStarted)
{
if (Input.GetMouseButtonDown(0))
{
GameStart();
}
}
}
public void GameStart()
{
gameStarted = true;
PlatformSpawner.SetActive(true);
}
public void GameOver()
{
gameStarted = false;
gameEnd = true;
PlatformSpawner.SetActive(false);
}
IEnumerator UpdateScore()
{
while (true)
{
yield return new WaitForSeconds(1f);
score += 1;
scoreText.text = score.ToString();
}
}
}
And here is the photo of unity where I have added scoreText in GameManager
When creating the game, I came across the problem that after adding animation to the player's code in the play mode, he stopped moving.
I did everything as usual, went into the animator, made an animation, and then set up and added everything necessary to the code in the animator, but after that the player stopped moving although before that everything was fine.
Help me, I hope it's not difficult.
Here is the code
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[Header("Controlled player")]
public Rigidbody2D rb;
[Header("Tinctures of movement")]
public float PSpeed = 1f;
public float jumpForce = 1f;
[Header("Variable Reduction parameters")]
public float LowingPSPeed;
public float FormedPSpeed;
[Header("Traffic Statuses")]
public bool Move;
public bool Jump;
[Header("")]
[Header("Checking the ground under the player")]
public bool OnGround;
private bool doJump = false;
private bool GOright = false;
private bool GOleft = false;
[Header("Ground Check Settings")]
private float groundRadius = 0.3f;
public Transform groundCheck;
public LayerMask groundMask;
private Animator anim;
void Math()
{
FormedPSpeed = PSpeed / LowingPSPeed;
}
void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
Math();
if (Input.GetKeyDown(KeyCode.Space))
{
doJump = true;
}
if (Input.GetKey("d"))
{
GOright = true;
}else
{
GOright = false;
}
if (Input.GetKey("a"))
{
GOleft = true;
}
else
{
GOleft = false;
}
}
void FixedUpdate()
{
OnGround = Physics2D.OverlapCircle(groundCheck.position, groundRadius, groundMask);
if (GOright && Move)
{
transform.position += new Vector3(FormedPSpeed, 0, 0);
GetComponent<SpriteRenderer>().flipX = false;
}
if (GOleft && Move)
{
transform.position += new Vector3(-(FormedPSpeed), 0, 0);
GetComponent<SpriteRenderer>().flipX = true;
}
if (doJump && Jump && OnGround)
{
rb.AddForce(new Vector2(0, (jumpForce * 10)));
anim.SetTrigger("takeOf");
doJump = false;
}
if (OnGround)
{
anim.SetBool("isJump", false);
}else
{
anim.SetBool("isJump", true);
}
if (!GOleft && !GOright)
{
anim.SetBool("isRun", false);
}else
{
anim.SetBool("isRun", true);
}
}
I don't understand why this is happening
i made 2d character and 3 ui buttons and they worked well
but the problem is when moving to the right or left by the ui buttons i can't jump however when jump from the ui button i can move to right and left
this is the script
public class PlayerWalk : MonoBehaviour {
private PlayerAnimation playerAnim;
private Rigidbody2D myBody;
private SpriteRenderer spriteRenderer;
public float speed = 7f;
public float jumpForce = 7f;
private bool moveLeft; // determine if we move left or right
private bool dontMove; // determine if we are moving or not
private bool canJump; // we will test if we can jump
void Start () {
playerAnim = GetComponent<PlayerAnimation>();
myBody = GetComponent<Rigidbody2D>();
dontMove = true;
}
void Update () {
//DetectInput();
HandleMoving();
}
void HandleMoving() {
if (dontMove) {
StopMoving();
} else {
if (moveLeft) {
MoveLeft();
} else if (!moveLeft) {
MoveRight();
}
}
} // handle moving
public void AllowMovement(bool movement) {
dontMove = false;
moveLeft = movement;
}
public void DontAllowMovement() {
dontMove = true;
}
public void Jump() {
if(canJump) {
myBody.velocity = new Vector2(myBody.velocity.x, jumpForce);
//myBody.AddForce(Vector2.right * jumpForce);
}
}
// PREVIOUS FUNCTIONS
public void MoveLeft() {
myBody.velocity = new Vector2(-speed, myBody.velocity.y);
playerAnim.ZombieWalk(true, true);
}
public void MoveRight() {
myBody.velocity = new Vector2(speed, myBody.velocity.y);
playerAnim.ZombieWalk(true, false);
}
public void StopMoving() {
playerAnim.ZombieStop();
myBody.velocity = new Vector2(0f, myBody.velocity.y);
}
void DetectInput() {
float x = Input.GetAxisRaw("Horizontal");
if (x > 0)
{
MoveRight();
}
else if (x < 0)
{
MoveLeft();
}
else
{
StopMoving();
}
}
void OnCollisionEnter2D(Collision2D collision) {
if(collision.gameObject.tag == "Ground") {
canJump = true;
}
}
void OnCollisionExit2D(Collision2D collision) {
if (collision.gameObject.tag == "Ground") {
canJump = false;
}
}
} // class
the 2d character moves well and there is no bugs or problems with scripts
Any help??
I don't know where is the problem??
**Note ** i used unity5.6
I would say onTriggerEnter2D instead of using onCollisionEnter2D would be a better option in this scenario. You can read more about that here.
https://answers.unity.com/questions/875770/ontriggerenter-or-oncollisionenter-1.html#:~:text=OnCollisionEnter%20is%20called%20when%20two,with%20%22IsTrigger%22%20set).
Did you try to debug the value of canJump while you are trying to move left or right?
I use a long press button, which works very well, on MouseDown and if I don't move the mouse, at the end of my delay, I have my action.
If I move the mouse (still pressing down and still on the button) it resets my button's delay, and I don't understand why.
if anyone has an idea, it would help me a lot.
Thx
Here's my code :
private bool _PointerDown;
public float DelaiReponse;
private float _PointerDownTimer;
private bool _IsValidate = false;
private float _Delai;
public void OnPointerDown(PointerEventData eventData)
{
_PointerDown = true;
}
public void OnPointerUp(PointerEventData eventData)
{
_PointerDown = false;
_PointerDownTimer = 0;
}
void Start()
{
_Delai = DelaiReponse;
}
private void Update()
{
if (_PointerDown) // Timer Button Validation Hold
{
_PointerDownTimer += Time.deltaTime;
if (_PointerDownTimer >= _Delai)
{
_IsValidate = true;
}
}
}enter code here
Using Input.GetMouseDown(0) and Input.GetMouseButtonUp(0) could fix the problem:
private void Update()
{
if (Input.GetMouseDown(0))
{
_PointerDown = true;
}
if (Input.GetMouseUp(0))
{
_PointerDown = false;
_PointerDownTimer = 0;
}
if (_PointerDown) // Timer Button Validation Hold
{
:
:
}
:
:
}
I finally found
OnMouseDown even worked outside my button, and I preferred the OnPointerDown method.
I added IDragHandler to my class, and create a bool _PointerDrag
private bool _PointerDown;
private bool _PointerDrag;
public float DelaiReponse;
private float _PointerDownTimer;
private bool _IsValidate = false;
private float _Delai;
public class LongClickButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
{
public void OnPointerDown(PointerEventData eventData)
{
_PointerDown = true;
}
public void OnPointerUp(PointerEventData eventData)
{
_PointerDown = false;
_PointerDownTimer = 0;
}
public void OnDrag(PointerEventData eventData)
{
_PointerDrag = true;
}
void Start()
{
_Delai = DelaiReponse;
}
private void Update()
{
if (_PointerDown || _PointerDrag) // Timer Button Validation Hold
{
_PointerDownTimer += Time.deltaTime;
if (_PointerDownTimer >= _Delai)
{
_IsValidate = true;
}
}
}
}
and it works.
Thanks for your time, Frenchy
The value of my HP bar drops when my runner collides an obstacle. Of course, Value of HP drop nomally, but Image of HP bar does not change. Why do I get an error?
public class CsRunner : MonoBehaviour
{
public Vector2 jumpVelocity;
public float _hp = 100f;
public float _curHP;
bool isJump;
public Image _hpValue;
bool collision_box;
// Use this for initialization
void Start()
{
_hpValue = GameObject.Find("HPbar").GetComponent<Image>();
_curHP = _hp;
}
// Update is called once per frame
void Update()
{
_hpValue.fillAmount = _curHP / _hp;
if (Input.GetKeyDown(KeyCode.Space) && isJump)
{
isJump = false;
transform.GetComponent<Rigidbody2D>().AddForce(jumpVelocity/2, ForceMode2D.Impulse);
}
if ((Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) && collision_box)
{
isJump = true;
transform.GetComponent<Rigidbody2D>().AddForce(jumpVelocity, ForceMode2D.Impulse);
}
else
{
GetComponent<Animator>().SetTrigger("Run");
}
}
void OnTriggerEnter2D(Collider2D coll)
{
if (coll.transform.tag == "Enemy")
{
_curHP--;
_hpValue.fillAmount = _curHP / _hp;
}
}
}
Here is a screenshot of my scene :
Thank you !