What does this unity3d c# code mean? -
ray mouseray = camera.main.screenpointtoray(input.mouseposition); vector3 newdotposition = mouseray.origin - mouseray.direction / mouseray.direction.y * mouseray.origin.y;
i see newdotposition gameobject's position @ terrain. following code do?
mouseray.direction / mouseray.direction.y * mouseray.origin.y
actually it's basic geometry.
mouseray.origin
camera's world position. mouseray.origin.y
camera's height.
result of mouseray.direction / mouseray.direction.y * mouseray.origin.y
red vector in image below. scales mouseray.direction
way through ground.
then mouseray.origin - result
hit point on ground in world coordinates.
and here code see result if interested.
public class test : monobehaviour { public float origingizmoradius = 1f; public float newdotpositiongizmosize = 0.5f; public float directionlength = 2f; vector3 origin; vector3 direction; vector3 newdotposition; void update() { ray mouseray = camera.main.screenpointtoray(input.mouseposition); origin = mouseray.origin; direction = mouseray.direction; newdotposition = origin - direction / direction.y * origin.y; } void ondrawgizmos() { gizmos.color = color.red; gizmos.drawsphere(origin, origingizmoradius); gizmos.drawline(origin, origin + (direction.normalized * directionlength)); gizmos.color = color.green; gizmos.drawcube(newdotposition, newdotpositiongizmosize * vector3.one); } }
Comments
Post a Comment