- Home /
Use GameObject's global position instead of local position
Hello everyone, I am new in unity and I am trying to create a level myself, but I have a problem. As you see on the first picture the position of cylinder is local. On the second picture the position of the cylinder is global, I have function OnTriggerEnter in which am instantiating an arrow on the cylinder position with its rotation and throwing it. I want the arrow to be thrown to y direction in local position. But I do not know how, it is done in global(world position). If anyone knows how to throw an arrow in y direction in local scope. Here is code: var clonedArrow = Instantiate(arrow,bow.transform.position, bow.transform.rotation); clonedArrow.rigidbody.velocity = bow.transform.TransformDirection(Vector3(0,20,0));
arrow is a public variable of type GameObject from prefab(which has rigidbody already). Bow is my cylinder. Thank you beforehand
Answer by arboshiki · Jan 14, 2013 at 12:15 PM
Thank you very much, I did what I want.
please don't post comments as answers, and please click the CHEC$$anonymous$$ $$anonymous$$AR$$anonymous$$ to accept an answer if it helps you
Answer by Julien-Lynge · Jan 11, 2013 at 09:17 PM
You need to spend some time investigating the Transform class. Everything you need to access both local and global coordinates is in there, and it's named very obviously.
http://docs.unity3d.com/Documentation/ScriptReference/Transform.html
For your TransformDirection stuff, take a look at things like up, right, and forward.
Answer by Seth-Bergman · Jan 11, 2013 at 09:45 PM
First off, this
bow.transform.TransformDirection(Vector3(0,20,0));
(same thing as)
bow.transform.up * 20;
http://docs.unity3d.com/Documentation/ScriptReference/Transform-up.html
this takes the local direction and converts it to world coordinates... in other words, the direction in question is in fact already based off of the local direction..
(y being the green axis)
so this already IS LOCAL!
(just tested your code to be SURE, it already does exactly what you say you want)
also, p.s...
if an answer solves your issue (such as):
http://answers.unity3d.com/questions/368033/a-little-problem-in-partciles.html
please be sure and click the check mark by the answer to accept it!
Your answer
Follow this Question
Related Questions
How to follow multiple clones positions of an instantiate prefab having a velocity ? 1 Answer
Take final position after a force applied to a GameObject 1 Answer
How can I get a GameObject's transform in game and set that to a variable? 1 Answer
Moving object with transform.position ignore other objects even if they collided 1 Answer
How do I get the opposite position of an object that spins around another object... 0 Answers