playMaker

Author Topic: Move Forward around object [SOLVED]  (Read 19360 times)

coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Move Forward around object [SOLVED]
« on: September 27, 2013, 10:33:39 AM »
Hi, Im new to unity and i am experimenting with Unity and Playmaker. I Want to make a character (just a cube atm) be able to walk around an object going against gravity.

The best example i can give is with the game Kula World, where the ball can move around some blocks. But when you jump off you fall as usual.

e.g. (go to 3:40 to see what i mean)

Is this possible in Playmaker or will I have to write a script?
« Last Edit: November 14, 2013, 11:57:13 AM by coxy17 »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Move Forward around object
« Reply #1 on: September 30, 2013, 02:16:55 AM »
Hi,

 I would use raycasting for this. Simply raycast ahead of the cube, if it finds nothing, check if you have a new route or side to the ground, and then conform to it.

 But it's not trivial to build for sure, so be prepare for some experimentation and trial/error quality time :)

bye,

 Jean

coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Re: Move Forward around object
« Reply #2 on: September 30, 2013, 03:55:46 AM »
Hi Jeanfabre

ok ill give it a shot. Thanks for the guideance. I was using Blender (python) and managed to achieve this but do not understand Unity fully yet. Heres my code from that game if anyone knows python to convert? *ignore key controllers.

Thanks again

Code: [Select]
from bge import logic, events
#from bge import render
from mathutils import Vector
from math import pi

tic_rate = logic.getLogicTicRate()

gravity = 20

turn_speed = 0.05
move_speed = 2.5
jump_force = 500.0

up_key = events.UPARROWKEY
down_key = events.DOWNARROWKEY
left_key = events.LEFTARROWKEY
right_key = events.RIGHTARROWKEY
run_key = events.LEFTCTRLKEY
jump_key = events.XKEY

class Translation:
   
    def __init__(self, own, sensors):
        self.own = own
        self.floor = own
        self.col_floor = sensors[1]
        self.velocity = Vector()
        self.rotation = Vector()
        self.force = Vector()
        self.gravity = Vector((0, 0, -gravity))
        self.normal = Vector((0, 0, 1))
   
    def keyboard(self, just_activated=1, active=2):
        events = logic.keyboard.events
        left = events[left_key] is active
        right = events[right_key] is active
        up = events[up_key] is active
        down = events[down_key] is active
        jump = events[jump_key] is just_activated
        move = up - down
        turn = left - right
        return turn, move, jump

    def get_normal(self):
        ori = self.own.worldOrientation
        start = self.own.worldPosition
        end = self.floor.worldPosition
#        render.drawLine(start, end, [1, 1, 0])
        hit_normal = self.own.rayCast(end, start, 1)[2]
        if hit_normal:
            self.normal = hit_normal
               
    def main(self):
        turn, move, jump = self.keyboard()
        self.rotation.z = turn * turn_speed
        if self.col_floor.positive:
            self.floor = self.col_floor.hitObject
            self.velocity.x = move * move_speed
            self.force.z = jump * jump_force
        self.get_normal()
        self.own.localLinearVelocity.xy = self.velocity.xy
        self.own.applyForce(self.force + self.gravity, True)
        self.own.alignAxisToVect(self.normal, 2, move_speed / 20)
        self.own.applyRotation(self.rotation, True)

def box(cont):
    own = cont.owner
    sensors = cont.sensors
    if 'translation' not in own:
        own['translation'] = Translation(own, sensors)
    own['translation'].main()
   
def mesh(cont):
    own = cont.owner
    velocity = own.parent.localLinearVelocity.x
    own.applyRotation([0, velocity * pi / 2 / tic_rate, 0], True)

coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Re: Move Forward around object
« Reply #3 on: October 03, 2013, 06:31:49 AM »
Hi, I have found this script on the Unity forum (http://answers.unity3d.com/questions/536740/magnetic-ball.html# and was wondering if it would work for what i am trying to achieve as i cant get the code to work :( . Could this be made just in Playmaker?

Thanks

Nick

mikejkelley

  • Full Member
  • ***
  • Posts: 136
Re: Move Forward around object
« Reply #4 on: October 03, 2013, 04:38:17 PM »
You can script in Unity using Boo, which is a Python variant...

coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Re: Move Forward around object
« Reply #5 on: October 03, 2013, 06:57:55 PM »
Hi Mike,

I could but i don't know about boo. Hardest part is understanding what BGE controls can be replaced with Unity? I feel like im not going to get anywhere with this. i guess there's not a clear answer. All of the guides online point to planets and not flat object e.g. Cube.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Move Forward around object
« Reply #6 on: October 04, 2013, 01:17:46 AM »
Hi,

 I have added this to my list of samples to do ( that huge list yes...). But it's low priority I am afraid, I have to keep up with what's currently supported on the wiki and finish that round first. So don't hesitate to contact me if you need something quick or if part of a project with a budget, we can see how to properly implement such system.

Bye,

 Jean

coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Re: Move Forward around object
« Reply #7 on: October 17, 2013, 09:34:22 AM »
Hi, Ive got a script which could be made in Playmaker but dont know how to go about it? The code has not been error checked. Just a guide. Would be great if anyone could create a demo.

Code: [Select]
private Vector3 surfaceNormal;
private Vector3 personalNormal;
private float personalGravity = 9.8f;
private float normalSwitchRange = 5.0f;

public void Start()
{   
    personalNormal = transform.up; // Start with "normal" normal
    rigidbody.freezeRotation = true; // turn off physics rotation   
}

public void FixedUpdate()
{
    // Apply force taking into account character normal as personal gravity:
    rigidbody.AddForce(-personalGravity * rigidbody.mass * personalNormal);
}

public void Update()
{
    // we don't update personal normals in the case of jumping
    if(!jumping)
    {
        UpdatePersonalNormal();
    }

}

public void UpdatePersonalNormal()
{
    RaycastHit hit; //hit register
    // list of valid normals to check (all 6 axis)
    Ray[] rays =
        {
        Vector3.up, Vector3.down,
        Vector3.left, Vector3.right,
        Vector3.forward, Vector3.backward
        };

    //for each valid normal...
    foreach(Ray rayDirection in rays)
    {
        //check if we are near a cube face...
        if(Physics.Raycast(rayDirection , hit, normalSwitchRange)
        {
             personalNormal = hit.Normal; //set personal normal ...
             return; // and return as we are done
        }
    }
}

I havent added a jump button as im not sure how it will work with not updating personal normals.

Also when the physics switches, i can then add in rotational changes.

Nick
« Last Edit: October 17, 2013, 09:43:42 AM by coxy17 »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Move Forward around object
« Reply #8 on: October 21, 2013, 03:24:22 AM »
Hi,

 Ok, first, your pseudo code is a bit odd, so I am not sure what you are trying to achieve here, but you will need to fiddle around I guess.

-- surface normal is not used
-- I assume you want to always have the local z axis as the personal normal, so you need to update this every update.
-- Freezing rotation, can be done by simply unchecked the rotations axis in the physics constraint section of the physics component, so no need to define it in your code.

 Please find attached a working sample doing all this. Let me know if you understand how it's  done.

bye,

 Jean

coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Re: Move Forward around object
« Reply #9 on: October 21, 2013, 09:19:23 AM »
ok i realise the code is a bit weird. Just trying to get a starting point. Ill look at the sample and if i have any luck ill post it up.  :) thanks!
« Last Edit: October 21, 2013, 09:23:57 AM by coxy17 »

coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Re: Move Forward around object
« Reply #10 on: October 22, 2013, 07:44:24 AM »
I understand the code you have submitted. Ive made this https://www.dropbox.com/s/3rx55rf32jvjv8e/test.avi from my previous Python example to show you all the actions i want to achieve. Your logic works but im struggling to add in last bit of the characters functionality. Ive tried various option with raycasting and having the character rotate when hanging over the edge. Just cant make it like my video.

I must be missing something. Shall i upload my test file?

All this for a uni project lol

Thanks

Nick

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Move Forward around object
« Reply #11 on: October 23, 2013, 02:37:11 AM »
Hi,

 Ok, I have added this to the trello task list management. If you want to be part of it and vote for what to do next, pm me with your email to become a member of the board and vote up for your favorite tasks.

Bye,

 Jean

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Move Forward around object
« Reply #12 on: November 07, 2013, 11:08:12 AM »
Hi,

 Ok, I finally had time to work on this, and given your requirement I went for the following approach:

 instead of defining the gravity using the normal of the ground, I use a collider. Everytime you have a new "gravity" field on your level, have this collider for the player to trigger and I use that collider transform to deduce the gravity to apply from then on.

The direct benefit is that you define accuratly what gravity to apply and where. In the case of your cube ( if the level was just a cube), using the normal of the cube is tricky to get because your character would be falling, but then how do you know what face of the cube to catch as the new gravity, for this would would need to raycast in all directions of the player, which in turn would make it difficult to scale, because imagine you have a wall, then it would detect the wall and apply the gravity and you would then be attracted by the wall. It is possible to implement layers and ignore objects etc, but I felt this has too much constraints.

So, have a play with this setup and let me know, If you still need a ground based normal gravity, let me know, that's nothing difficult to implement, but then the player will only check for what's beneath himself, not what's surrounding, as implement htis would require a very specific description of how the game play should behave.

Bye,

 Jean

coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Re: Move Forward around object
« Reply #13 on: November 11, 2013, 09:39:33 AM »
Thanks Jean,

I will take a look and let you know :)

Nick

coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Re: Move Forward around object
« Reply #14 on: November 11, 2013, 11:57:34 AM »
Hi Jean,

How can you rotate a player when entering a trigger and set the Y direction to the trigger just entered? i can sort of see how to apply it but this is the only part im struggling to understand. The rest is great!!!!!! i can apply what you have supplied to what im trying to achieve.

Just need to know how to 'get' and 'set' the player orientation on entering a trigger to the triggers world position.

Thanks so much!

Nick