Why is my character flying off screen?
Hi, everyone,
I have been working on a 2D platformer, and everything has been going fine for weeks, including my character's movement. However, after working in Unity for a few hours today my character started flying off screen (in the negative X and Y directions) upon pressing play. I did not change any code, and I think the only 2 things I did before everything was working fine and this bug appearing were:
Creating a new prefab for a spike that would do damage to my character.
Accidentally deleting my character from the hierarchy.
I thought these could be the issues causing my character to fly off screen, but I simply undid these actions (ctrl+z) until I had reversed both actions back to the state in which everything in my game was working fine. Does anybody know why this would occur? I have checked my character for any rogue colliders or objects, but none exist. I should also mention that this bug occurs no matter where I place my character (in the air, inside my ground blocks, etc.) before clicking play. Here is the movement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player_1_controller : MonoBehaviour {
//movement variables
public float maxSpeed;
//jumping variables
bool grounded = false;
float groundCheckRadius = 0.2f;
public LayerMask groundLayer;
public Transform groundCheck;
public float jumpHeight;
Rigidbody2D myRB;
Animator myAnim;
bool facingRight;
//for shooting arrow
bool firing = true;
public Transform arrowTip;
public GameObject arrow;
public float fireRate = 0.5f;
public float nextFire = 0f;
//for being alert or in combat NEED DAMAGE TUTORIAL
// Use this for initialization
void Start () {
myRB = GetComponent<Rigidbody2D> ();
myAnim = GetComponent<Animator> ();
facingRight = true;
}
// Update is called once per frame
void Update () {
if (grounded && Input.GetButtonDown ("Jump")) {
grounded = false;
myAnim.SetBool ("isGrounded", grounded);
myRB.AddForce (new Vector2 (0, jumpHeight));
}
//player shooting
if (grounded && Input.GetButtonDown ("Fire1")) {
firing = true;
fireArrow ();
myAnim.SetBool ("arrowShoot", firing);
}
//player in combat or alert NEED TO GET TO DAMAGE TUTORIAL TO DO THIS
}
void FixedUpdate() {
//check if we are grounded - if no, then we are falling
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
myAnim.SetBool ("isGrounded", grounded);
myAnim.SetFloat ("verticalSpeed", myRB.velocity.y);
float move = Input.GetAxis ("Horizontal");
myAnim.SetFloat ("speed", Mathf.Abs (move));
myRB.velocity = new Vector2 (move * maxSpeed, myRB.velocity.y);
if (move > 0 && !facingRight) {
flip ();
} else if (move < 0 && facingRight) {
flip ();
}
}
void flip () {
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void fireArrow() {
if(Time.time > nextFire){
nextFire = Time.time + fireRate;
if (facingRight) {
Instantiate (arrow, arrowTip.position, Quaternion.Euler (new Vector3 (0, 0, 0)));
} else if (!facingRight) {
Instantiate (arrow, arrowTip.position, Quaternion.Euler (new Vector3 (0, 0, 180)));
}
}
}
}