I'm having some trouble with the alignToVect to the ground

(PixelSonic101) #1

I’m having some trouble with the alignToVect.I’m trying to make sonic to aligning to the ground

(Cotaks) #2

cast ray down, grab ray.hitNormal and align to that.

(PixelSonic101) #3

sorry i’m confused

(PixelSonic101) #4

if you want to have the blend file then here https://drive.google.com/file/d/1Z9FCnz0egc2xZWlgCz7Z4pmsr3NUIIi4/view?usp=sharing

(BluePrintRandom) #5

so we want to take a sample down from the actors origin but we move the endpoint out in front of the actor based on linV just a little

import bge

own  = bge.logic.getCurrentController().owner

rayStart = own.worldPosition
rayEnd = own.worldPostion + (own.worldOrientation.col[2]*-(1/2 height + margin))
prop = ""
ray = own.rayCast(rayEnd,rayStart, 0, prop, 0,0,0)
rate = .5
if ray[0]:
    own.alignAxisToVect(ray[2],2, rate)

(PixelSonic101) #6

so do i add this to the player or the alignToVect

(Cotaks) #7

put it on the player box

BPR’s solution needs some more information filled in, this wil just do what you asked align to floor

import bge

own  = bge.logic.getCurrentController().owner

align_vec = own.worldPosition - own.worldOrientation.col[2]

ray_distance = 2
ray = own.rayCast(align_vec,own, ray_distance)

if ray[0]:
    align_speed = 1.0
    own.alignAxisToVect(ray[2], 2, align_speed)

oh at the end of a ramp, if you want to lets say run up a wall, just bevel the start point and end point, and you get a seemless running sonic

(PixelSonic101) #8

there’s a problem https://blenderartists.org/uploads/default/original/4X/b/5/b/b5b468e6f006b2678b75bdc6746f5d38112f9d75.mp4

(Cotaks) #9

it aligns so what is the problem, if you use even less words, even less people will be interested to help.

anyway take a look at my resource here, might help you.

(PixelSonic101) #10

when Sonic goes up he turns

(Cotaks) #11

you probably got more then just the align script to it, because the script wont turn it at the end, to prove that here is an example:

running up.blend (487.1 KB)

(PixelSonic101) #12

ok so I copy and paste your script the alignToVect doesn’t work so I’m going send you a blend file and see what am I doing wrong https://drive.google.com/file/d/1kEb8hmuv-PxkhbOATVaij9PZ1uYWZqSh/view?usp=sharing

(Cotaks) #13

I can’t do much with the .blend. if i hit p sonic gets in mid air but nothing does anything.

While i can’t test anything i do see that you got it on the wrong object, select the up and lower box of sonic, press H to hide it. now you see the actual bounding box, and you see that the other align script is running from there, replace that script with mine and in theory it should work.

(PixelSonic101) #14

I’m so confused

(Cotaks) #15

why? 123456789

(PixelSonic101) #16

it worked ‍ ‍ ‍

(PixelSonic101) #18

I think I know why Sonic turns when he goes up like in the video it’s because of a script that I use of alignAxisToVect
here is a script for it

import bge

ALIGN_SPEED = 0.5 # set this to determine how fast the player aligns to the motion. 1.0 being instant and 0.0001 being really slow.

own = bge.logic.getCurrentController().owner

target = bge.logic.getCurrentScene().objects["Player"]

target_vel = -target.worldLinearVelocity
if target_vel.length > 0.1:
    own.alignAxisToVect(target_vel, 1, ALIGN_SPEED)
    own.alignAxisToVect([0,0,1], 2, 1)

(Cotaks) #19

import bge

# put a property in objects you want to align to called : align_player
# now when ray hit that property it will align to it, put property 
# ground in the things when sonic needs his head to be up

own  = bge.logic.getCurrentController().owner

ground_vec = own.worldPosition - own.worldOrientation.col[2]

ray_distance = 2
ray = own.rayCast(ground_vec,own, ray_distance)
align_speed = 1.0

if ray[0]:
    if 'align_player' in ray[0]:
        #align to an object        
        own.alignAxisToVect(ray[2], 2, align_speed)
    elif 'ground' in ray[0]:
        #heads up
        own.alignAxisToVect([0,0,1], 2, align_speed)

(PixelSonic101) #20

am I doing this right

(jesusmora) #21

this is unnecesarily complicated.

what does col[2] stand for? you are supposed to call worldOrientation like own.worldOrientation[2].

@PixelSonic101 try this instead:

import bge
cont = bge.logic.getCurrentController()
obj = cont.owner
scene = bge.logic.getCurrentScene()

ray_distance = 2.0
align_speed = 1.0
#speed at wich sonic must turn and face upwards
falling_speed = 0.0
#the speed at wich sonic will wall walk
wall_walking_speed = -0.2

pos = obj.worldPosition
other = scene.objects.get("step")
floor = obj.rayCast(other, pos, ray_distance, "floor", 0, 1, 0)
if floor[0] != None and obj.getLinearVelocity(True)[1] > wall_walking_speed:
     obj.alignAxisToVect(floor[2], 2, align_speed)
elif obj.getLinearVelocity(False)[2] <= falling_speed:
     obj.alignAxisToVect([0.0, 0.0, 1.0], 2, 0.5)

then create an empty called “step” and parent it at the bottom of sonic, exactly at sonics x and y coordinates but shifted in z.
and the surface on wich sonic can walk must have the property “floor” on them.