Raycast() public method

public Raycast ( Ray ray, RaycastHit &hitInfo, float maxDistance ) : bool
ray Ray
hitInfo RaycastHit
maxDistance float
return bool
Exemplo n.º 1
0
 public static bool existsBetween(Collider collider, Triangle3 triangle)
 {
     RaycastHit info;
     return (
         collider.Raycast(new Ray(triangle.A, triangle.B - triangle.A), out info, (triangle.B - triangle.A).magnitude) ||
         collider.Raycast(new Ray(triangle.B, triangle.C - triangle.B), out info, (triangle.C - triangle.B).magnitude) ||
         collider.Raycast(new Ray(triangle.C, triangle.A - triangle.C), out info, (triangle.A - triangle.C).magnitude)
         );
 }
 public static bool collider_contains_pt( Collider test, Vector3 point)
 {
     Vector3    center;
     Vector3    direction;
     Ray        ray;
     RaycastHit hitInfo;
     bool       hit;
     center = test.bounds.center;
     direction = center - point;
     ray = new Ray(point, direction);
     hit = test.Raycast(ray, out hitInfo, direction.magnitude);
     return !hit;
 }
Exemplo n.º 3
0
    /// <summary>
    /// Returns whether the point is inside the mesh collider.
    /// </summary>
    private static bool PointIsInsideMeshCollider(Collider c, Vector3 p)
    {
        Vector3[] directions = { Vector3.up, Vector3.down, Vector3.left, Vector3.right, Vector3.forward, Vector3.back };

        foreach (var ray in directions)
        {
            RaycastHit hit;
            if (c.Raycast(new Ray(p - ray * 1000, ray), out hit, 1000f) == false)
            {
                return false;
            }
        }

        return true;
    }
Exemplo n.º 4
0
        public static bool IsInside(Collider test, Vector3 point)
        {
            Vector3 center;
            Vector3 direction;
            Ray ray;
            RaycastHit hitInfo;
            bool hit;

            // Use collider bounds to get the center of the collider. May be inaccurate
            // for some colliders (i.e. MeshCollider with a 'plane' mesh)
            center = test.bounds.center;

            // Cast a ray from point to center
            direction = center - point;
            ray = new Ray(point, direction);
            hit = test.Raycast(ray, out hitInfo, direction.magnitude);

            // If we hit the collider, point is outside. So we return !hit
            return !hit;
        }
Exemplo n.º 5
0
 public static bool ColliderLineCast(Collider collider, Vector3 src, Vector3 des, out RaycastHit hit)
 {
     return  collider.Raycast (new Ray (src, des-src), out hit, (des - src).magnitude);
 }
Exemplo n.º 6
0
 public void CheckBodyPartHit(Collider coll, Vector3 origin, Vector3 direction, float radius)
 {
     if (bulletCollision)
     {
         foreach (Transform child in shootables)
         {
             RaycastHit hit;
             Ray ray = new Ray(origin, direction.normalized);
             if (coll.Raycast(ray, out hit, radius))
             {
                 //Debug.Log(child.gameObject);
             }
         }
     }
 }
Exemplo n.º 7
0
        /// <summary>
        /// Checks if point is at most <paramref name="tolerance"/> away from the collider edge.
        /// </summary>
        /// <param name="collider">
        /// The collider to check against.
        /// </param>
        /// <param name="point">
        /// Point being checked.
        /// </param>
        /// <param name="tolerance">
        /// Maximal distance
        /// </param>
        /// <returns>
        /// <c>true</c> if the <paramref name="point"/> is inside the <paramref name="collider"/> 
        /// and at most <paramref name="tolerance"/> away from its edge, 
        /// <c>false</c> otherwise.
        /// </returns>
        public static bool IsPointAtColliderEdge(Collider collider, Vector3 point, float tolerance) {
            RaycastHit hit;

            tolerance *= 0.71f; // Approximately 1/sqrt(2)
            Vector3 direction = collider.bounds.center - point;
            Vector3 directionNormalized = direction.normalized;

            bool result = direction != Vector3.zero &&
                          collider.Raycast(
                              new Ray(point - directionNormalized * tolerance, directionNormalized),
                              out hit, tolerance
                              );

            return result;
        }
    private Vector3 GetWorldPointFromMouse(Collider obj_collider) {
		float planeLevel = 0;
        var groundPlane = new Plane(Vector3.up, new Vector3(0, planeLevel, 0));

        Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
        Vector3 hit = new Vector3(0,0,0);
        float dist;
		
		RaycastHit rayHit;
        if (obj_collider.Raycast(ray, out rayHit, Mathf.Infinity)) {
			return rayHit.point;
		} else if (groundPlane.Raycast(ray, out dist)) {
            hit = ray.origin + ray.direction.normalized * dist;
		}
        return hit;
    }
Exemplo n.º 9
0
        /// <summary>
        /// Checks whether the point is inside a collider.
        /// </summary>
        /// <remarks>
        /// This method can check against all types of colliders, 
        /// including <c>TerrainCollider</c> and concave <c>MeshCollider</c>.
        /// </remarks>
        /// <param name="collider">
        /// The collider to check against.
        /// </param>
        /// <param name="point">
        /// The point being checked.
        /// </param>
        /// <param name="specificTest">
        /// Defines a kind of specific collision test that must be done against <paramref name="collider"/>.
        /// </param>
        /// <returns>
        /// <c>true</c> if the <paramref name="point"/> is inside the <paramref name="collider"/>, 
        /// <c>false</c> otherwise.
        /// </returns>
        public static bool IsPointInsideCollider(Collider collider, Vector3 point, ColliderSpecificTestEnum specificTest = ColliderSpecificTestEnum.None) {
            RaycastHit hit;
            if (specificTest == ColliderSpecificTestEnum.None) {
#if !UNITY_FLASH
                if (collider is TerrainCollider) {
                    if (!collider.Raycast(new Ray(point, Vector3.up), out hit, collider.bounds.size.y)) {
                        return false;
                    }
                } else
#endif
                    if (collider is MeshCollider && !((MeshCollider) collider).convex) {
                        if (!IsPointInsideMeshCollider(collider, point)) {
                            return false;
                        }
                    } else {
                        Vector3 direction = collider.bounds.center - point;
                        float directionMagnitude = direction.sqrMagnitude;
                        if (directionMagnitude > 0.0001f &&
                            collider.Raycast(new Ray(point, direction.normalized), out hit, FastFunctions.FastSqrt(directionMagnitude))) {
                            return false;
                        }
                    }
            } else {
                if (specificTest == ColliderSpecificTestEnum.Terrain) {
                    if (!collider.Raycast(new Ray(point + Vector3.up * collider.bounds.size.y, Vector3.down), out hit, collider.bounds.size.y)) {
                        return false;
                    }
                }
            }

            return true;
        }
Exemplo n.º 10
0
        /// <summary>
        /// Checks whether the point is inside a MeshCollider.
        /// </summary>
        /// <param name="collider">
        /// Collider to check against.
        /// </param>
        /// <param name="point">
        /// Point being checked.
        /// </param>
        /// <returns>
        /// <c>true</c> if the <paramref name="point"/> is inside the <paramref name="collider"/>, 
        /// <c>false</c> otherwise.
        /// </returns>
        public static bool IsPointInsideMeshCollider(Collider collider, Vector3 point) {
            Ray rayCast = new Ray();
            for (int i = 0; i < meshColliderCheckDirections.Length; i++) {
                Vector3 dir = meshColliderCheckDirections[i];
                rayCast.origin = point - dir * 1000f;
                rayCast.direction = dir;
                RaycastHit hit;
                if (collider.Raycast(rayCast, out hit, 1000f) == false)
                {
                    return false;
                }
            }

            return true;
        }
Exemplo n.º 11
0
	static public bool PtInCollider( Collider col, Collider excludeCol, Ray ray, bool all=false)
	{
		bool ret = false;
		
		if( true == all)
		{
			RaycastHit hit = new RaycastHit();
			ret = col.Raycast( ray, out hit, 500.0f);
		}
		else
		{
			IComparer comp = new DepthComparer();
			
			RaycastHit[] hits = Physics.RaycastAll( ray);
			if( 0 == hits.Length)
				return false;
			
			Array.Sort( hits, comp);
			
			int index = 0;
			if( excludeCol == hits[0].collider)
				index = 1;
			
			ret = ( col == hits[ index].collider) ? true : false;
		}
		
		return ret;
	}
Exemplo n.º 12
0
    void UpdateToggleState(Collider collider, DiageticToggle toggle, Ray ray, Vector3 heading, Vector3 p, bool isActive, string handedness)
    {
        RaycastHit hit = new RaycastHit();

        if (toggle.state != toggle.DRAGGING && isActive && collider.Raycast(ray, out hit, 200.0f))
        { // start a drag
            toggle.state = toggle.DRAGGING;
            toggle.OnGrab();
            toggle.handUsed = handedness;

            // do things related to starting a drag
        }

        if (toggle.state == toggle.DRAGGING && toggle.handUsed == handedness)
        {

            if (!isActive)
            { // no longer dragging
                toggle.state = toggle.NORMAL;
                performToggleAction(toggle);
            }

        }
    }
Exemplo n.º 13
0
    private static void BuildBridge(Collider bridgeCollider)
    {
        //Find min and max tiles
        Tile maxTile = GetClosestTile (bridgeCollider.bounds.max);
        Tile minTile = GetClosestTile (bridgeCollider.bounds.min);

        bool leftToRight = bridgeCollider.bounds.size.x > bridgeCollider.bounds.size.z;

        int firstMinNumber, firstMaxNumber, secondMinNumber, secondMaxNumber;

        if (leftToRight)
        {
            firstMinNumber = minTile.I;
            firstMaxNumber = maxTile.I;

            secondMinNumber = minTile.J;
            secondMaxNumber = maxTile.J;
        }
        else
        {
            firstMinNumber = minTile.J;
            firstMaxNumber = maxTile.J;

            secondMinNumber = minTile.I;
            secondMaxNumber = maxTile.I;
        }

        for (int i = firstMinNumber; i<= firstMaxNumber; i++)
        {
            for (int j = secondMinNumber; j <= secondMaxNumber; j++)
            {
                Tile terrainTile;

                if (leftToRight)
                {
                    terrainTile = m_Grid[i,j];
                }
                else
                {
                    terrainTile = m_Grid[j,i];
                }

                if (j != secondMinNumber && j != secondMaxNumber)
                {
                    //Create tile and check if tile underneath is passable, we're raycasting as ClosestPointOnBounds wouldn't return the value we wanted if the bridge is sloped
                    Tile tileToAdd;

                    if (leftToRight)
                    {
                        Ray yValueRay = new Ray(m_Grid[i,j].Center+(Vector3.up*1000), Vector3.down);
                        RaycastHit hitInfo;
                        if (bridgeCollider.Raycast (yValueRay, out hitInfo, Mathf.Infinity))
                        {
                            tileToAdd = new Tile(i, j, new Vector3(m_Grid[i,j].Center.x, hitInfo.point.y, m_Grid[i,j].Center.z), true, false, bridgeCollider);
                        }
                        else
                        {
                            //We didn't hit the collider, that means it's either an entrance or exit, now we have to use ClosestPointOnBounds instead
                            tileToAdd = new Tile(i, j, new Vector3(m_Grid[i,j].Center.x, bridgeCollider.ClosestPointOnBounds (m_Grid[i,j].Center+(Vector3.up*10)).y, m_Grid[i,j].Center.z), true, false, bridgeCollider);
                        }
                    }
                    else
                    {
                        Ray yValueRay = new Ray(m_Grid[j,i].Center+(Vector3.up*1000), Vector3.down);
                        RaycastHit hitInfo;
                        if (bridgeCollider.Raycast (yValueRay, out hitInfo, Mathf.Infinity))
                        {
                            tileToAdd = new Tile(j, i, new Vector3(m_Grid[j,i].Center.x, hitInfo.point.y, m_Grid[j,i].Center.z), true, false, bridgeCollider);
                        }
                        else
                        {
                            tileToAdd = new Tile(j, i, new Vector3(m_Grid[j,i].Center.x, bridgeCollider.ClosestPointOnBounds (m_Grid[j,i].Center+(Vector3.up*10)).y, m_Grid[j,i].Center.z), true, false, bridgeCollider);
                        }
                    }

                    if (i == firstMinNumber || i == firstMaxNumber)
                    {
                        tileToAdd.BridgeTunnelEntrance = true;
                    }

                    terrainTile.LayeredTiles.Add(tileToAdd);
                }

                //Raycast to find height (but beware the bridge might not cover the center point)
                //Since the bridge might not be over the center point, we'll raycast in 3 places
                Vector3 startPoint = terrainTile.Center+(Vector3.down*5);
                Ray rayCenter = new Ray(startPoint, Vector3.up);
                Ray ray1;
                Ray ray2;
                RaycastHit hit;

                if (leftToRight)
                {
                    //ray1 wants to be above, ray2 below
                    Vector3 zHalfTileSize = new Vector3(0, 0, TileSize/2.0f);
                    ray1 = new Ray(startPoint + zHalfTileSize, Vector3.up);
                    ray2 = new Ray(startPoint - zHalfTileSize, Vector3.up);
                }
                else
                {
                    //ray1 wants to be to the left, ray2 to the right
                    Vector3 xHalfTileSize = new Vector3(TileSize/2.0f, 0, 0);
                    ray1 = new Ray(startPoint - xHalfTileSize, Vector3.up);
                    ray2 = new Ray(startPoint + xHalfTileSize, Vector3.up);
                }

                float heightVal;
                if (bridgeCollider.Raycast (rayCenter, out hit, Mathf.Infinity))
                {
                    heightVal = hit.point.y;
                }
                else if (bridgeCollider.Raycast (ray1, out hit, Mathf.Infinity))
                {
                    heightVal = hit.point.y;
                }
                else if (bridgeCollider.Raycast (ray2, out hit, Mathf.Infinity))
                {
                    heightVal = hit.point.y;
                }
                else
                {
                    //We haven't hit any of our points, so we must an entrance/exit tile, set it to blocked
                    terrainTile.Status = Const.TILE_Blocked;
                    continue;
                }

                if (Mathf.Abs (heightVal - terrainTile.Center.y) < PassableHeight)
                {
                    terrainTile.Status = Const.TILE_Blocked;
                }
            }
        }
    }
Exemplo n.º 14
0
    protected void StartGrabbing(Kinect.Body body)
    {
        Debug.Log ("Start Grabbing");
        //Kinect.Joint sourceJoint = body.Joints [Kinect.JointType.HandLeft];
        Vector3 leftHandCoordinate = GetVector3FromJoint (body.Joints [Kinect.JointType.HandLeft]);
        Vector3 rightHandCoordinate = GetVector3FromJoint (body.Joints [Kinect.JointType.HandRight]);
        Vector3 curGrabPosition = (leftHandCoordinate + rightHandCoordinate) / 2.0f;

        activeObject = GrabSearch(leftHandCoordinate, rightHandCoordinate);
        //Debug.Log ("Grab Position: " + curGrabPosition);

        if (activeObject == null) {
            Debug.Log("No Active Object");
            return;
        }
        Holdable_object holdable = activeObject.GetComponent<Holdable_object>();
        grab_offset = Vector3.zero;

        if(holdable == null){
            Vector3 delta_position = activeObject.transform.position -curGrabPosition;
            Ray grab_ray = new Ray (curGrabPosition, delta_position);
            RaycastHit grab_hit;

            if(activeObject.Raycast(grab_ray, out grab_hit, grabObjectDistance))
                grab_offset = activeObject.transform.position - grab_hit.point;
            else
                grab_offset = activeObject.transform.position - curGrabPosition;
        }
        //wow... Quaternion

        if (holdable != null) {
            holdable.OnGrab();
        }
    }
    RaycastHit GetHit(Collider colliderToHit)
    {
        Vector3 position = transform.position;
        Vector3 direction = colliderToHit.transform.position - position;
        Ray ray = new Ray(position, direction);
        RaycastHit hit;

        colliderToHit.Raycast(ray, out hit, Mathf.Infinity);

        if (Application.isEditor)
        {
            Debug.DrawRay(ray.origin, ray.direction, Color.magenta, 0.1F);
            Debug.DrawRay(hit.point, hit.normal, Color.yellow, 0.1F);
        }

        return hit;
    }
