# AddForce at Local position and Rotate Around Local Y Axis

To better understand my problem i will first describe what i want to do.

Objective:
I want to make a drone with four motors that add downward force. The motors will be controlled by input from the gamepad.
The LeftStick forward/backward will control all motors up/down.
The LeftStick left/right will control the rotation around the vertical axis left/right.
The RightStick left/right/forward/backward control the tilt left/right/forward/backward.

My implementation:

• Motors are represented by AddForceAtPosition(force, point) where point is position of drone ± offset (offset indicates if motor is FrontRight, FrontLeft, RearRight, RearLeft)
• Moving LeftStick forward/backward adds force using AddForceAtPosition at all 4 motor positions
-Moving RightStick left adds force using AddForceAtPosition to 2 motors on Right side
-Moving RightStick right adds force using AddForceAtPosition to 2 motors on Left side
-Moving RightStick forward adds force using AddForceAtPosition to 2 motors on Rear side
-Moving RightStick backward adds force using AddForceAtPosition to 2 motors on Front side

The problem:
First lets begin with what works (or i think it works as i want):

• flying up and down using LeftStick(forward/backward)
• rotating to the left and right using LeftStick(left/right)
• tilting forward/backward/left/right using RightStick

What dont work:

• when i rotate drone using LeftStick(left/right) and then move RightStick forward, the drone tilts towards world forward axis instead of the drones forward axis.

So my question is, how can i make the drone tilt in directions of its axis instead of the world axis ?
And I also want to ask if the other functions (which i think work as i want) really work as i want or if it is just a coincidence.

My code:

``````using UnityEngine;
using UnityEngine.InputSystem;

public class Motors : MonoBehaviour
{
public Rigidbody rb;
public float strength = 10f;
public float strengthRotations = 5f;
public float rotationSpeed = 10f;
public float maxSpeed = 50f;

// Update
void FixedUpdate()
{
if (rb.velocity.magnitude > maxSpeed)
{
rb.velocity = rb.velocity.normalized * maxSpeed;
}

}

void LeftStickInputActions(float InputThrottle, float InputRotation)
{
Vector3 FrontLeft = GetPosition(1.0f, 1.0f);
rb.AddForceAtPosition(strength * rb.transform.up * InputThrottle, FrontLeft);
Vector3 FrontRight = GetPosition(1.0f, -1.0f);
rb.AddForceAtPosition(strength * rb.transform.up * InputThrottle, FrontRight);
Vector3 RearLeft = GetPosition(-1.0f, 1.0f);
rb.AddForceAtPosition(strength * rb.transform.up * InputThrottle, RearLeft);
Vector3 RearRight = GetPosition(-1.0f, -1.0f);
rb.AddForceAtPosition(strength * rb.transform.up * InputThrottle, RearRight);

}

void RightStickInputActions(float InputDrive, float InputSteer)
{

if (InputDrive > 0)
{
Vector3 RearRight = GetPosition(-1.0f, -1.0f);
rb.AddForceAtPosition(strengthRotations * rb.transform.up * InputSteer * -1, RearRight);
Vector3 FrontRight = GetPosition(1.0f, -1.0f);
rb.AddForceAtPosition(strengthRotations * rb.transform.up * InputSteer * -1, FrontRight);
}
if(InputDrive < 0)
{
Vector3 RearLeft = GetPosition(-1.0f, 1.0f);
rb.AddForceAtPosition(strengthRotations * rb.transform.up * InputSteer, RearLeft);
Vector3 FrontLeft = GetPosition(1.0f, 1.0f);
rb.AddForceAtPosition(strengthRotations * rb.transform.up * InputSteer, FrontLeft);
}
if (InputSteer > 0)
{
Vector3 FrontLeft = GetPosition(1.0f, 1.0f);
rb.AddForceAtPosition(strengthRotations * rb.transform.up * InputDrive * -1, FrontLeft);
Vector3 FrontRight = GetPosition(1.0f, -1.0f);
rb.AddForceAtPosition(strengthRotations * rb.transform.up * InputDrive * -1, FrontRight);
}
if (InputSteer < 0)
{
Vector3 RearRight = GetPosition(-1.0f, -1.0f);
rb.AddForceAtPosition(strengthRotations * rb.transform.up * InputDrive, RearRight);
Vector3 RearLeft = GetPosition(-1.0f, 1.0f);
rb.AddForceAtPosition(strengthRotations * rb.transform.up * InputDrive, RearLeft);
}
rb.angularVelocity = Vector3.zero;
rb.freezeRotation = true;
rb.freezeRotation = false;
}

Vector3 GetPosition(float offsetX, float offsetZ)
{
Vector3 position = rb.transform.position;
position.x += offsetX;
position.z += offsetZ;
return position;
}
}
``````

Using an offset is kind of flawed since that assumes the drone has no rotation. You’d have to rotate the offsets based on its current rotation.

Why not parent empty GameObjects on the position of each propeller? By doing that, you could refer to their Transforms in your script and use their positions in AddForceAtPosition().

And again, don’t overwrite velocities and angular velocities since that erases existing physics information about the object. If you need limits, simply saturate the propeller speeds or forces using Mathf.Clamp().

You should use AddTorque() instead since you have transform.up inside of AddRelativeTorque. Other than that, not seeing other issues in the code.