Maintaining GameObject Rotation When Throwing

Good Afternoon/Evening/Morning … Night :slight_smile:

I’m still relatively new to the whole Unity scene, and have quite been enjoying all the latest in agonising moments that come from a certain lack of knowledge. I’ve managed to design a system that’ll allow the player (first-person controller) to pick up and rotate a specific GameObject, and that’s all working perfectly fine.

Only an issue arises when I attempt to implement a throw/drop feature. As I’m attempting to implement the ability to throw the GameObject directly forwards, whilst it continues to maintain it’s local rotation. As currently all that’s occurring is that’ll shoot forwards in which ever way it’s local vectors are facing, rather than the vectors of the player.

I’ve looked at a few other projectile/throw methods. Though they all seem to apparently cater towards objects that have a set rotation, rather than local rotation that’ll conceivably be different every time. So any advice/personal thoughts or cold-hearted insults would be much appreciated.

Many Thanks,

I’ll leave the insults for someone else. :slight_smile: So, if I understand, you basically want to make an object fly forward from the player’s (i.e. the camera’s) perspective, even if that object has been rotated in its own local space. You will need to translate (or add force to) the object in world coordinates and use the camera’s forward vector for the force. Something like this I think would work:

cubeObj.transform.Translate(Camera.main.transform.forward * yourVelocity * Time.deltaTime, Space.World);

Of course this will only move the cube forward in one frame, but it gets the idea across. You could reduce velocity each frame and continue calling it, and you could also apply velocity to all 3 components (if you wanted to simulate gravity for instance). You could do a similar call to rigidBody.addForce() if you’re using the physics. Use Time.deltaTime if you put the code in the Update() method and Time.fixedDeltaTime if you put it in the FixedUpdate() method.