Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Dalsia · Jul 01, 2017 at 12:19 AM · movementcharactercontrollerbug-perhapscharacter movement

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:

  1. Creating a new prefab for a spike that would do damage to my character.

  2. 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)));
             }
 
     }
  }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

141 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Issue using WASD controls in FPS microgame template 0 Answers

Help converting a character controller script to rigidbody 1 Answer

Flipping MeshRenderer upon direction change 2 Answers

Error CS1061 - Error CS0103 - Error CS0165 0 Answers

Using Velocity breaks my jumping 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges