Home > database >  Attempting to add a Dash movement option using Unity and C#
Attempting to add a Dash movement option using Unity and C#

Time:12-03

I'm making a very simple platformer game, not to publish or anything like that but rather to experiment with Unity and C#, and I've been trying to make a dash mechanic. two ways that I tried to go about this were

  1. Getting the players position and teleporting them in any one direction, depending on the direction of the dash, didn't work because I couldn't figure out how to find the player's position
  2. Making the player move fast in any one direction, didn't work because of how the rest of the movement script works.

I would prefer to use the first option, does anyone know how to find the players location? I think I was able to find the transform position, but I didn't know how to use it since it was 3 values, x, y, and z, rather than one, and I didn't know how to only get 1. Thanks in advance!

CodePudding user response:

Not a definitive answer, since this depends on the code you are using and i have not shown how to dash, there is a lot of camera code and i am not coding unity anymore, so guessing this out without tests seem wrong, i would recommend adding the code, but the first option is simple enough to an answer.

In the player script, use transform.position, this will not fail since all Unity GameObjects have a world position, and therefore a transform.

// not sure if i spelled correctly
public class Player: Monobehaviour {
    /* ... */

    void Dash () {
        // transform.position is the current position as a 3D vector
       var pos = transform.position;

       // to access its x, y and z do this:
       var x = pos.x;
       var y = pos.y;
       var z = pos.z;
    }
}
  • Related