当前位置: 首页>>代码示例>>C#>>正文


C# Ray类代码示例

本文整理汇总了C#中Ray的典型用法代码示例。如果您正苦于以下问题:C# Ray类的具体用法?C# Ray怎么用?C# Ray使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


Ray类属于命名空间,在下文中一共展示了Ray类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DoClick

 private void DoClick(object sender, ClickedEventArgs e)
 {
     if (this.teleportOnClick)
     {
         float y = this.reference.position.y;
         Plane plane = new Plane(Vector3.up, -y);
         Ray ray = new Ray(base.transform.position, base.transform.forward);
         bool flag = false;
         float d = 0f;
         if (this.teleportType == SteamVR_Teleporter.TeleportType.TeleportTypeUseCollider)
         {
             TerrainCollider component = Terrain.activeTerrain.GetComponent<TerrainCollider>();
             RaycastHit raycastHit;
             flag = component.Raycast(ray, out raycastHit, 1000f);
             d = raycastHit.distance;
         }
         else if (this.teleportType == SteamVR_Teleporter.TeleportType.TeleportTypeUseCollider)
         {
             RaycastHit raycastHit2;
             Physics.Raycast(ray, out raycastHit2);
             d = raycastHit2.distance;
         }
         else
         {
             flag = plane.Raycast(ray, out d);
         }
         if (flag)
         {
             Vector3 position = ray.origin + ray.direction * d - new Vector3(this.reference.GetChild(0).localPosition.x, 0f, this.reference.GetChild(0).localPosition.z);
             this.reference.position = position;
         }
     }
 }
开发者ID:GameDiffs,项目名称:TheForest,代码行数:33,代码来源:SteamVR_Teleporter.cs

示例2: FindClosestHitObject

    Transform FindClosestHitObject(Ray ray, out Vector3 hitPoint)
    {
        RaycastHit[] hits = Physics.RaycastAll(ray);

        Transform closestHit = null;
        float distance = 0;
        hitPoint = Vector3.zero;

        foreach(RaycastHit hit in hits) {
            if(hit.transform != this.transform && ( closestHit==null || hit.distance < distance ) ) {
                // We have hit something that is:
                // a) not us
                // b) the first thing we hit (that is not us)
                // c) or, if not b, is at least closer than the previous closest thing

                closestHit = hit.transform;
                distance = hit.distance;
                hitPoint = hit.point;
            }
        }

        // closestHit is now either still null (i.e. we hit nothing) OR it contains the closest thing that is a valid thing to hit

        return closestHit;
    }
开发者ID:clemvarois,项目名称:FPS_Unity3D,代码行数:25,代码来源:PlayerShooting.cs

示例3: FixedUpdate

    private float origDist; ///< Start distance between camera and lookat-target

    #endregion Fields

    #region Methods

    void FixedUpdate()
    {
        // Camera collision
        Vector3 d = transform.position - m_target.position;
        Ray ray = new Ray();
        float rayDist = d.magnitude;
        ray.direction = d.normalized; // all rays share direction
        // Cast several rays to avoid clipping in corners
        // and to be able to determine ignore-cases(such as small debree)
        numHits = 0;
        for (int x=-rayGridBorderDim;x<rayGridBorderDim+1;x++)
        for (int y=-rayGridBorderDim;y<rayGridBorderDim+1;y++)
        {
            // Create a ray on the grid
            ray.origin = m_target.position + transform.TransformDirection(new Vector3(x,y,0.0f)) * rayGridStepVal;
            if (Physics.Raycast(ray, out hit, rayDist))
            {
                if (hit.distance < closestHitDist)
                {
                    closestHitDist = Mathf.Max(minCamHitAdjustDistance, hit.distance); // camera can not move further than the minimum adjust distance
                    hitOccuredCooldown = 1.0f; // reset cooldown tick
                    numHits++;
                    Debug.DrawLine(ray.origin, ray.origin + ray.direction * hit.distance, Color.white);   // considered ray
                }
                else
                    Debug.DrawLine(ray.origin, ray.origin + ray.direction * hit.distance, Color.yellow);  // non-considered hit ray
            }
            else
                Debug.DrawLine(ray.origin, ray.origin + ray.direction * rayDist, Color.red);              // non hit ray
        }
    }
开发者ID:jarllarsson,项目名称:PA2505-Stort-Spelprojekt-Prototyp,代码行数:37,代码来源:CameraBehaviour.cs

