Making a dash

Hello guys,
I am doing a minor project for an exam, and I am currently a bit stuck.
The base of the game is just using Unity’s Roll-A-Ball tutorial. What I am doing for my exam, is trying to make a seperate kind of collectibles that requires the player to dash through them in order to pick up said collectible.
I got the collectible made, however I can’t figure out how to make the dash ability, actually only dash relative to the axes of the plane / ground, rather than the axes of the sphere when it is rolling…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Character.Abilities
{
    public class DashAbility : Ability
    {
        [SerializeField] private float dashForce;
        [SerializeField] private float dashDuration;

        private Rigidbody rb;

        private void Awake()
        {
            rb = GetComponent<Rigidbody>();
        }
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.R))
            {
                StartCoroutine(Cast());
            }
        }

        public override IEnumerator Cast()
        {
            rb.AddForce(transform.forward * dashForce, ForceMode.VelocityChange);

            yield return new WaitForSeconds(dashDuration);

            rb.velocity = Vector3.zero;
        }
    }

}

I know that it is not supposed to use the transform.forward, hence why it’s using the axes of the rolling sphere. What I can’t seem to figure out is how to make it move relative to the ground.

Hope somebody can help me!

Rolling sphere forward vector is rolling, i.e. pointing different direction every frame. Use rigidbody.velocity instead

Hey, I have not worked much with velocity before. I haven’t been able to work out a way for it to work yet. Can you maybe give me a little hint? :slight_smile:

You should be able to just replace transform.forward with rb.velocity and it’ll work.

You may want to use rb.velocity.normalized if you want the boost to give the same speed boost regardless of how fast the player rolls the ball into it.

I came up with a solution myself!
However, yours was somewhat simpler, so I think i’ll go with that! Thank you guys :slight_smile: