Comments and answers for "Move GameObject along a line according to the players look direction"
http://answers.unity.com/questions/241778/move-gameobject-along-a-line-according-to-the-play.html
The latest comments and answers for the question "Move GameObject along a line according to the players look direction"Comment by Kmulla on Kmulla's answer
http://answers.unity.com/comments/242149/view.html
Thank you so much! This works exactly as intended, and it's so simple!Thu, 19 Apr 2012 13:30:35 GMTKmullaAnswer by Bunny83
http://answers.unity.com/answers/242119/view.html
You can also use the [Plane object][1] so you have an infinite large "collider" plane.
A <a href="http://en.wikipedia.org/wiki/Plane_%28geometry%29">mathematical plane</a> is defined by it's normal vector and the distance from 0,0,0. You can create a plane be hand in the normal vector to define in which direction the plane should face and any point in world coordinates that is on the plane:
private Plane m_Plane;
void Start()
{
// This plane will be at 0,0,20 and face towards the origin(0,0,0)
m_Plane = new Plane(-Vector3.forward, new Vector3(0,0,20));
}
//[...]
Ray ray = Camera.main.ViewportPointToRay (new Vector3(0.5,0.5,0));
float dist;
if (m_Plane.Raycast(ray, out dist))
{
Vector3 hitPoint = ray.GetPoint(dist);
// use it
}
The plane object isn't a visual component. It's just the mathematical representation of a plane.
[1]: http://unity3d.com/support/documentation/ScriptReference/Plane.htmlThu, 19 Apr 2012 11:44:42 GMTBunny83Comment by Kmulla on Kmulla's answer
http://answers.unity.com/comments/242077/view.html
This solution needs a few adjustments to work, like locking the x-axis as well :)
When I tried it out, the box kept moving in the direction the player was looking, and I couldn't think of a quick solution to make it stop at the wanted place, but I'm sure it would work with additional coding! … nevertheless kingk614's solution is the answer to this problem AND another I wasn't looking forward to. But thanks anyway :)Thu, 19 Apr 2012 10:00:54 GMTKmullaComment by Kmulla on Kmulla's answer
http://answers.unity.com/comments/242074/view.html
This is the solution! Since there's a place where the line isn't straight, this would work great there too. I wasn't even aware that the Raycast had a point-function. Thank you :)
… And it's a great excuse for finally learning how to use the layer masks!Thu, 19 Apr 2012 09:54:15 GMTKmullaAnswer by Tseng
http://answers.unity.com/answers/241854/view.html
Just use the camera transforms forward direction and zero out the y axis. that's important because the camera always "looks down" on the player, but you don't want to move in that direction (cause you'd go through the ground :P)
Vector3 forward = Camera.main.transform.foward;
forward.y = 0f;
// Move the box
transform.Translate(forward * speed * Time.deltaTime);Wed, 18 Apr 2012 20:54:53 GMTTsengAnswer by TheDarkVoid
http://answers.unity.com/answers/241835/view.html
you can use a ray cast hitting an invisible collier which will act as your line and place the gameObject at the raycast contact point.
see: http://unity3d.com/support/documentation/ScriptReference/RaycastHit-point.html
and: http://unity3d.com/support/documentation/ScriptReference/RaycastHit.html
for helpWed, 18 Apr 2012 20:33:45 GMTTheDarkVoid