How to make a POWER UP that slows the player down with force.
I am new to unity and I'm kinda stuck when it comes to power ups. I saw brackeys video on creating power ups and tried to make my own but I want to know how to get a power up that slows the player down with force. The code I have used for the player is:
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
// This is a reference to the Rigidbody component called "rb"
public Rigidbody rb;
public float forwardForce = 2000f; // Variable that determines the forward force
public float sidewaysForce = 500f; // Variable that determines the sideways force
// We marked this as "Fixed"Update because we
// are using it to mess with physics.
void FixedUpdate()
{
// Add a forward force
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("d")) // If the player is pressing the "d" key
{
// Add a force to the right
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a")) // If the player is pressing the "a" key
{
// Add a force to the left
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("right")) // If the player is pressing the "right" key
{
// Add a force to the right
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("left")) // If the player is pressing the "left" key
{
// Add a force to the left
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (rb.position.y < -1f)
{
FindObjectOfType<GameManager>().EndGame();
}
}
}
I was wondering if it is possible to get a power up that puts force against the player for a certain amount of time then disables itself. I thought that force was a good way to do it because of the way the player moves but I could change the time? I have seen videos on people using something called Time.deltaTime and changing the timeScale? I'm not sure what that does so using force to change players speed made sense to me. the power up I currently have changes the size of the player so I got thinking and wondered if I could alter it slightly to change the players speed.. the code for the power up is:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PowerUp : MonoBehaviour { public float multiplier = 0.7f;
public GameObject pickupEffect;
void OnTriggerEnter (Collider other)
{
if (other.CompareTag("Player"))
{
Pickup(other);
}
}
void Pickup(Collider player)
{
Instantiate(pickupEffect, transform.position, transform.rotation);
player.transform.localScale *= multiplier;
Destroy(gameObject);
}
}
I would really appreciate it if someone could help me with the code. I'm really new to coding in general because I have only been using unity for around a month. thanks for reading :)
Your answer
Follow this Question
Related Questions
Collider disabling by itself... 2 Answers
2D Game powerup script not working at all 0 Answers
2D Power up script not working 0 Answers
2D Power up script not working 0 Answers
How can I separate my Code efficiently ? 0 Answers