Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Floating Issue? and other control issues

Discussion in 'Scripting' started by abstractfragment, Feb 27, 2014.

  1. abstractfragment

    abstractfragment

    Joined:
    Jan 16, 2014
    Posts:
    15
    So I have this issue in my game where when the character jumps they can essentially float around mid-air. I don't know how to fix this. I also have an error where no matter what the players speed is always greater than zero (even when standing still) after moving. What did I screw up.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class playercontroller : MonoBehaviour
    5. {
    6.     public float maxspeed = 10f;
    7.     bool facingRight = true;
    8.     Animator anim;
    9.     bool grounded = false;
    10.     public Transform groundCheck;
    11.     float groundRadius = 0.2f;
    12.     public LayerMask whatIsGround;
    13.     public float jumpForce = 100;
    14.     public float sprintAbility = 500;
    15.     public bool isSprinting = false;
    16.  
    17.  
    18.     // Use this for initialization
    19.     void Start ()
    20.     {
    21.         anim = GetComponent<Animator> ();
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void FixedUpdate () {
    26.         grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
    27.         anim.SetBool ("Ground", grounded);
    28.         anim.SetBool ("Sprinting", isSprinting);
    29.         anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
    30.         float move = Input.GetAxis ("Horizontal");
    31.  
    32.         if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.LeftArrow))   {
    33.             isSprinting = false;
    34.                 anim.SetFloat ("Speed", Mathf.Abs (move));
    35.                 rigidbody2D.AddForce(new Vector2(move * maxspeed, 0));
    36.                 }
    37.         else if (Input.GetKey(KeyCode.RightArrow)  Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.LeftArrow)  Input.GetKey(KeyCode.LeftShift))
    38.             {
    39.             isSprinting = true;
    40.             anim.SetFloat ("Speed", Mathf.Abs (move * sprintAbility));
    41.             rigidbody2D.AddForce(new Vector2((move * (sprintAbility * maxspeed)), 0));
    42.         }
    43.  
    44.  
    45.             if(move > 0 !facingRight)
    46.             flip ();
    47.         else if (move < 0  facingRight)
    48.             flip ();
    49.         isSprinting = false;
    50.        
    51.     }
    52.     void Update()
    53.     {
    54.         if (grounded  Input.GetKeyDown (KeyCode.Space)) {
    55.             anim.SetBool("Ground", false);
    56.                 rigidbody2D.AddForce(new Vector2(0, jumpForce));
    57.         }
    58.         }
    59.    
    60.     void flip()
    61.     {
    62.         facingRight = !facingRight;
    63.         Vector3 theScale = transform.localScale;
    64.         theScale.x *= -1;
    65.         transform.localScale = theScale;
    66.     }
    67.  
    68.  
    69. }