How to align cube to plane ?

Use image as reference.

I want the cube “Playerbox example” to go in a straight line of the pipe on top, when the player box touches the plane with the 3d cursor on it, it will go to a PIPE CLIMB STATE.

The player will then shimmy across the bar to the other side, but the problem is if the character
is slightly off axis, it will go away from the other platform.

Any ideas how to keep in straight line all the time.

The playerbox detects the property of the plane on floor to go into a Pipe climb state.

ray = cont.sensors[‘ray’]

You also may want a bool to decide what to do with the other axis,

So my logic on my “PlayerBox” would be. Ray > python ?
is that all the python script ? do i paste just that into the text ?


No, you probably need to have a seperate state,
And you will probably need to add more

import bge
from mathutils import Vector
cont = bge.logic.getCurrentController()
own = cont.owner
Ray = cont.sensors['Ray']
if Ray.positive:

This will line up the up axis to the plane, but you need to face forward/backward etc

You can get a local axis out of the hitObject like this
direction1 = Ray.hitObject.worldOrientation.col[0]

This is the local x axis of the hitObject

Direction2 =Direction1*-1

And this will give you the -x

I added .blend as it would be easier for me too see it in action :slight_smile:

I think this video explains better. As you can see, it is not aligned up, so when i touch the plane
i need it to make the cube align perfect forward :slight_smile:


and like I said, there will be complications going back the other way, without some math :smiley:

I know it though, I will try and simplify it for you.

btw, I moved the plane above the actor, and went ahead and added the logic to stick to it :stuck_out_tongue:

What you’re actually asking (I think) is to align the player direction to the bar that spans the gap between the platforms. You are essentially intending on creating a constraint. You can probably use a BGE constraint, or do it using Python. If the path is always straight, do the following:

Find the end and start points, and subtract the start from the end to get the direction (normalize this).
Align the player direction using alignAxisToVect (or find the alignment matrix (Y is the direction vector, X is the cross product between the direction and the world Z (in that order) and Z is the cross product between the X you found and the direction). This is the new worldOrientation matrix.
To move the player, add a position delta along the direction (own.worldPosition += direction * displacement). You would need to handle the fact that gravity will move the player.

here it is with a system that always faces across the gap, but if you back up to it , it will flip you the right way,

goose, already done :smiley:

My method is different, but it works

I store a local, and if local.x is greater than 0, align the y axis to the bars local axis X,

else align the players local y to the bars -x

the local resets after you drop off the bar.

That blend is exactly what i need. It`s just implementing it now, how would it work with a state.
When my character reacts to that plane on the floor, he goes into a state so he can do the action.

I will upload a video soon. I think i got it :wink:

I put on first state (main movement) the Ray and Time.
Once ray has been hit, it will change to Pipe grab state, once its aligned because the ray has to hit for the state to change. ?? BPR thanks once again :slight_smile: i hope this is working 100%

you may want to have the timer go ->45

and then count down back to 30 while aligning the player worldPosition

I’ll take a look at it tonight, but basically, when you get under the bar, it will always ‘nudge’ the actor into the right place/ world position, to make it 100% the same each time you use it.

Yeah i noticed it always works, but sometimes it still offsets the player :confused:
i think we almost nailed it though :slight_smile:

In BPR’s example the x axis (side to side) does not change, whereas in your video it does.

Therefore you need to align both the x and the z.
I would think something along the lines of:

OFFSET = 0.6
own.worldPosition.x = rail.worldPosition.x
own.worldPosition.z = rail.worldPosition.z - OFFSET

After you have that part working then you can focus on smoothing it out (through code or animations).

Yeah its confusing, as im using exact code as the .blend her shared.

which sensor do you use to detect the climbable object?

I`m using a “Ray” to detect a property and once property is detected i enter a new state :slight_smile:

I localize the player position to the bar, so the x axis I check against is local to the pipe, I only check this one time and store it, when you climb off the value is cleared when the cloxk runs out.

Ah you use state to separate the functions?

I have ladders in my game too, and they basicly just switch a property that allows you to control the character, if that property is off, the ladder can fully remote the character ( climbing action, move it up ). I use collision sensor for it, it works really well and prevents wrong offset