- Home /
Question by
khellstr · Sep 30, 2013 at 08:46 AM ·
worldspacelocallocalposition
How to aply force to parent gameObject using childs local space coordinates?
Hi I'm using empty gameObjects to simulate water drag force in boat. Force is bigger in boats width direction than length, and its depended on speed of that empty GO. ( not on boats speed because they can be different when boat rotates). I got this script to work, but I use dirty trick to get force vector to point right direction.
// This script is in few gameobject that are children of boat GO
// I Use it to simulate drag, so its bigger in sideways
var boatRigidBody : Rigidbody;
var dragX : float; // this are used to adjust drag difference
var dragY : float;
private var dragForceX: Vector3;
private var dragForceY: Vector3;
private var previousFramePosition : Vector3;
private var speed : Vector3;
private var speedLocal : Vector3;
function FixedUpdate () {
// Have to calculate sped this way because this has not rigidBody
speed = (transform.position - previousFramePosition) / Time.deltaTime;
// speed to local coordianates
speedLocal = transform.InverseTransformDirection(speed);
// ****************************************
// this dirty hack I did to get Force vector to point right direction
dragForceX = transform.right*-speedLocal.x*dragX ;
dragForceY = transform.forward*-speedLocal.y*dragY ;
// *****************************************
// Apply forces
boatRigidBody.AddForceAtPosition( dragForceX , transform.position);
boatRigidBody.AddForceAtPosition( dragForceY , transform.position);
previousFramePosition = transform.position;
}
What would be right way to get vector from world space to local, ( and then back to world). That script does what I want, but I don't like those "hacks".
Comment
Your answer
Follow this Question
Related Questions
Move object relative to its axis 2 Answers
How to get the world position of the edge of an object? 2 Answers
World Space Coordinations are different ? 1 Answer
Class member Vector3 based on local transform 1 Answer
Get child position in world space 4 Answers