playMaker

Author Topic: Add force to parented object only on one axis  (Read 3821 times)

18LSC

  • Playmaker Newbie
  • *
  • Posts: 32
Add force to parented object only on one axis
« on: March 13, 2018, 12:24:30 PM »
I have made a pickup script, which parents the held object to a small invisible cube in front the player. Which I can also mouse look rotate with a key.

I want to be able to throw the object, by adding force to it along the players Z axis.

My problem is the 2 options for adding force are "world or self", world wont work because the player is constantly rotating around. So the added force will throw the object in random directions.
And self won't work either because the held objects Z axis also keeps changing.

I want to add force to the held object that always follow the players Z (or other) axis, how do I isolate the always changing players axis and apply that direction of force to the held object each time? regardless of the world or the held objects personal axis? :-\  thank you.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Add force to parented object only on one axis
« Reply #1 on: March 13, 2018, 01:05:52 PM »
Hi,
Maybe you can use an empty parent on the held object and then add force on that?

Many times i use multiple partents on a singe object. especially on the player

18LSC

  • Playmaker Newbie
  • *
  • Posts: 32
Re: Add force to parented object only on one axis
« Reply #2 on: March 14, 2018, 05:47:58 AM »
Hi,
Maybe you can use an empty parent on the held object and then add force on that?

Many times i use multiple partents on a singe object. especially on the player

I fail to see how that would be any different, the empty would then also rotate when I pick it up, changing its axis just like before

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Add force to parented object only on one axis
« Reply #3 on: March 14, 2018, 09:53:28 AM »
Hi.
Can you show some images or a video on what you try to do?
Maybe i misunderstood what you mean.

18LSC

  • Playmaker Newbie
  • *
  • Posts: 32
Re: Add force to parented object only on one axis
« Reply #4 on: March 14, 2018, 05:06:27 PM »
Hi.
Can you show some images or a video on what you try to do?
Maybe i misunderstood what you mean.

Sure, I just want to add force to my held object regardless of its own axis or the world axis, I only want to use the players Z axis

https://i.imgur.com/bMYNSYg.jpg
« Last Edit: March 14, 2018, 05:08:27 PM by 18LSC »

18LSC

  • Playmaker Newbie
  • *
  • Posts: 32
Re: Add force to parented object only on one axis
« Reply #5 on: March 18, 2018, 07:41:22 AM »
I'm still stumped on this one  :-\

I don't know what actions to use to always track the players forward axis they are facing, and add force to the held object from that direction every time.

Doh

  • Full Member
  • ***
  • Posts: 124
Re: Add force to parented object only on one axis
« Reply #6 on: March 18, 2018, 08:51:27 AM »
One way you could do this would be to use "Transform Direction" to convert your player look direction to world space, then use "Vector 3 Multiply" to setup the power of the throw and finally use "Add Force" to propel the object.

I've attached an image for reference, in which "cube (6)" was the child object being thrown.

In your case you probably want to unparent the object just before throwing by using "set parent" and setting it to none.

Hope that helps!

18LSC

  • Playmaker Newbie
  • *
  • Posts: 32
Re: Add force to parented object only on one axis
« Reply #7 on: March 20, 2018, 01:19:00 PM »
One way you could do this would be to use "Transform Direction" to convert your player look direction to world space, then use "Vector 3 Multiply" to setup the power of the throw and finally use "Add Force" to propel the object.

I've attached an image for reference, in which "cube (6)" was the child object being thrown.

In your case you probably want to unparent the object just before throwing by using "set parent" and setting it to none.

Hope that helps!

Massive thanks! I knew what I had to do but didnt know how to apply it, your tips helped ;D

But, I was typing my reply that this didn't work at first, besides a tiny puff of a force added to the thrown object, but I tested a little more and it now works, but I dont quite understand why lol

My changes:
I took the "transform direction" settings you gave "Z=1" and changed that to a higher number on Z, and that was my issue, 1 gave a little puff of force, I changed that to 5, now it works, even if I disable vector3 multiply altogether it still works the same :/

I still need addforce as the last step, but now its taking the "transform direction" of the players Z axis and adding the force there, and it throws only in the direction I face, yay.
So Im not sure why the vector3 multiply wasnt being applied to the other method.

Though I have one question, why does addforce "force mode" use Velocity change and not Force?
« Last Edit: March 20, 2018, 01:38:12 PM by 18LSC »