示例4: FindPath

	bool FindPath(Vector3 target, Vector3 current, List<Vector3> steps, int n) {
		if (n > 100)
			return false;
		if (Vector3.Distance(current, target) < 1f) {
			return true;
		} else {
			List<Node> nodes = new List<Node>();

			for (int i = 0; i < testSteps.Length; i++) {
				Vector3 nextStep = current + testSteps[i] * stepLenght;

				Ray ray = new Ray(nextStep + Vector3.up, Vector3.down);
				RaycastHit hit;
				if (Physics.Raycast(ray, out hit, 1)) {
					Debug.DrawRay(nextStep, Vector3.up, Color.red, 30);
				} else {
					nodes.Add(new Node(Vector3.Distance(nextStep, target), nextStep));
					Debug.DrawRay(nextStep, Vector3.up, Color.green, 30);
				}
			}

			nodes.Sort(delegate(Node a, Node b) {
				return a.distance.CompareTo(b.distance);
			});

			for (int i = 0; i < nodes.Count; i++) {
				bool reachedTarget = FindPath(target, nodes[i].position, steps, n + 1);
				if (reachedTarget)
					steps.Add(target);
			
				return reachedTarget;
			}
		}
		return false;
	}
开发者ID:Nunsense,项目名称:Cuackeddon,代码行数:35,代码来源:PathFinder.cs

示例5: MoveTo

 private void MoveTo()
 {
     if (target != lastTarget)
       {
           if ((transform.position - target).sqrMagnitude > heightPlayer + 0.1f)
           {
               if (!walk)
               {
     //              animation.CrossFade(a_Walk.name);
                   walk = true;
               }
               mag = (transform.position - target).magnitude;
               transform.position = Vector3.MoveTowards(transform.position, target, mag > stopStart ? speed * UnityEngine.Time.deltaTime : Mathf.Lerp(speed * 0.5f, speed, mag / stopStart) * UnityEngine.Time.deltaTime);
               ray = new Ray(transform.position, Vector3.up);
               if (Physics.Raycast(ray, out hit, 1000.0f))
               {
                 transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
               }
           }
           else
           {
               lastTarget = target;
               if (walk)
               {
     //              animation.CrossFade(a_Idle.name);
                   walk = false;
               }
           }
       }
 }
开发者ID:Shipaaaa,项目名称:TestVuf,代码行数:30,代码来源:drag.cs

示例6: button

    IEnumerator button()
    {
        RaycastHit hit;
        while (true) {
            mouseRay = transform.parent.camera.ScreenPointToRay(Input.mousePosition);
            Debug.DrawRay (mouseRay.origin, mouseRay.direction*50,Color.blue,1);

            if(Physics.Raycast(mouseRay, out hit,50)){
                if(hit.collider.name == "button"){
                    if(continueButton.gameObject.GetComponent<glow>() == null){
                        continueButton.gameObject.AddComponent<glow>();
                    }
                    onButton = true;
                }
            }else{
                if(continueButton.gameObject.GetComponent<glow>() != null){
                    continueButton.gameObject.GetComponent<glow>().noMore();
                }
                onButton = false;
            }
            if(onButton && canInput){
                if(Input.GetMouseButtonDown(0)){
                    textIndex++;
                }
            }

            continueButton.enabled = canInput;
            if(continueButton.enabled){
                continueButton.transform.Rotate(0,1,0);
            }
            yield return null;
        }
    }
开发者ID:ryand626,项目名称:Genetics,代码行数:33,代码来源:testGui.cs

示例7: FixedUpdate

    //Fixed Update is called every __ seconds, Edit Project Settings Time Fixed TimeStamp
    //Movement
    void FixedUpdate()
    {
        if (Input.GetKey (KeyCode.UpArrow)){
            GetComponent<Rigidbody>().AddForce(GetComponent<Transform>().forward *2f, ForceMode.VelocityChange);
        }

        if (Input.GetKey (KeyCode.LeftArrow)) {
            GetComponent<Rigidbody>().AddForce(GetComponent<Transform>().right *2f, ForceMode.VelocityChange);
            //transform.Rotate(new Vector3(0f,-5f,0f));
        }
        if (Input.GetKey (KeyCode.RightArrow)) {
            GetComponent<Rigidbody>().AddForce(GetComponent<Transform>().right *-2f, ForceMode.VelocityChange);
            //transform.Rotate(new Vector3(0f,5f,0f));
        }

        //transform.position = GetComponent<Transform>
        Ray ray = new Ray (transform.position, -Vector3.up);
        //to know where and what the raycast hit , we have to store that impact info
        RaycastHit rayHit = new RaycastHit (); //Blank Container for Info

        if (Physics.Raycast (ray, out rayHit, 1.1f))
        {
            if (Input.GetKey (KeyCode.RightShift))
            {
                GetComponent<Rigidbody> ().AddForce (new Vector3 (0f, 5000f, 0f), ForceMode.Acceleration);
                Debug.Log (rayHit.point);
                //Destroy (rayHit.collider.gameObject);
            }
        }
    }
开发者ID:hejohnny,项目名称:FourSquare,代码行数:32,代码来源:Player2Controller.cs

