playMaker

Author Topic: Add Force at position  (Read 3470 times)

JellyHair

  • Playmaker Newbie
  • *
  • Posts: 16
Add Force at position
« on: July 31, 2015, 11:35:50 AM »
Imagine that you have a boat with an outboard motor. By using the motor you can apply force to the rear of the boat. If the force is forward then the boat with go forward. If you turn the engine and the force is now coming from the x axis the boat will rotate.

I am trying to replicate this using playmaker and the Add Force action, however, when I apply force to the X axis my boat does not rotate it just moves along the X axis. It does this even though I have specified the position of the force to be the 'engine'.

I've attached screenshots of how I have the action set up and a picture of how I would expect it to behave.

Any ideas as to what I'm doing wrong?

P.S. My setup works fine when I just use a script and AddForceAtPosition but I'd really like to get it to work in Playmaker
Code: [Select]
BoatRigidbody.AddForceAtPosition(-EngineTransform.right * ThrustPower, EngineTransform.position)

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Add Force at position
« Reply #1 on: July 31, 2015, 01:53:36 PM »
Hi,
just thinking out loud :)

i think you need to use a rotate action,
but you need to set a game object on the top side from the boat and make it a parent from the boat and rotate from there.


JellyHair

  • Playmaker Newbie
  • *
  • Posts: 16
Re: Add Force at position
« Reply #2 on: July 31, 2015, 03:39:34 PM »
Thanks for the suggestion djaydino
 
That would certainly work to turn the boat but wouldn't get the effect I want. The final control scheme will have you able to rotate the 'engine' just like you would an outboard motor to control the boat. But this won't work if the force isn't actually coming from the position of the engine.

I guess a simplification of my question is:

"Why doesn't the 'At position' option in the Add Force action seem to do anything?"
« Last Edit: July 31, 2015, 03:42:19 PM by JellyHair »

dudebxl

  • Hero Member
  • *****
  • Posts: 602
Re: Add Force at position
« Reply #3 on: August 01, 2015, 05:10:31 AM »
Hi,

Why don't you post ur script and maybe someone can convert it to an action (and share with the community).

 :)

JellyHair

  • Playmaker Newbie
  • *
  • Posts: 16
Re: Add Force at position
« Reply #4 on: August 01, 2015, 07:25:40 AM »
There's not a huge amount more to the script other than adding in the rotation for the engine :) but here you go.

Code: [Select]
using UnityEngine;
using System.Collections;

public class BoatControl : MonoBehaviour {

public Rigidbody BoatRigidbody;
public Transform BoatTransform;

//Engine orientation variables
public Transform EngineTransform;
public Vector3 EngineLeftVector;
public Vector3 EngineRightVector;
public float EngineRotateSpeed = 0.2f;

//Engine thrusting Variables
public float ThrustPower = 1;


// Use this for initialization
void Start ()
{


}

// Update is called once per frame
void Update ()
{

if(Input.GetKey(KeyCode.LeftArrow))
{
RotateEngineRight();
}

if(Input.GetKey(KeyCode.RightArrow))
{
RotateEngineLeft();
}




}

void FixedUpdate ()
{
if(Input.GetKey(KeyCode.Space))
{
EngineThrust();
}



}

void RotateEngineLeft()
{
EngineTransform.localEulerAngles = EngineTransform.localEulerAngles + (EngineLeftVector * EngineRotateSpeed);
}

void RotateEngineRight()
{
EngineTransform.localEulerAngles = EngineTransform.localEulerAngles + (EngineRightVector * EngineRotateSpeed);
}

void EngineThrust ()
{
BoatRigidbody.AddForceAtPosition(EngineTransform.forward * ThrustPower, EngineTransform.position);
}

}

This would make a pretty specific action so I'd be surprised if it got much use. You can see from the script that it would make sense that the AddForce action we already have should be able to take care of the thrusting part.

I feel like this is one of those 'right under your nose' things where I'm just not understanding how to use the Add Force action properly...  ::)

JellyHair

  • Playmaker Newbie
  • *
  • Posts: 16
Re: Add Force at position
« Reply #5 on: August 01, 2015, 11:21:00 AM »
Talking of custom actions made me think to actually take a look at the Add Force action myself!

It looks like the reason it is not working is because it only takes the 'Position' into account if you set it to 'world' space and not 'local' (self):

Code: [Select]
if (space == Space.World)
{
if (!atPosition.IsNone)
{
rigidbody.AddForceAtPosition(force, atPosition.Value, forceMode);
}
else
{
rigidbody.AddForce(force, forceMode);
}
}
else
{
rigidbody.AddRelativeForce(force,forceMode);
}

Looks like I might need to either adapt this action or make my own... Hmm down the rabbit hole we go...