Exemplo n.º 16
0
	// Use this for initialization
	void Start ()
	{
		//float volume = mesh.bounds.size.x * mesh.bounds.size.y * mesh.bounds.size.z;
		Mesh mesh = GetComponent<MeshFilter> ().mesh;
		Bounds bounds = mesh.bounds;
		int[] triangles = mesh.triangles;
		coll = GetComponent<Collider> ();

		//Debug.Log ("xSize: "+bounds.size.x/xSize);

		Vector3 v3Center = bounds.center;
		Vector3 v3Extents = bounds.extents;
		cubeSize = 0.4f;
		float halfCube = cubeSize / 2;

		v3FrontTopLeft = new Vector3 (v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z);  // Front top left corner
		v3FrontTopRight = new Vector3 (v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z);  // Front top right corner
		v3FrontBottomLeft = new Vector3 (v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z);  // Front bottom left corner
		v3FrontBottomRight = new Vector3 (v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z);  // Front bottom right corner
		v3BackTopLeft = new Vector3 (v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z);  // Back top left corner
		v3BackTopRight = new Vector3 (v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z);  // Back top right corner
		v3BackBottomLeft = new Vector3 (v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z);  // Back bottom left corner
		v3BackBottomRight = new Vector3 (v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z);  // Back bottom right corner
		
		v3FrontTopLeft = transform.TransformPoint (v3FrontTopLeft);
		v3FrontTopRight = transform.TransformPoint (v3FrontTopRight);
		v3FrontBottomLeft = transform.TransformPoint (v3FrontBottomLeft);
		v3FrontBottomRight = transform.TransformPoint (v3FrontBottomRight);
		v3BackTopLeft = transform.TransformPoint (v3BackTopLeft);
		v3BackTopRight = transform.TransformPoint (v3BackTopRight);
		v3BackBottomLeft = transform.TransformPoint (v3BackBottomLeft);
		v3BackBottomRight = transform.TransformPoint (v3BackBottomRight); 

		sp = new Vector3 (v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z);  // Front bottom left corner
		variablepoint = new Vector3 (v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z);  // Front bottom left corner
		GameObject cube = GameObject.CreatePrimitive (PrimitiveType.Cube);
		sp = transform.TransformPoint (sp);
		variablepoint = transform.TransformPoint (variablepoint);
		cube.transform.localScale = new Vector3 (cubeSize, cubeSize, cubeSize);
		cube.transform.position = sp;

		int xSize = (int)Math.Ceiling ((v3BackBottomRight.x - v3FrontBottomLeft.x) / cubeSize) + 2;
		int ySize = (int)Math.Ceiling ((v3FrontTopLeft.y - v3FrontBottomLeft.y) / cubeSize) + 2;
		int zSize = (int)Math.Ceiling ((v3BackBottomLeft.z - v3FrontBottomLeft.z) / cubeSize) + 2;

		//bool inside = false;
		for (int y = 0; y <= ySize; y++) {
			for (int x = 0; x <= xSize; x++) {
				for (int z = 0; z <= zSize; z++) {
					sp.x += x * cubeSize; // sp = startingposition
					sp.y += y * cubeSize;
					sp.z += z * cubeSize;

					// Cast Rays from the right side top and bottom to the right
					Vector3 vert1 = new Vector3 (sp.x - halfCube, sp.y - halfCube, sp.z - halfCube); // 1
					Vector3 vert2 = new Vector3 (sp.x - halfCube, sp.y + halfCube, sp.z - halfCube); // 2
					Vector3 vert3 = new Vector3 (sp.x + halfCube, sp.y + halfCube, sp.z - halfCube); // 3
					Vector3 vert4 = new Vector3 (sp.x + halfCube, sp.y - halfCube, sp.z - halfCube); // 4
					Vector3 vert5 = new Vector3 (sp.x + halfCube, sp.y - halfCube, sp.z + halfCube); // 5
					Vector3 vert6 = new Vector3 (sp.x + halfCube, sp.y + halfCube, sp.z + halfCube); // 6
					Vector3 vert7 = new Vector3 (sp.x - halfCube, sp.y + halfCube, sp.z + halfCube); // 7
					Vector3 vert8 = new Vector3 (sp.x - halfCube, sp.y - halfCube, sp.z + halfCube); // 8

					// Rays to the right
					Ray rayTopRight = new Ray (vert2, Vector3.right);
					bool rayCastTopRight = coll.Raycast (rayTopRight, out rayhitinfoun, cubeSize);

					Ray rayTopLeft = new Ray (vert7, Vector3.right);
					bool rayCastTopLeft = coll.Raycast (rayTopLeft, out rayhitinfoun, cubeSize);

					Ray rayBotRight = new Ray (vert1, Vector3.right);
					bool rayCastBotRight = coll.Raycast (rayBotRight, out rayhitinfoun, cubeSize);

					Ray rayBotLeft = new Ray (vert8, Vector3.right);
					bool rayCastBotLeft = coll.Raycast (rayBotLeft, out rayhitinfoun, cubeSize);

					// Left 3 4 5 6
					Ray rayTopRightrev = new Ray (vert3, Vector3.left);
					bool rayCastTopRightrev = coll.Raycast (rayTopRightrev, out rayhitinfoun, cubeSize);
					
					Ray rayTopLeftrev = new Ray (vert6, Vector3.left);
					bool rayCastTopLeftrev = coll.Raycast (rayTopLeft, out rayhitinfoun, cubeSize);
					
					Ray rayBotRightrev = new Ray (vert4, Vector3.left);
					bool rayCastBotRightrev = coll.Raycast (rayBotRightrev, out rayhitinfoun, cubeSize);
					
					Ray rayBotLeftrev = new Ray (vert5, Vector3.left);
					bool rayCastBotLeftrev = coll.Raycast (rayBotLeftrev, out rayhitinfoun, cubeSize);

					// Forward, z direction
					Ray ray1forw = new Ray (vert1, Vector3.forward);
					bool cast1forw = coll.Raycast (ray1forw, out rayhitinfoun, cubeSize);

					Ray ray2forw = new Ray (vert2, Vector3.forward);
					bool cast2forw = coll.Raycast (ray2forw, out rayhitinfoun, cubeSize);

					Ray ray3forw = new Ray (vert3, Vector3.forward);
					bool cast3forw = coll.Raycast (ray3forw, out rayhitinfoun, cubeSize);

					Ray ray4forw = new Ray (vert4, Vector3.forward);
					bool cast4forw = coll.Raycast (ray4forw, out rayhitinfoun, cubeSize);

					// backward,z direction, 5 6 7 8
					Ray ray5back = new Ray (vert5, Vector3.back);
					bool cast5back = coll.Raycast (ray5back, out rayhitinfoun, cubeSize);

					Ray ray6back = new Ray (vert6, Vector3.back);
					bool cast6back = coll.Raycast (ray6back, out rayhitinfoun, cubeSize);
					
					Ray ray7back = new Ray (vert7, Vector3.back);
					bool cast7back = coll.Raycast (ray7back, out rayhitinfoun, cubeSize);

					Ray ray8back = new Ray (vert8, Vector3.back);
					bool cast8back = coll.Raycast (ray8back, out rayhitinfoun, cubeSize);

					// RayCast Down: 2 3 6 7
					Ray ray2down = new Ray (vert2, Vector3.down);
					bool cast2down = coll.Raycast (ray2down, out rayhitinfoun, cubeSize);

					Ray ray3down = new Ray (vert3, Vector3.down);
					bool cast3down = coll.Raycast (ray3down, out rayhitinfoun, cubeSize);

					Ray ray6down = new Ray (vert6, Vector3.down);
					bool cast6down = coll.Raycast (ray6down, out rayhitinfoun, cubeSize);

					Ray ray7down = new Ray (vert7, Vector3.down);
					bool cast7down = coll.Raycast (ray7down, out rayhitinfoun, cubeSize);

					// RayCast UP
					// bottom facees to cast ray up, 1, 4, 5 and 8
					Ray ray1up = new Ray (vert1, Vector3.up);
					bool cast1up = coll.Raycast (ray1up, out rayhitinfoun, cubeSize);

					Ray ray4up = new Ray (vert4, Vector3.up);
					bool cast4up = coll.Raycast (ray4up, out rayhitinfoun, cubeSize);
					
					Ray ray5up = new Ray (vert5, Vector3.up);
					bool cast5up = coll.Raycast (ray5up, out rayhitinfoun, cubeSize);

					Ray ray8up = new Ray (vert8, Vector3.up);
					bool cast8up = coll.Raycast (ray8up, out rayhitinfoun, cubeSize);

					if (cast8up | cast5up | cast4up | cast1up | cast7down | cast6down | cast3down | cast2down | cast5back | cast6back | cast7back | cast8back | cast1forw | cast2forw | cast3forw | cast4forw | rayCastTopRight | rayCastTopLeft | rayCastBotRight | rayCastBotLeft | rayCastTopRightrev | rayCastTopLeftrev | rayCastBotRightrev | rayCastBotLeftrev) {
						GameObject cubedeux = GameObject.CreatePrimitive (PrimitiveType.Cube);
						cubedeux.transform.localScale = new Vector3 (cubeSize, cubeSize, cubeSize);
						cubedeux.transform.position = sp;
					}
					sp = variablepoint; // sp = startingposition

				}
			}
		}
	}
    //------------------------------
    //ダメージを受ける / Suffer damage
    //------------------------------
    private void Damage(Collider collider)
    {
        damageFrame = 0;
        animator.SetTrigger("isDamageTrigger");
        GameController.isCanInput = false;
        horizontalVelocity = 0.0f;
        verticalVelocity = 0.0f;

        //衝突方向を取得 / Get the collision direction
        Ray ray = new Ray(transform.position, collider.bounds.center - transform.position);
        RaycastHit hit;
        if (collider.Raycast(ray, out hit, 100.0f)) {
            hitNormal = hit.normal;
        }

        //SE
        soundDamage[balloonNum].Play();

        //風船を割る / break a balloon
        if (balloonNum > 0) {
            balloonNum --;
        }
        ChangeBalloonColor();
    }
