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