Challenges
- Keshav Batra
- May 19, 2022
- 3 min read
I've officially finished the challenge for slowing down time at the push of a button. It took me a few days, but with a bit of patience I was able to get it working properly! Below is all the code and videos for the movement, pickup, and slow down function. I have also been participating in a game jam by programming the player to pickup an object every time a button is pressed. To prepare for that I had to practice doing collision scripts in the project. Also below is the code I have for the practice script.
Player Movement
public class PlayerMovement : MonoBehaviour
{
public GameObject Cube;
public float speed = 6;
public CharacterController Movement;
[SerializeField] private Vector3 moveDirection = Vector3.zero;
public float gravity=-12f;
public float JumpHeight =3f;
Vector3 velocity;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
bool isGrounded;
public TimeManager timeManager;
public bool HasPickup = false;
// Start is called before the first frame update
void Start()
{
Movement = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask); //if the player collides with anything in the ground mask, isGrounded will be true. Otherwise it'll be false
print(isGrounded);
if (isGrounded && velocity.y < 0)
{
velocity.y = -3f; //Resets the velocity
}
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Vertical"); //Setting the value based on the input given
Vector3 go = transform.right * x + transform.forward * y; //This is the direction in which the player is going to move based on the x and y movement
Movement.Move(go * speed * Time.deltaTime); //Allows the player to move
velocity.y += gravity * Time.deltaTime; //Controls the velocity and gravity of the player
Movement.Move(velocity * Time.deltaTime); //Moves based on velocity
if(Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(JumpHeight * -3 * gravity);
}
Sprint(); //Call it by its name and then use parentheses and a semi colon and put it on whichever function requires it
Jump();
SlowDown();
}
void Sprint()
{
if (Input.GetKey(KeyCode.LeftShift))
{
speed = 12;
print("Speed 12");
}
else
{
speed = 6;
}
}
void Jump()
{
if (Input.GetKey(KeyCode.Space))
{
}
}
void SlowDown()
{
if (Input.GetKeyDown(KeyCode.P) && HasPickup) //If the player is pressing the button and has the pickup
{
timeManager.SlowDown(); //Sets the time to slow down
}
}
}
Pickup Script
public class Pickup : MonoBehaviour
{
public GameObject pickup;
public TimeManager timeManager;
public GameObject Player;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player")) //If the tag of the collider is Player, then the collectable method will be called
{
Player.GetComponent<PlayerMovement>().HasPickup = true;
Collectable();
}
}
void Collectable()
{
Destroy(gameObject); //The pickup will be destroyed whenever the player collides with it
}
//Instantiate(pickup, transform.position, transform.rotation); //This spawns the pickup
}
Slowdown Script
{
// Start is called before the first frame update
public float slowdownFactor = 1f;
public float slowdownLength = 2f;
void Update()
{
//Time.timeScale += (1f / slowdownLength) * Time.unscaledDeltaTime;
//Time.timeScale = Mathf.Clamp(Time.timeScale, 0f, 1f); //This will clamp the speed between 0 and 1
}
public void SlowDown()
{
Time.timeScale = 0.2f; //Controls how fast or slow the game runs
//Time.timeScale = slowdownFactor; // if Timescale is 1, time will pass at a normal speed and will run in realtime. If its 0.5 it will pass 2x slower than realtime
//Time.fixedDeltaTime = Time.timeScale * 0.2f;
}
public void NormalSpeed()
{
Time.timeScale = 2f;
}
}
Practice Script
private void OnTriggerEnter(Collider other)
{
if (PlayerSprite.CompareTag("Player"))
{
Debug.Log("Picked Up");
}
}
Comments