Barofioso

How to create a car at right side next to player

Recommended Posts

Hello

Someone asks me, how to create a car at right side next to player. So i just wrote this little tutorial about. I hope you enjoy this little thing, which was did in two hours, and explained with a horrible english ;D

Spend time:

  • 2 hours testing the spawn of vehicles and coding + doc
  • 3 hours making this tutorial with translation
  • 0.5 hours thinking about, why i am doing this ... (i didn't found an answer -.-)

First we have to know some things:

  • How is the World working
  • How is the Position working
  • How is the Rotation working
  • How can i spawn a car (Code snippet)

 

How is the World working? => It is a north axis oriented map. If you open the full map ingame, top is north, bottom is south. It is every time a constant and will not change

How is the Position working? => It is a Vector based information. It has three types.

  • X => left - | + right
  • Y => top - | + bottom
  • Z => down - | + up

y4m7eVkzPnEUMadAQwV9Ec8srFKoeYwnY4QN7Tt9

The "+" and "-" are the operators here in our coordination system, which we have to use later.

How is the Rotation working? => It is a Vector based information. Same here we have three types, but the number is always positive. So we can just use the Z variable.

  • X => back | front (Not tested)
  • Y => left | right (Not tested)
  • Z => ground

Then we will get four options:

  • N => 0°
  • E => 270°
  • S => 180°
  • W => 90°

y4mY3DXqda1iG1geXF1OcsDMcJbofX37xB9FVVvI

We have to use the radius to know how the player is rotated away from north.

Just think about your shool days. back then you had geometry and raius was a topic of it. :D

 

Ok, we got our information. Let us code that stuff

Small version

Spoiler
//Get Data
Vector3 carPos = client.Position;//current player pos
Vector3 cr = client.Rotation;//current player rot

//Get config
float distance = 2f; //hardcode

//Check player rotation
if (cr.Z >= 0 && cr.Z < 90) {
  //left side
  carPos.Y += distance;
}
else {
  if(cr.Z >= 90 && cr.Z < 180) {
    //back side
    carPos.X -= distance;
  }
  else {
    if (cr.Z >= 180 && cr.Z < 270) {
      //right side
      carPos.Y -= distance;
    }
    else {
      if(cr.Z >= 270 && !(cr.Z > 360)) {
        //front side
        carPos.X += distance;
      }
      else {
        //No way! 359.9 + 0.1 => 0
        NAPI.Util.ConsoleOutput("create car -> ??? more than 360 degrees rotation?");
      }
    }
  }
}

//finally create the car and enjoy 
Vehicle xvehicle = NAPI.Vehicle.CreateVehicle(VehicleHash.Adder, carPos, cr, 1, 1, "34", 255, false, true, client.Dimension);

 

Doc version

Spoiler
/*
 * Orientation NorthAxis
 * 
 * X => left - | + right
 * Y => back - | + front
 * Z => down - | + up
 */
Vector3 carPos = client.Position;

/*
 * Orientation NorthAxis (everytime positive float number)
 * 
 * N => 0°
 * E => 270°
 * S => 180°
 * W => 90°
 */
Vector3 cr = client.Rotation;

/*
 * if you want to create an admin command, 
 * which allows you to create every time on your right side a car, 
 * no matter how the player is rotated on the north axis (face looks on x)
 * 
 * You have following to know
 * 
 * if the player looks left West
 *  - rotation = 90
 *  - position
 *   -> X - = front
 *   -> X + = back
 *   -> Y - = left
 *   -> Y + = right
 *  
 * if the player looks right East
 *  - rotation = 270
 *  - position
 *   -> X - = back
 *   -> X + = front
 *   -> Y - = right
 *   -> Y + = left
 *  
 * if the player looks back South
 *  - rotation = 180
 *  - position
 *   -> X - = right
 *   -> X + = left
 *   -> Y - = front
 *   -> Y + = back
 *  
 * if the player looks front North
 *  - rotation = 0
 *  - position
 *   -> X - = left
 *   -> X + = right
 *   -> Y - = back
 *   -> Y + = front
 * 
 * And you have to use a variable to declare the distance between car and your player :D
 * 
 */
float distance = 2f;//use it with a config system or something like that, where you can set this easy - hardcode ;D


//If the player rotation is bigger/or 0 and smaller than 90
if (cr.Z >= 0 && cr.Z < 90) {
  //left side
  /*
   * position
   * 
   * X => 0
   * Y => Y + distance
   * 
   * rotation = same like player
   * 
   */
  carPos.Y += distance;
}
else {
  //if the player rotation is bigger/or 90 and smaller than 180
  if(cr.Z >= 90 && cr.Z < 180) {
    //back side
    /*
     * position
     * 
     * X => X - distance
     * Y => 0
     * 
     * rotation = same like player
     * 
     */
    carPos.X -= distance;
  }
  else {
    //if the player rotation is bigger/or 90 and smaller than 270
    if (cr.Z >= 180 && cr.Z < 270) {
      //right side
      /*
       * position
       * 
       * X => 0
       * Y => Y - distance
       * 
       * rotation = same like player
       * 
       */
      carPos.Y -= distance;
    }
    else {
      //if the player rotation is bigger/or 270 and not bigger than 360
      if(cr.Z >= 270 && !(cr.Z > 360)) {
        //front side
        /*
         * position
         * 
         * X => X + distance
         * Y => 0
         * 
         * rotation = same like player
         * 
         */
        carPos.X += distance;
      }
      else {
        //No way! 359.9 + 0.1 => 0
        NAPI.Util.ConsoleOutput("create car -> ??? more than 360 degrees rotation?");
      }
    }
  }
}

//finally create the car and enjoy 
Vehicle xvehicle = NAPI.Vehicle.CreateVehicle(VehicleHash.Adder, carPos, cr, 1, 1, "34", 255, false, true, client.Dimension);

 

Edited by Barofioso
  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.