Exemplo n.º 18
0
 private static bool InternalIsTouchingFromCollider(Camera cam, Collider collider, bool usePreviousPosition)
 {
     Ray ray = cam.ScreenPointToRay(GetTouchPosition(usePreviousPosition));
     RaycastHit hit = new RaycastHit();
     return collider.Raycast(ray, out hit, 1000.0f);
 }
Exemplo n.º 19
0
 private static bool PointInsideMeshCollider(Collider c, Vector3 p)
 {
     Vector3[] directions = Buoyancy._directions;
     for (int i = 0; i < directions.Length; i++)
     {
         Vector3 vector = directions[i];
         RaycastHit raycastHit;
         if (!c.Raycast(new Ray(p - vector * 1000f, vector), out raycastHit, 1000f))
         {
             return false;
         }
     }
     return true;
 }
Exemplo n.º 20
0
	static public bool PtInCollider( Camera cam, Collider col, Vector2 pt, bool all=false)
	{
		bool ret = false;
		
		if( true == all)
		{
			Ray ray = cam.ScreenPointToRay( new Vector3( pt.x, pt.y, 0.0f));
			RaycastHit hit = new RaycastHit();
			ret = col.Raycast( ray, out hit, 500.0f);
		}
		else
		{
			IComparer comp = new DepthComparer();
			
			Ray ray = cam.ScreenPointToRay( new Vector3( pt.x, pt.y, 0.0f));
			RaycastHit[] hits = Physics.RaycastAll( ray);
			if( 0 == hits.Length)
				return false;
			
			Array.Sort( hits, comp);

			ret = ( col == hits[0].collider) ? true : false;
		}
		
		return ret;
	}
