- Home /
Z-axis relative to an object.
I want to drop an item in front of me, but right now the item gets dropped on the world z axis, and not the character z axis (forward). How do I get the z-axis from the player to be the the z axis that is used?
This is the important part of my script:
void ItemDrop() {
CarryableObject.transform.parent = null;
CarryableObject.transform.position = new Vector3(transform.localPosition.x, transform.localPosition.y, transform.localPosition.z + OffsetDropPosition);
CarryableObject.transform.rotation = Character.transform.rotation;
CarryableObject.GetComponent<Rigidbody>().useGravity = true;
ItemPickedUp = false;
if(Handle != null){
Handle.GetComponent<Collider>().enabled = true;
}
if(HandleExtra != null){
HandleExtra.GetComponent<Collider>().enabled = true;
}
if(BladeHolder != null){
BladeHolder.GetComponent<Collider>().enabled = true;
}
if(Blade != null){
Blade.GetComponent<Collider>().enabled = true;
}
}
Answer by Kristinosis · May 17, 2017 at 05:40 AM
If you want to drop the item in front of the character, I don't see why you couldn't just do this:
CarryableObject.transform.position = Character.transform.position + Character.transform.forward
This of course assumes that the character's blue axis is pointing forward. You could also maybe add some sort of variable so you can control how far in front of the character it is.
Answer by ErikHallmarkDev · May 16, 2017 at 09:41 PM
I would suggest setting up a vector relative to your player, and multipling that by the worldToLocalMatrix on your players transform.
so instead your code would look something like this:
CarryableObject.transform.position = Vector3.forward * OffsetDropPosition * transform.worldToLocalMatrix;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Can I use the local x,y transforms of a plane? 1 Answer
Get Local Mouse Position from the center of the Gameobject clicked on 2 Answers
Constrain local X movement 2 Answers