So I have this script (it’s long)
using UnityEngine; //referencing namespaces to use in the script
using System.Collections;
using UnityEngine.UI;
using System;
public class PlayerController : MonoBehaviour {
public float moveSpeed; //variable declaration
public float moveVelocity;
public float jumpHeight;
public bool isRolling;
public Sprite[] rollStates;
public SpriteRenderer sr;
public Sprite playerIdle;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool grounded;
public bool canMove;
public bool onLadder;
public Guns guns;
public float damage;
public float firerate;
public float bulletSpeed;
public float totalAmmo;
public float currentAmmo;
public Transform bulletSpawnPoint;
public GameObject bullet;
public float health;
public bool canTakeDamage;
public float currentHealth;
public float stamina;
public float currentStamina;
public int dexterity;
public int strength;
public int agility;
public int sorcery;
public int ramTotal;
public Text ramText;
public Text ammoText;
public RectTransform healthBar, staminaBar, healthBound, staminaBound, deadHealthBar, deadStaminaBar;
public GameObject deadScreen;
void Start() {
ramTotal = 0; //set the score/currency to 0
health = 100; //set health to 100
stamina = 100; //set stamina to 100
dexterity = 1; //set all base stats to 1
strength = 1;
agility = 1;
sorcery = 1;
ramText = GameObject.Find ("ramText").GetComponent<Text> (); //find the component that controls the score/currency
guns = GameObject.Find("currentGun").GetComponent<Guns>(); //find the Guns script on the current gun
bullet = GameObject.Find ("currentBullet"); //find the current bullet
bullet.transform.localScale = new Vector3 (0.05f, 0.05f, 1f); //set the scale of the bullets fired
isRolling = false; //the player isn't rolling by default
sr = GetComponent<SpriteRenderer> (); //get the sprite renderer on the player
currentHealth = health; //set current health to the total health
currentStamina = stamina; //same for stamina
canTakeDamage = true; //the player can be damaged by default
deadScreen = GameObject.Find("deadScreen");
deadScreen.SetActive (false);
}
void FixedUpdate() { //checking for player collision with the ground is resource consuming, so we don't call it every fixed frame
grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround); //this checks for ground collision
}
void Update() {
ammoText.text = Convert.ToString (currentAmmo + "/" + totalAmmo); //converts the int values to a string and displays it
ramText.text = Convert.ToString(ramTotal); //converts the int to a string and displays it in the textbox
healthBar.localScale = new Vector3(currentHealth / 200f, 0.35f, 1f); //sets the health bar scale
healthBar.localPosition = new Vector3 (-340.4f + (0.25f * (((currentHealth / 200f) - .5f) * 200f)), 467f, 0f); //and the position
deadHealthBar.localScale = new Vector3(health / 200f, 0.35f, 1f); //keep the black health bar the same as the health bar
deadHealthBar.localPosition = new Vector3 (-340.4f + (0.25f * (((health / 200f) - .5f) * 200f)), 467f, 0f); //but use the health stat instead of current health
staminaBar.localScale = new Vector3(currentStamina / 200f, 0.35f, 1f); //do the same for the stamina bar
staminaBar.localPosition = new Vector3 (-340.4f + (0.25f * (((currentStamina / 200f) - .5f) * 200f)), 426f, 0f);
deadStaminaBar.localScale = new Vector3(stamina / 200f, 0.35f, 1f);
deadStaminaBar.localPosition = new Vector3 (-340.4f + (0.25f * (((stamina / 200f) - .5f) * 200f)), 426f, 0f);
healthBound.localPosition = new Vector3 (-290.4f + (0.5f * (((health / 200f) - .5f) * 200f)), 467f, 0f); //move the right boundaries with the health bar as it expands
staminaBound.localPosition = new Vector3 (-290.4f + (0.5f * (((stamina / 200f) - .5f) * 200f)), 426f, 0f);
if (Input.GetKeyDown (KeyCode.O)) { //was 'R' pressed?
currentHealth += 10;
currentStamina += 10;
health += 10;
stamina += 10;
}
if (Input.GetKeyDown (KeyCode.R)) { //was 'R' pressed?
currentAmmo = totalAmmo; //if so, reload the current weapon
}
if (GameObject.Find ("blueNPC") == null) { //does blueNPC still exist (which means the cutscene is playing)
canMove = true; //if not, let the player move
}
if (Input.GetKeyDown (KeyCode.Space) && firerate != 0 && currentAmmo > 0 && canMove) { //has space been pressed, with a gun equipped, and ammo in the gun?
Instantiate (bullet, bulletSpawnPoint.position, bulletSpawnPoint.rotation); //if so, instantiate a new bullet at the bullet spawn
currentAmmo -= 1; //and decrement the gun ammo by 1
}
if (Input.GetKeyDown (KeyCode.W) && grounded && canMove && !onLadder) { //was 'W' pressed with the player on the ground?
Jump (); //if so, call the jump method
} else if (Input.GetKey (KeyCode.W) && canMove && onLadder) {
GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, moveVelocity);
} else if (Input.GetKey (KeyCode.D) && canMove && onLadder) {
GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, -moveVelocity);
}
if (Input.GetKeyDown (KeyCode.S) && grounded && !isRolling && currentStamina > 0 && canMove) { //was 'S' pressed with the player on the ground, and not already rolling?
StartCoroutine ("Roll"); //if so, call the Roll IEnumerator
currentStamina -= 25; //subtract 25 from the player's stamina
}
moveVelocity = 0f; //set the move velocity to 0
if (Input.GetKey (KeyCode.D) && canMove) { //was 'D' pressed?
moveVelocity = moveSpeed; //if so, change the velocity to the move speed
}
if (Input.GetKey (KeyCode.A) && canMove) { //was 'A' pressed?
moveVelocity = -moveSpeed; //if so, change the velocity to the opposite of the move speed
}
GetComponent<Rigidbody2D>().velocity = new Vector2(moveVelocity, GetComponent<Rigidbody2D>().velocity.y); //change the rigidbody every frame to update movement
if(GetComponent<Rigidbody2D>().velocity.x > 0) { //check if the velocity is positive
GetComponent<Transform>().localScale = new Vector3 (0.05f, 0.05f, 1f); //if so, make the player face right
} else if(GetComponent<Rigidbody2D>().velocity.x < 0) { //check if the velocity is negative
GetComponent<Transform>().localScale = new Vector3 (-0.05f, 0.05f, 1f); //if so, make the player face left
}
if (currentStamina <= stamina) { //is the player stamina 0 or sub-0?
} else if (currentStamina > stamina) { //is the stamina regenerated higher than the max value?
currentStamina = stamina; //if so, set it back to it's max value
}
}
public void Jump() { //new method to jump
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpHeight); //add a new vector upwards whenever the method is called
}
public IEnumerator Roll() {
isRolling = true; //once the method is called, set rolling to true
canTakeDamage = false; //set the rollHealth to currentHealth to reset it later
sr.sprite = rollStates [0]; //change the roll sprites over time
yield return new WaitForSeconds (0.05f);
sr.sprite = rollStates [1]; //continue the changes
yield return new WaitForSeconds (0.05f);
sr.sprite = rollStates [2];
yield return new WaitForSeconds (0.05f);
sr.sprite = rollStates [3];
yield return new WaitForSeconds (0.05f);
sr.sprite = playerIdle; //after the roll is over, return to the idle state
canTakeDamage = true; //and reset the health
isRolling = false; //rolling is no longer true after the roll
}
public IEnumerator InvincibleFrames() {
canTakeDamage = false; //the player cannot take damage
yield return new WaitForSeconds (1f); //wait for 1 second
canTakeDamage = true; //the player can now take damage
}
}
But the question I have concerns lines {107-129, and 144-146}
I want to change the vector acting on the player’s rigidbody at a steady speed only when on a ladder. I have this all working, except for the code inside the if-elseif statements checking for the ‘W’ keypress. I think that since I update the rigidbody on line 129 constantly, the code in the elseif statement never has time to execute, but I don’t know what to put in there in order to fix the problem. I want to move up a ladder at a steady speed. If you have any ideas, let me know. Thanks!