playMaker

Author Topic: add speed float to player controller movement based on controller's Y rotation?  (Read 203 times)

westingtyler

• Full Member
• Posts: 221
add speed float to player controller movement based on controller's Y rotation?
« on: April 09, 2019, 07:19:55 PM »
how do I add a speed float to a player controller based on controller's Y rotation?

Basically, the goal is: the player is on a bike. and if you press A, it starts you moving using a speed float, in the direction the player controller game object is facing. You can use a joystick to change the player's facing direction (Y rotation), and you always move forward according to the direction of the bike.

the character controller will move along the X and Z axes at the speed specified by the speed float, using a controller move action, but I'm not sure how to add the speed to the move vector based on the Y direction of the player. (to deal with diagonal movement in the world.)

How is this achieved? I originally used a rigidbody add force thing, but I could not figure out how to make gravity work while also preventing it from ramming into inclines and hills it met. So I'm using a player controller because it knows how to fall and go up and down hills normally.
« Last Edit: April 26, 2019, 06:13:22 AM by westingtyler »

westingtyler

• Full Member
• Posts: 221
Re: add speed float to player controller movement based on controller's Y rotation?
« Reply #1 on: April 26, 2019, 06:12:22 AM »
bump. does anyone how how to do this kind of conversion?

I'm thinking:
somehow get the world direction the player/bike is pointed. (?)
get speed float.
DO SOMETHING.
set final vector3 as a controller move vector.
what is the missing piece?

i want my character to continue moving in the direction the bike is pointed, regardless of direction the camera is facing, with acceleration being added or removed based on which trigger you're holding. (basically i want to build grand theft auto vehicle controls.)
« Last Edit: April 26, 2019, 07:44:50 AM by westingtyler »

Thore

• Sr. Member
• Posts: 275
Re: add speed float to player controller movement based on controller's Y rotation?
« Reply #2 on: April 28, 2019, 05:01:19 AM »
In principle, solve these parts.

1st FSM: Axis to Rotation
Get horizontal axis and use the value (which goes from -1 to +1) to incrementally turn/rotate the object. Observe the scene view and see the arrows when having the object selected. They represent the forward vectors. Make sure your bike is aligned to the correct axis (or better, try with a clean scene and prototype there with a sphere, add a small coloured cube with no collider as a child, so you see instantly where forward is).

2nd FSM: Speed
Get vertical axis and store the value. Usually, float multiply it with 5 (as a start) and store that on a new variable, and use that variable to set velocity. Make sure you do all of this in one state and every frame.

This outline should work. Problems with slopes and such should disappear when you set the rigidbody collision as continuous, and by using a sphere collidere, where you can freeze their rotation. The colliders should be used in a functional way, not mimick how your object looks like, and you can easily use multiple on the same object. Use a sphere for the base (or where the wheels are) and add cube(s) offset above when you want collison there, too, but make sure that the base (where it drives) is round.

Controls should be easy to set up and get going, but they require A LOT of time to get right. You need to tweak gravity scale on the object, or set up more elaborate ways how velocity is added etc.