player moves position when I hit play
Hi, so Im new to unity and was just finishing up my game for class when I ran into this problem where I set the position of the character to 0 for x and then once I hit play, the x position changes to 2.6 and I keep resetting it but it automatically goes back no matter if I reset it or not in edit mode, here are some images of what I have so far for my game, notice how in the transform before I press play the position is set to 0 and as soon as I press play it jumps to 2.6 where the character falls down out of screen never to be seen again. if anyone can figure this out it would be very much appreciated.
also here is the code for my character if that has anything to do with the issue.
public class PlayerBounds : MonoBehaviour {
public float min_X = -2.6f, max_X = 2.6f, min_Y = -5.6f;
private bool out_Of_Bounds;
void Update() {
CheckBounds();
}
void CheckBounds() {
Vector2 temp = transform.position;
if (temp.x > max_X)
temp.x = max_X;
if (temp.x < max_X)
temp.x = max_X;
transform.position = temp;
if(temp.y <= min_Y) {
if (!out_Of_Bounds) {
out_Of_Bounds = true;
//SoundManager.instance.DeathSound();
//GameManager.instance.RestartGame();
}
}
} //Check bounds
void OnTriggerEnter2D(Collider2D target) {
if(target.tag == "TopSpike") {
transform.position = new Vector2(1000f, 1000f);
//SoundManager.instance.DeathSound();
//GameManager.instance.RestartGame();
}
}
} //class
here is the player movement script as well public class PlayerMovement : MonoBehaviour {
private Rigidbody2D myBody;
public float moveSpeed = 2f;
// Start is called before the first frame update
void Awake()
{
myBody = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
Move();
}
void Move()
{
if (Input.GetAxisRaw("Horizontal") > 0f)
{
myBody.velocity = new Vector2(moveSpeed, myBody.velocity.y);
}
if (Input.GetAxisRaw("Horizontal") < 0f)
{
myBody.velocity = new Vector2(-moveSpeed, myBody.velocity.y);
}
}//move
public void PlatformMove(float x)
{
myBody.velocity = new Vector2(x, myBody.velocity.y);
}
}
Answer by Chris333 · Apr 05, 2019 at 02:28 PM
You are checking twice for the max_X but in the second if-statement it should be min_X instead of max_X.
if (temp.x < min_X)
temp.x = min_X;
Your answer
Follow this Question
Related Questions
Make object move back and forth 2 Answers
Every GameObject that has a rigidbody, won't stay at Z 0 and I don't know why 0 Answers
error CS0201 1 Answer