Exemplo n.º 21
0
        /// <summary>
        /// Performs a collider raycast (this is likely more efficient than doing a dumb raycast
        /// and checking to see if any of the hit objects are the one you're looking for) to determine
        /// if the user is looking at this collider.
        /// </summary>
        /// <param name="col">The collider to check against the user's gaze.</param>
        /// <returns>Whether or not the referenced collider is being looked at.</returns>
        public static bool IsLookingAtCollider(Collider col)
        {
            CheckDataConcurrency();
            if (_eyeRayLeft.origin == _eyeRayLeft.direction)
            {
                return false;
            }
            RaycastHit hit;
            if (col.Raycast(_eyeRayLeft, out hit, 1000))
            {
                return true;
            }

            // Right eye disabled for now
            //		if (col.Raycast (_eyeRayRight, out hit, 1000))
            //		{
            //			return true;
            //		}

            return false;
        }
Exemplo n.º 22
0
    void Update()
    {
        if ( surface )
        {
            col = surface.GetComponent<Collider>();

            if ( col )
            {
                Vector3 pos = transform.position;

                if ( pos != lastpos )
                {
                    lastpos = pos;
                    RaycastHit hit;
                    Ray ray = new Ray(pos, Vector3.down);

                    if ( col.Raycast(ray, out hit, 20.0f) )
                    {
                        if ( col is BoxCollider )
                        {
                            Vector3 p = surface.transform.worldToLocalMatrix.MultiplyPoint(hit.point);
                            BoxCollider bc = (BoxCollider)col;
                            if ( bc.size.x != 0.0f )
                                p.x /= bc.size.x;

                            if ( bc.size.y != 0.0f )
                                p.y /= bc.size.y;

                            if ( bc.size.z != 0.0f )
                                p.z /= bc.size.z;

                            p.x += 0.5f;
                            p.y += 0.5f;
                            p.z += 0.5f;

                            float column = 0.0f;
                            float row = 0.0f;

                            switch ( surface.axis )
                            {
                                case MegaAxis.X:
                                    column = (p.y) * (surface.cols - 1);
                                    row = p.z * (surface.rows - 1);
                                    break;

                                case MegaAxis.Y:
                                    column = (p.x) * (surface.cols - 1);
                                    row = p.z * (surface.rows - 1);
                                    break;

                                case MegaAxis.Z:
                                    column = (p.x) * (surface.cols - 1);
                                    row = p.y * (surface.rows - 1);
                                    break;
                            }

                            if ( lastdown )
                                surface.Line(lastcol, lastrow, column, row, -force);
                            else
                                surface.wakeAtPointAdd1((int)column, (int)row, -force);

                            lastdown = true;
                            lastrow = row;
                            lastcol = column;
                            return;
                        }
                        else
                        {
                            float column = (1.0f - hit.textureCoord.x) * (surface.cols - 1);
                            float row = hit.textureCoord.y * (surface.rows - 1);

                            if ( lastdown )
                                surface.Line(lastcol, lastrow, column, row, -force);
                            else
                                surface.wakeAtPointAdd1((int)column, (int)row, -force);

                            lastdown = true;
                            lastrow = row;
                            lastcol = column;
                        }
                    }
                    else
                        lastdown = false;
                }
                else
                    lastdown = false;
            }
            else
                lastdown = false;
        }
    }
