playMaker

Author Topic: [solved] Move cube when player gets near  (Read 8042 times)

slipside

  • Playmaker Newbie
  • *
  • Posts: 4
[solved] Move cube when player gets near
« on: March 26, 2011, 03:14:37 PM »
Hi.
I just bought playMaker and I'm trying to learn it.
Im new to unity and playMaker
How do I perform a simple task as making a cube move out of the way a bit when my player object gets within a certain distance?
I get the distance from Get Distance, but how can I use that distance to tell the cube to move when player gets near?
Before playMaker I accomplished this with a script but I cant seem to do it using playMaker.
I'm no good at coding so thats why playMaker seems like a great way to get my ideas in motion.
Appreciate any help I can get :)


« Last Edit: April 15, 2011, 04:36:30 PM by alexchouls »

tobbeo

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 186
Re: Move cube when player gets near
« Reply #1 on: March 27, 2011, 02:40:34 AM »
Hi,

The distance is stored in a float variable, so make sure where it says "store result" you have created a variable that appears in the drop down menu for you to store it in (and make sure you check Every Frame). Then you use a "Float compare" under the Logic category to send the event onwards to another state when it becomes less than your variable.

By now you you have the ability to do what you want when your object gets too close to your other object. Since you want to move it away, you need to use "Move Towards" but in order to use that you need to figure out how to get the position that's just a bit further away than your current position. That would get you started, by now you should at least be able to move it away when it gets too close to your player, do you need to figure out how to determine what direction you move it away and determine how far. I would probably use a Look At and then move it opposite on the axis that is pointing towards the player. Stuff like this will be a bit easier when iTween is incorporated as actions.

What I found when I got Playmaker is that you need to sit down and figure it out. Do small things to begin with to just get the feeling of how it works. It is a very easy program but at the same time you need to understand how it works, check out the examples included as they helped me out a lot personally. Also refer to the manual a lot. Go through the screen casts. Also remember that all your actions in your state is executed from top to bottom so the order that you put your actions in is important.


MaDDoX

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 165
    • FluidPlay Studios
Re: Move cube when player gets near
« Reply #2 on: March 27, 2011, 06:37:15 AM »
I get the distance from Get Distance, but how can I use that distance to tell the cube to move when player gets near?
Hi slipside, welcome aboard :) As for your question, IMHO you're taking the wrong (or, to be more precise, hardest) route. All you need is to add a collider to the cube (spherical, cubic, whatever suits your will) and check the "trigger" checkbox in Unity's inspector. As a trigger the collider won't "block" the player movement, which's probably what you want.

Inside PlayMaker one of the builtin events is exactly TRIGGERENTER, which's fired everytime one object (generally the one attached to the state machine) collides with a trigger collider. Make that event drive the FSM execution to another state where you push the box away from the player. As for actually getting the direction, you can try a raycast (https://hutonggames.fogbugz.com/default.asp?W492), if it's fired by the player you won't even have to transform it like what tobbeos suggested. Actually you can skip the triggerenter event entirely and just use raycast with the distance you want, having it fired all the time it's costlier than just once (of course) but if you don't have many objects in your scene and keep the distances low you can get away with it just fine.

You can get as fancy as taking the player speed (supposing it's variable) at the moment of collision (using Get Speed) and applying that as the force component in Z ;)
--
Breno "MaDDoX" Azevedo
@brenoazevedo

e.Ko

  • Administrator
  • Playmaker Newbie
  • *****
  • Posts: 13
Re: Move cube when player gets near
« Reply #3 on: March 27, 2011, 11:49:17 AM »
As you can see there a few different ways to do what you want.

Personally I think the easiest approach is to use a trigger volume to play an animation on the cube.

See PlaymakerSamples\TestLab\Triggers\AutomaticDoor for an example. You can also mockup an animation very quickly in the Unity Animation window - it's worth spending some time learning how to use the Animation window since it lets you prototype very quickly - instead of worrying about modelling/animating/exporting...

Trigger volumes and collisions are very quick ways to make objects interactive. Learn how they work in Unity (how to set them up in the scene etc.) and you can then use them to trigger state changes in Playmaker.

edwon

  • Playmaker Newbie
  • *
  • Posts: 4
Re: [solved] Move cube when player gets near
« Reply #4 on: July 21, 2011, 03:48:25 AM »
I have to say this is not solved. I have been trying to do the same thing for some time now (move a box away from the player when the player enters the trigger) and the explanations here are incomplete at best.

The hardest part of the problem? finding the direction to move the box away from the player. I've tried everything I can think of with no luck. Please help!

Maddox, perhaps you can expand upon your idea to use Raytrace to get figure out which direction the box should move?
« Last Edit: July 21, 2011, 05:09:31 AM by edwon »

tobbeo

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 186
Re: [solved] Move cube when player gets near
« Reply #5 on: July 21, 2011, 05:45:42 AM »
I have to say this is not solved. I have been trying to do the same thing for some time now (move a box away from the player when the player enters the trigger) and the explanations here are incomplete at best.

The hardest part of the problem? finding the direction to move the box away from the player. I've tried everything I can think of with no luck. Please help!

Maddox, perhaps you can expand upon your idea to use Raytrace to get figure out which direction the box should move?

You say the biggest problem is finding the direction to move the box. You know the direction the player is moving because you are controlling him. So if you move your character "left" towards the box, you use that value to orient your box and then play a simple animation to move it (or use itween). So your problem now sounds just to store that vector, and you already have that vector somewhere since you've been able to set-up your player to move in a direction based on a button press.

I bolded part of your reply. They aren't incomplete, but they aren't going to walk you through step by step. Someone needs to do a tutorial in that case and that shouldn't be expected to get for free. I am not an experienced coder but your problem is fairly simple. Maybe if I have time I'll do it but I also suggest keep at it as it will increase your understanding of PM and Unity when you do solve it yourself.

edwon

  • Playmaker Newbie
  • *
  • Posts: 4
Re: [solved] Move cube when player gets near
« Reply #6 on: July 22, 2011, 12:51:16 PM »
So finally found the solution to this, or at least a solution.

Using a trigger attached to the cube to go into a "move away" state.

The move away state uses a Vector3 Subtract to subtract the cubes position from the players position, resulting in the difference between their positions.

Then I added that difference to the cubes position using iTween Move Add.

Works pretty well although not perfect. I also experimented with using Add Force but couldn't get it function even with a rigidbody on the cube.
« Last Edit: July 22, 2011, 12:52:48 PM by edwon »