Doh

  • Full Member
  • ***
  • Posts: 124
Re: Add force to parented object only on one axis
« Reply #8 on: March 20, 2018, 03:47:59 PM »
I came across and still come across similar issues where I know every step of what I want to do but finding the best actions is difficult.

If you want to upload a screen shot of your state in play mode I could see if something stands out with the multiplier. The multiplier as I'm sure you can see gives you a decent way to control throwing power.

I only used Velocity Change as a quick means to an end, it ignores mass so it's pretty straight forward to get going. I'd recommend you use what works best, you may want a system that takes into account the weight of objects so that larger objects are only slightly heaved forwards, almost dropping at the player's toes. If you wanted that you'd possibly use Impulse force mode.

In case you've not come across it at the bottom of this link you'll see the force descriptions: https://docs.unity3d.com/ScriptReference/ForceMode.html

I regularly have to re-check it.

18LSC

  • Playmaker Newbie
  • *
  • Posts: 32
Re: Add force to parented object only on one axis
« Reply #9 on: March 20, 2018, 05:24:51 PM »
I came across and still come across similar issues where I know every step of what I want to do but finding the best actions is difficult.

If you want to upload a screen shot of your state in play mode I could see if something stands out with the multiplier. The multiplier as I'm sure you can see gives you a decent way to control throwing power.

I only used Velocity Change as a quick means to an end, it ignores mass so it's pretty straight forward to get going. I'd recommend you use what works best, you may want a system that takes into account the weight of objects so that larger objects are only slightly heaved forwards, almost dropping at the player's toes. If you wanted that you'd possibly use Impulse force mode.

In case you've not come across it at the bottom of this link you'll see the force descriptions: https://docs.unity3d.com/ScriptReference/ForceMode.html

I regularly have to re-check it.

Okay well heres a screenshot of them during gameplay, both attached to the FPC.

The V3 is updating as you can see. I turned multiply back on for this but it works on or off, but making it "111" doesn't affect throw force



Doh

  • Full Member
  • ***
  • Posts: 124
Re: Add force to parented object only on one axis
« Reply #10 on: March 20, 2018, 06:32:55 PM »
Have you tried putting Multiply above Add Force? For it to work exactly as I had it you need it to execute in the same order, so top to bottom, 1st Transform Direction, 2nd Multiply Vector3 and 3rd Add Force.

Is it important for you to get the transform direction every frame? If it's only required on throw I'd probably contain them together in one state and have a transition/global-transition leading to it. There are many ways you could approach this, how are you planning to instigate a throw? Do you have a central player controller FSM that monitors player input?

It's hard to tell what's really going on due to the image being quite tight, the second column for Transform Direction kinda makes it look like a screenshot on a screenshot. Probably besides the point anyway.

18LSC

  • Playmaker Newbie
  • *
  • Posts: 32
Re: Add force to parented object only on one axis
« Reply #11 on: March 20, 2018, 08:06:50 PM »
Have you tried putting Multiply above Add Force? For it to work exactly as I had it you need it to execute in the same order, so top to bottom, 1st Transform Direction, 2nd Multiply Vector3 and 3rd Add Force.

Is it important for you to get the transform direction every frame? If it's only required on throw I'd probably contain them together in one state and have a transition/global-transition leading to it. There are many ways you could approach this, how are you planning to instigate a throw? Do you have a central player controller FSM that monitors player input?

It's hard to tell what's really going on due to the image being quite tight, the second column for Transform Direction kinda makes it look like a screenshot on a screenshot. Probably besides the point anyway.

Wow okay I didn't realize putting multiply at the top of the stack would help but it did in this case, i just threw an object at the speed of light lmao

and yeah I had those 3 actions separated, but yeah I can see now that that isnt necessary :)

Quote
Im not sure what you mean regarding the "transition/global-transition leading to it", you mean have a in between state that only gets the players Z axis right before the throw? so Im not chewing up performance on every frame like I currently use

EDIT, I tested it and yes it works if I just add it to the unparent state that comes right before the throw state :D, thanks for all the tips, really helped me wrap my head around this and many other future actions I can apply this too
« Last Edit: March 20, 2018, 08:15:58 PM by 18LSC »

Doh

  • Full Member
  • ***
  • Posts: 124
Re: Add force to parented object only on one axis
« Reply #12 on: March 20, 2018, 10:00:58 PM »
Snazzy, glad I could help!