Question by
KP0178 · Mar 26, 2021 at 01:25 AM ·
script.charactercontrollermovement scriptspeed
How do I transfer the momentum from my speed to the player?
I'm working on a 3D plat former and currently, while I'm running, the momentum from the extra speed is not being transferred to the player. Here is my script, can anybody help?
public class CubeController : MonoBehaviour {
public Rigidbody rb;
public bool cubeIsOnTheGround = true;
public float speed = 6f;
public ParticleSystem dust;
public AudioSource sound;
public AudioSource ring;
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
float horizontalMovement = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
float verticalMovement = Input.GetAxis("Vertical") * Time.deltaTime * speed;
transform.Translate(horizontalMovement, 0, verticalMovement);
if(Input.GetButtonDown("Jump") && cubeIsOnTheGround)
{
rb.AddForce(new Vector3(0, 5, 0), ForceMode.Impulse);
cubeIsOnTheGround = false;
JumpSound();
}
if(Input.GetButton("Sprint") && cubeIsOnTheGround)
speed = 10f;
else
speed = 6f;
if(Input.GetButton("Sprint"))
{
CreateDust();
}
if (Input.GetButtonDown("Sprint"))
{
SprintSound();
}
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Ground")
{
cubeIsOnTheGround = true;
}
}
void CreateDust()
{
dust.Play();
}
void SprintSound()
{
sound.Play();
}
void JumpSound()
{
ring.Play();
}
}
Comment