示例8: Shoot

	public void Shoot()
	{
		if(CanShoot() && !Pause.pause)
		{
			Ray ray = new Ray(spawn.position,spawn.forward);
			RaycastHit hit;
			Instantiate (smoke, smokespawn.transform.position, spawn.transform.rotation);
			float shotDistance = 20;

			if (Physics.Raycast(ray,out hit, shotDistance))
			{
				shotDistance = hit.distance;
				
				if (hit.collider.tag == "Zombie")
				{
					GameObject go = hit.collider.gameObject;
					mAI other = (mAI)go.GetComponent (typeof(mAI));
					other.hurt();
					Instantiate (blood, hit.transform.position, transform.rotation);
				}
			}

			nextShootTime = Time.time + secondsInterval;

			audio.Play();

			StartCoroutine("RenderTracer", ray.direction * shotDistance);
		}
	}
开发者ID:sybiload,项目名称:0xyde,代码行数:29,代码来源:Gun.cs

示例9: Update

    void Update()
    {
        if(Input.GetMouseButtonDown( 0 ))
        {
            if(cam)
            {
                ray = cam.ScreenPointToRay( Input.mousePosition );
            }else{
                ray = Camera.main.ScreenPointToRay( Input.mousePosition );
            }

            RaycastHit rayHit = new RaycastHit();

            if( Physics.Raycast(ray, out rayHit, 100f) )
            {
                if(rayHit.transform.gameObject.layer == 11)
                {
                    rayHit.transform.gameObject.GetComponent<LoadSceneButton>().StartLoading();

                }else if( rayHit.transform.gameObject.layer == 12)
                {
                    rayHit.transform.gameObject.GetComponent<OtherButton>().SetClicked();
                    rayHit.transform.gameObject.GetComponent<OtherButton>().Execute();
                }
            }
        }
    }
开发者ID:ramsesoriginal,项目名称:Bzzt,代码行数:27,代码来源:ClickBehaviour.cs

示例10: FixedUpdate

    // Update is called once per frame
    void FixedUpdate()
    {
        Vector3 directionToMouse = mouse.position - transform.position;
        float angle = Vector3.Angle(transform.forward, directionToMouse);

        if (angle < 90f) {

            Ray catRay = new Ray (transform.position, directionToMouse);
            RaycastHit catRayHitInfo = new RaycastHit();

            if (Physics.Raycast (catRay, out catRayHitInfo, 100f))
            {
                if(catRayHitInfo.collider.tag == "Mouse" )
                {
                    failed.Play();
                    if(catRayHitInfo.distance <= 5)
                {
                        arrow.Play();
                        Destroy (mouse.gameObject);
                }
                    else{

                        rbody.AddForce (directionToMouse.normalized * 1000f);

                    }
                }
            }
        }
    }
开发者ID:aaronconpollo,项目名称:CatNMouse,代码行数:30,代码来源:Cat.cs

示例11: AddRope

    IEnumerator AddRope()
    {
        Ray axis;
        GameObject temp;

        temp = GameObject.CreatePrimitive(PrimitiveType.Capsule);
        temp.transform.localScale = new Vector3 (0.2f, 0.1f, 0.2f);
        temp.AddComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationY;
        if (rope.Count == 0)
        {
            temp.transform.position = this.transform.position + 10 * Vector3.up;
            temp.transform.rotation = Quaternion.Euler(new Vector3(0, 0, 90));
            rope.Add(temp.transform);
        }
        else if (rope.Count == 1)
        {
            axis = new Ray(rope[rope.Count - 1].position, Vector3.right);
            temp.transform.position = axis.GetPoint (0.1f);
            temp.transform.rotation = Quaternion.Euler(new Vector3(0, 0, 90));
            temp.AddComponent<HingeJoint>().connectedBody = rope[rope.Count - 1].gameObject.rigidbody;
            rope.Add(temp.transform);
        }
        else
        {
            axis = new Ray(rope[rope.Count - 1].position, rope[rope.Count - 1].position - rope[rope.Count - 2].position );
            temp.transform.position = axis.GetPoint (0.1f);
            temp.transform.rotation = rope[rope.Count-1].transform.rotation;
            temp.AddComponent<HingeJoint>().connectedBody = rope[rope.Count - 1].gameObject.rigidbody;
            rope.Add(temp.transform);
        }
        yield return new WaitForSeconds(.01f);
    }
开发者ID:shamushand,项目名称:GHNBgame1,代码行数:32,代码来源:RopeShooter.cs

