Random Unity Notes

Andrei Marks · February 7, 2012

http://answers.unity3d.com/questions/50757/checking-if-object-is-in-air-or-on-ground.html

http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionStay.html?from=WheelCollider

http://answers.unity3d.com/questions/136197/check-if-collider-touching-anything.html

My new go-to site. Will be doing some thorough knowledge-mining there.

http://unifycommunity.com/wiki/index.php?title=Tips

You can add components to multiple objects at the same time just by selecting all those objects. Duh.

unity make programmatic changes in editor

http://unity3d.com/support/documentation/ScriptReference/ExecuteInEditMode.html

// Make the script also execute in edit mode. @script ExecuteInEditMode()

 

This one took way too long for me to work out…

http://answers.unity3d.com/questions/50757/checking-if-object-is-in-air-or-on-ground.html

http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionStay.html?from=WheelCollider

http://answers.unity3d.com/questions/136197/check-if-collider-touching-anything.html

My new go-to site. Will be doing some thorough knowledge-mining there.

http://unifycommunity.com/wiki/index.php?title=Tips

You can add components to multiple objects at the same time just by selecting all those objects. Duh.

unity make programmatic changes in editor

http://unity3d.com/support/documentation/ScriptReference/ExecuteInEditMode.html

// Make the script also execute in edit mode. @script ExecuteInEditMode()

// Just a simple script that looks at the target transform. var target : Transform; function Update () { if (target) transform.LookAt(target); }

  • http://answers.unity3d.com/questions/28064/get-mouse-position-on-object.html
    • You would likely want to raycast. You could use ScreenToWorldPoint and build your own ray or you could just use raycast with ScreenPointToRay to save yourself the effort.How you choose to raycast is up to you. You can use a physics raycast which requires you have a collider on the object you are casting against and that it not be on the ignoreRaycast layer or you could raycast against the collider explicitly or you could raycast against the plane itself which is computationally somewhat cheaper, but only negligibly so.
      
      //Using physics
      var hit : RaycastHit; if(Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), hit))
      //we hit
      
      //Raycast against a specific collider (plane is a gameObject or Transform)
       if(plane.collider.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), hit))
        //we hit
      //hit.Point would be the point in the world where the ray hit.
      
      //Raycast against the plane itself (plane is a Plane)  var enter : float; if(plane.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition)))
      //we hit
      
      //enter gives us the distance along the ray, so  //mainCamera.ScreenPointToRay(Input.mousePosition).getPoint(enter);
      //would give us the point in the world where the ray hit. 
      You say you want the point on the plane. For most purposes, having the world position is enough. If you actually need to know a point relative to the plane, then, assuming that your plane's transform pivot is properly centered and aligned, you would convert to object space and get the point relative to the plane.
      
      //This is the position relative to the center of the plane. 
      //We should only need to care about the x and y coordinates as z should be 0.
      var localPoint : Vector3 = plane.transform.InverseTransformPoint(hit.point);
      
      Do as you like with that. If you need coordinates in some other system relative to the plane's scale or something, the calculation is straightforward.

// Just a simple script that looks at the target transform. var target : Transform; function Update () { if (target) transform.LookAt(target); }

  • http://answers.unity3d.com/questions/28064/get-mouse-position-on-object.html
    • You would likely want to raycast. You could use ScreenToWorldPoint and build your own ray or you could just use raycast with ScreenPointToRay to save yourself the effort.How you choose to raycast is up to you. You can use a physics raycast which requires you have a collider on the object you are casting against and that it not be on the ignoreRaycast layer or you could raycast against the collider explicitly or you could raycast against the plane itself which is computationally somewhat cheaper, but only negligibly so.
       //Using physics var hit : RaycastHit; if(Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), hit)) //we hit //Raycast against a specific collider (plane is a gameObject or Transform) if(plane.collider.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), hit))  //we hit //hit.Point would be the point in the world where the ray hit. //Raycast against the plane itself (plane is a Plane)  var enter : float; if(plane.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition))) //we hit //enter gives us the distance along the ray, so //mainCamera.ScreenPointToRay(Input.mousePosition).getPoint(enter); //would give us the point in the world where the ray hit. 
      You say you want the point on the plane. For most purposes, having the world position is enough. If you actually need to know a point relative to the plane, then, assuming that your plane's transform pivot is properly centered and aligned, you would convert to object space and get the point relative to the plane.
      //This is the position relative to the center of the plane.  //We should only need to care about the x and y coordinates as z should be 0. var localPoint : Vector3 = plane.transform.InverseTransformPoint(hit.point); 
      Do as you like with that. If you need coordinates in some other system relative to the plane's scale or something, the calculation is straightforward.

Twitter, Facebook