I have a problem. In my C# script I have a variable named "topTouched", that is false by default. It becomes true if a y position is reached. the problem is : In an if, one condition is to "topTouched" have to be true. But when I print a Log with that variable. It shows false. I don't understand. Thanx for your help. :3
Here is the problem :
if (-5f > Mace.transform.position.y || topTouched == true)
{
print("hey1" + topTouched);
rb.AddForce(new Vector2(0f, -10f));
}
Complete code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Moves : MonoBehaviour
{
public Rigidbody2D rb;
private GameObject Mace;
private bool topTouched;
void Start()
{
Mace = this.gameObject;
}
void Update()
{
if(Mace.transform.position.y == -7.5f)
{
topTouched = true;
}
if (Mace.transform.position.y == 3.3f)
{
topTouched = false;
}
if (Mace.transform.position.y != 3.4f || Mace.transform.position.y != -7.6f)
{
if( -5f > Mace.transform.position.y || topTouched == false)
{
print("hey_" + topTouched);
rb.AddForce(new Vector2(0f, 10f));
}
if (-5f > Mace.transform.position.y || topTouched == true)
{
print("hey1" + topTouched);
rb.AddForce(new Vector2(0f, -10f));
}
if (-5f < Mace.transform.position.y || topTouched == true)
{
print("hey2" + topTouched);
rb.AddForce(new Vector2(0f, -10f));
}
if (-5f < Mace.transform.position.y || topTouched == false)
{
print("hey3" + topTouched);
rb.AddForce(new Vector2(0f, 10f));
}
}
}
}
In unity console, it shows :
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…