示例12: FixedUpdate

    private void FixedUpdate()
    {
        if(enemy.dead) return;

        // raycasting
        float facing = (transform.localScale.x < 0f ? -1f : 1f);
        Vector2 origin = (Vector2)eye.position + offset*facing;
        Vector2 direction = Vector2.right * transform.localScale.x;
        Ray ray = new Ray(origin, direction);

        RaycastHit2D hit;
        hit = Physics2D.Raycast(ray.origin, ray.direction, visionDistance);
        Debug.DrawRay(ray.origin, ray.direction.normalized * visionDistance, Color.yellow);

        if(hit != null && hit.collider != null) {
            Player player = null;
            if (hit.collider.gameObject.layer == LayerMask.NameToLayer("Player"))
                player = hit.collider.GetComponent<Player>();

            if (hit.collider.gameObject.layer == LayerMask.NameToLayer("Weapon"))
                player = hit.collider.transform.parent.GetComponent<Player>();

                if(player != null)
                    player.Die();
        }
    }
开发者ID:RSMGameJam,项目名称:super-metal-bros,代码行数:26,代码来源:EnemyVision.cs

示例13: checkIfBeingBlocked

    bool checkIfBeingBlocked(Vector3 position)
    {
        //2 rays are needed incase the enemy is inside a collider

        bool forwardCheck = false;
        bool backwardCheck = false;

        Vector3 direction = target.position - position;
        float distance = Mathf.Sqrt((direction.x * direction.x) + (direction.y * direction.y) + (direction.z * direction.z));
        distance += 1; //error

        Ray ray = new Ray (position, direction);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, distance, layerMask)) { //send a ray forwards from the enemy
            if (hit.collider.tag == "Player")
                forwardCheck = false;
            else
                forwardCheck = true;
        }

        ray.origin = ray.GetPoint(distance - 1);
        ray.direction = -ray.direction;
        if (Physics.Raycast(ray, out hit, distance, layerMask)) { //send another ray backwards from the player
            if (hit.collider.tag == "Enemy")
                backwardCheck = false;
            else
                backwardCheck = true;
        }

        if (forwardCheck || backwardCheck)
            return true;
        else
            return false;
    }
开发者ID:artboy77,项目名称:Golem,代码行数:34,代码来源:MagneticBaller.cs

示例14: GroundCheck

    void GroundCheck()
    {
        RaycastHit check;
        Ray downRay = new Ray (transform.position, -transform.up);

        if (Physics.Raycast (downRay, out check)) {
            Debug.DrawLine (transform.position,check.point);
            if (check.distance <= (charCollider.height/2)+.5f)  {
                if (check.collider.tag == "Death"){
                    infoScript.Kill();
                }
                if(check.collider.tag == "win"){
                //	infoScript.camScript.win = true;
                }
                grounded = true;
                groundPos = check;
                frictionCoefficent = initialFrictionCoefficent;
            }else{
                grounded = false;
                frictionCoefficent = airFrictionCoefficent;
            }
        } else{
            frictionCoefficent = airFrictionCoefficent;
            grounded = false;
        }
    }
开发者ID:spencerHarrill,项目名称:UnityThirdPersonTut,代码行数:26,代码来源:PlayerMovement.cs

示例15: AddBulletAndHit

        public bool AddBulletAndHit(VanillaCharacter source, Vector3 decalage, VanillaCharacter target)
        {
            Vector3 sourcePoint = source.FeetPosition + (source.Size.y / 2) * Vector3.UNIT_Y + decalage;
            Vector3 targetPoint = target.FeetPosition + target.Size.y / 2 * Vector3.UNIT_Y;
            Vector3 diff = targetPoint - sourcePoint;
            if (Math.Abs(diff.y / Cst.CUBE_SIDE) > 6) { return false; }

            Degree pitch = -Math.ATan2(diff.y,  diff.z);
            Degree yaw = source.GetYaw();
            if (yaw.ValueAngleUnits > 90 && yaw.ValueAngleUnits < 270)
            {
                yaw *= -1;
                yaw += new Degree(180);
            }

            float targetDistance = diff.Length;
            Ray ray = new Ray(sourcePoint + Vector3.NEGATIVE_UNIT_Z, diff.NormalisedCopy);
            float blockDistance = VanillaBlock.getBlockOnRay(source.getIsland(), ray, Bullet.DEFAULT_RANGE, 30);
            if (targetDistance > blockDistance) { return false; }

            SceneNode pitchNode = this.SceneMgr.RootSceneNode.CreateChildSceneNode();
            pitchNode.Position = sourcePoint;
            pitchNode.Pitch(pitch);

            SceneNode yawNode = pitchNode.CreateChildSceneNode();
            yawNode.Yaw(yaw);
            yawNode.AttachObject(StaticRectangle.CreateLine(this.SceneMgr, Vector3.ZERO, new Vector3(0, 0, 15), ColoredMaterials.YELLOW));

            this.mBullets.Add(new Bullet(source, target, pitchNode, yawNode));
            return true;
        }
开发者ID:RenaudWasTaken,项目名称:SkyLands,代码行数:31,代码来源:BulletManager.cs


注:本文中的Ray类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。