- Home /
Rigidbody add force relative to any rotation
Hi, currently my main camera holds the cube in front of it, and as I look around the cube rotates as i am turning. My script now only will shoot the rigidbody straight on the right mouse click if the camera is in such a position that the cube is rotated straight, aligned with the camera (player). How can i make this work when i look at any direction and the cube is turned in any rotation as i look and then I can press right mouse to shoot it forward in that direction? In my fixed update i currently am using transform.forward, which seems to only shoot it correctly when the rotation is near 0. Thanks!
var currentObject : GameObject;
var spawnTo : GameObject;
var objectHeld : boolean = false;
var force : float = 40.0f;
@HideInInspector
var lerpPosition : float = 0.0f;
@HideInInspector
var lerpTime : float = 2.0f;
function Update (){
if (Input.GetMouseButtonDown(0)){
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit, 8)){
if (hit.rigidbody){
currentObject = hit.rigidbody.gameObject;
objectHeld = true;
}
}
}
if (!Input.GetMouseButton(0)){
objectHeld = false;
lerpPosition = 0.0;
}
if (objectHeld){
lerpPosition += Time.deltaTime/lerpTime;
currentObject.transform.position = Vector3.Lerp(currentObject.transform.position, spawnTo.transform.position, lerpPosition);
currentObject.rigidbody.useGravity = false;
currentObject.rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezePositionZ;
}
if (!objectHeld){
currentObject.rigidbody.useGravity = true;
currentObject.rigidbody.constraints = RigidbodyConstraints.None;
}
}
function FixedUpdate ( ) {
if (Input.GetMouseButtonDown(1) && objectHeld){
objectHeld = false;
currentObject.rigidbody.AddRelativeForce(transform.forward * force);
}
}
Answer by sparkzbarca · May 23, 2013 at 01:04 AM
addrelativeforce adds forces that shoots in it's local space.
basically
transform.forward == that objects version of forward
but you don't want that to shoot straight out.
you dont give a damn what the object thinks is forward. forward is what the camera thinks is forward
if this script is attached to the camera simply use addforce instead of addrelativeforce.
if this is not
currentObject.rigidbody.AddForce(camera.main.transform.forward * force)
mark as answered if your happy witht he answer
Hi, thank you but i have already tried using Camera.main.transform.forward and it still doesnt fix the issue. :(
oh wait your doing a couple wierd things here
first off you can't use input in fixedupdate.
Secondly you shouldn't do the if(!objectheld) in update you should just do it when you release the update because it won't run between the release and the addforce
input only works in the frame your in. the update your in.
fixed update only happens every so many updates.
to use fixed update you need to do.
void update()
{
if (intput.getmousebutton(0))
{
mousedown = true;
}
}
void fixedupdate()
{
if(mousedown)
{
//do something;
}
}
because mouse down can happen between fixedupdates and be missed
do not use addrelativeforce use addforce relative is in the objects space. add force is in global space.
since you don't want to be in the objects space you want to be in the camera space you just move relative to world space and give the camera's world space to addforce
also because you freeze position in z in you need to release BEFORE YOU ADD FORC$$anonymous$$
you can't add force in that direction and get movement if the constrint is there
if your code you have
if (mouse down)
object held = false;
add force;
release constraint, because that update runs seperatly your not in that scope it'll run after this
what you need is
if(mouse down)
{
objectheld = false
release constraint
addfoce
}