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 zachmartinez · Feb 24, 2016 at 04:51 AM · 2d-physicsglitchplay

Selecting the Character Makes Him Run Faster?

I'm making a 2D game where I have the player character with a set acceleration and maximum jump height. However it's not always consistent when I playtest the game. If I select the player character in the inspector when the game is running, he'll for some reason accelerate faster and jump higher. I've also had a problem where he'll randomly fidget around instead of running forward, and I had to restart Unity to fix it. Why is this happening? If it helps, here's my entire code for the character.

 using UnityEngine;
 using System.Collections;
 
 public class SonicControllerScript : MonoBehaviour
 {
     public float jumpAxis;
     public bool grounded;
     public Vector2 currentSpeed;
     public float speedCap = 15f;
     public float acceleration = 0.45f;
     bool facingRight = true;
     Animator anim;
     public Vector2 localVelocity; //Stores the relative velocity to be converted to world velocity
 //    private Vector2 jumpVelocity; //Copies the localVelocity.y during a jump to prevent Sonic from curving jumps.
     public float hSpeed;
     private bool stationary;
     [SerializeField] private LayerMask whatIsGround;
     public float groundedRadius = 0.2f;
     public float readjustRotatonSpeed = 5f;
     [SerializeField] private bool jumping;
     public float jumpSpeed; //The force at which Sonic jumps.
     public float jumpMin; //The minumum height Sonic can jump.
     public float jumpMax; //The maximum height Sonic can jump.
     public float jumpMinAdjust;
     public float jumpMaxAdjust;
     public float gravity;
     public float resistance;
     [SerializeField]private bool holdingJump;
 
 
 
     void Awake()
     {
         //Setting the Transform variables groundCheck and ceilingCheck to match the cooresponding game objects.
     }
 
     // Use this for initialization
     void Start()
     {
         anim = GetComponent<Animator>();
     }
 
     void OnCollisionEnter2D(Collision2D col)
     {
 //        if (gameObject.GetComponentInChildren<GroundCheckScript> ().isHitting == false//Only triggers if Sonic is both touching the ground and detecting it with his ray.
 //            && col.gameObject.layer == 0)
 //        {
 //            Quaternion tempRotation = new Quaternion();
 //            tempRotation = Quaternion.Euler (0, 0, 0);
 //            gameObject.GetComponent<RectTransform>().rotation = tempRotation;
 //            jumpMax = jumpMaxAdjust;
 //            jumpMin = jumpMinAdjust;
 //            jumping = false;
 //            holdingJump = false;
 //        } 
     }
 
     void OnCollisionStay2D(Collision2D col)
     {
         
     }
 
     void Update()
     {
         jumpAxis = Input.GetAxis ("Jump");
         hSpeed = localVelocity.x;
         localVelocity = transform.InverseTransformDirection(gameObject.GetComponent<Rigidbody2D>().velocity);
 
         //Programming Sonic's world velocity to adhere to his local velocity when grounded.
         if (grounded == true)
         {
             GetComponent<Rigidbody2D> ().velocity = transform.TransformDirection (localVelocity);
         }
         //Setting the speed of Sonic's animation to match his hspeed.
         //gameObject.GetComponent<Rigidbody2D>().velocity.x;
         //Setting a condition so that the animation will play only when Sonic is moving.
         if (hSpeed != 0
             && grounded == true) {
             anim.speed = Mathf.Abs (hSpeed / 4);
             if (anim.speed < .5f) {
                 anim.speed = .5f;
             }
         }
 
         else if (jumping == true)
         {
             anim.speed = 1f;
         }
         //Programming Sonic's jumping button detection.
         if (Input.GetButtonDown ("Jump") == true
             && grounded == true)
         {
             jumping = true;
             holdingJump = true;
             Debug.Log ("Jump has been pressed.");
             AudioSource.PlayClipAtPoint (gameObject.GetComponent<AudioSource>().clip, gameObject.GetComponent<RectTransform>().position);
         }
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         if (jumping == false) 
         {
             gameObject.GetComponent<BoxCollider2D> ().enabled = true;
         }
         //Logging Sonic's current speed
         currentSpeed = GetComponent<Rigidbody2D>().velocity;
         GetComponent<Rigidbody2D> ().velocity = transform.TransformDirection (localVelocity);
 
 
         //The initial jump...
         if (jumpMin > 0
             && jumping == true)
         {
             localVelocity.y = jumpSpeed;
 //            jumpVelocity = gameObject.GetComponent<Rigidbody2D>().velocity;
 //            jumpVelocity.x = 0;
 //            GetComponent<Rigidbody2D> ().velocity = transform.TransformDirection (localVelocity);
 //            gameObject.GetComponent<Rigidbody2D> ().velocity = gameObject.GetComponent<Rigidbody2D> ().velocity + localVelocityUnglobalized;
             jumpMin = jumpMin - 1;
         }
 
         //The continued press of the jump button to extend the jump.
         if (Input.GetAxis ("Jump") > 0.1
             && jumpMax > 0
             && jumpMin <= 0
             && jumping == true
             && holdingJump == true)
         {
 //            gameObject.GetComponent<Rigidbody2D> ().AddRelativeForce (new Vector2 (0, jumpSpeed), ForceMode2D.Force);
             localVelocity.y = jumpSpeed;
 //            gameObject.GetComponent<Rigidbody2D> ().velocity += jumpVelocity;
 //            gameObject.GetComponent<Rigidbody2D> ().velocity = gameObject.GetComponent<Rigidbody2D> ().velocity + localVelocityUnglobalized;
             jumpMax = jumpMax - 1;
             gameObject.GetComponent<BoxCollider2D> ().enabled = false;
         }
 
         //Detect when the player lets go of the jump button mid-jump.
         if (Input.GetAxis ("Jump") == 0
             && jumping == true)
         {
             holdingJump = false;
         }
 
         if (gameObject.GetComponentInChildren<GroundCheckScript>().isHitting == true)
         {
             grounded = true;
         }
 
         else if (gameObject.GetComponentInChildren<GroundCheckScript>().isHitting == false)
         {
             grounded = false;
         }
 
         //Reset Sonic's grounding only if he's already jumped.
         if (gameObject.GetComponentInChildren<GroundCheckScript> ().isHitting == true
             && localVelocity.y < jumpSpeed)
         {
             jumpMax = jumpMaxAdjust;
             jumpMin = jumpMinAdjust;
             jumping = false;
             holdingJump = false;
             gameObject.GetComponent<AudioSource>().Stop();
         }
 
         //Creating the float variable "move"
         float move = Input.GetAxis("Horizontal");
 
         //Code for animation begins here...
 
         anim.SetFloat("Speed", Mathf.Abs(hSpeed));
         anim.SetBool("Ground", grounded);
         anim.SetBool("Jumping", jumping);
 
         //Code for animation ends here.
 
 
 
         //Code for movement begins here...
 
 
 
         //Programming Sonic to move left and right depending on key input
         if (Input.GetAxis("Horizontal") > .1
             && hSpeed <= speedCap)
         {
 //            GetComponent<Rigidbody2D> ().velocity = transform.TransformDirection (localVelocity);
             if (hSpeed <= speedCap)
             {
                 localVelocity.x = localVelocity.x + acceleration;
             }
         }
         else if (Input.GetAxis("Horizontal") < -.1)
         {
 //            GetComponent<Rigidbody2D> ().velocity = transform.TransformDirection (localVelocity);
             if (hSpeed >= (speedCap * -1))
             {
                 localVelocity.x = localVelocity.x - acceleration;
             }
         }
             
 
         //Programming Sonic to change his rotation and assume downward force when in the air.
         if (grounded == false) 
         {
             gameObject.GetComponent<ConstantForce2D> ().relativeForce = new Vector2 (0, 0);
             gameObject.GetComponent<ConstantForce2D> ().force = new Vector2 (0, gravity);
             //Only reset Sonic's rotation if he isn't jumping.
             if (jumpMin <= 0
                 && holdingJump == false
                 || jumpMax <= 0)
             {
                 //Reset Sonic's rotation.
                 if (transform.rotation.eulerAngles.z > 0
                     && transform.rotation.eulerAngles.z < 180) 
                 {
                     transform.Rotate (0, 0, -readjustRotatonSpeed);
                 }
                 if (transform.rotation.eulerAngles.z > 180) 
                 {
                     transform.Rotate (0, 0, readjustRotatonSpeed);
                 }
             }
         }
         else if (grounded == true)
         {
             gameObject.GetComponent<ConstantForce2D> ().relativeForce = new Vector2 (0, gravity);
             gameObject.GetComponent<ConstantForce2D> ().force = new Vector2 (0, 0);
         }
 
         //Programming in the code to make Sonic turn around if the player presses the other direction.
 
         if (move > 0 &&!facingRight)
             Flip();
         else if (move < 0 && facingRight)
             Flip();
 
         //Raycasting...
 //        Ray groundAlign = new Ray(transform.position,Vector3.down);
 //        Physics2D.Raycast(groundCheck, new Vector2(0, groundCheck.localRotation.z), 1f, 0);
     }
         
 
     void Flip()
     {
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 
     void Resistance()
     {
         
     }
 }
 
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

47 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

Related Questions

Reload Assemblies making enterplaymode take long 4 Answers

Game Builds and Runs on Android but doesn't work on Play Store? 0 Answers

Starting a scene without pressing the play 0 Answers

Graphical Bug In Game Tab 1 Answer

What could I do with this kind of glitches? 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