Exemplo n.º 23
0
    void UpdateSliderState(Collider collider, DiageticSlider slider, Ray ray, Vector3 heading, Vector3 p, bool isActive, string handedness)
    {
        // updating for both hands is screwing it up

        RaycastHit hit = new RaycastHit();

        if (slider.state != slider.DRAGGING && isActive && collider.Raycast(ray, out hit, 200.0f))
        { // start a drag
            slider.state = slider.DRAGGING;
            slider.OnGrab();
            slider.handUsed = handedness;

            // do things related to starting a drag
        }

        if (slider.state == slider.DRAGGING && slider.handUsed == handedness)
        {

            Vector3 perp = Vector3.Cross(heading, (playerCamera.transform.position - slider.transform.position));
            float dir = Vector3.Dot(perp, playerCamera.transform.up);

            if (dir > 0f)
            {
                slider.transform.localPosition = new Vector3(slider.transform.localPosition.x + SLIDER_MOVE_SPEED, slider.transform.localPosition.y, slider.transform.localPosition.z);
            }
            else if (dir < 0f)
            {
                slider.transform.localPosition = new Vector3(slider.transform.localPosition.x - SLIDER_MOVE_SPEED, slider.transform.localPosition.y, slider.transform.localPosition.z);
            }
            else
            {
                //Debug.Log("ontarget");
            }
            if (slider.UpdateBarValue())
            { // value changed
                performSliderAction(slider.sliderType, slider.currentValue);
            }
            if (!isActive)
            { // no longer dragging
                NeutralizeSliderState(slider);
            }

        }
    }