本文整理汇总了C#中Ray类的典型用法代码示例。如果您正苦于以下问题:C# Ray类的具体用法?C# Ray怎么用?C# Ray使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Ray类属于命名空间,在下文中一共展示了Ray类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
示例16: Boomy
/// <summary>
/// Load the aim and wait for input to throw BOOMnana
/// Left clicking outside the valid area will cancel placement of the trap
/// Right clicking will cancel placement right away
/// </summary>
public IEnumerator Boomy(ThrowBoomnana ability)
{
distance = ability.distance;
Activate(true);
projector.GetComponent<Projector>().material.mainTexture = Resources.Load("Images/AimPointer") as Texture;
projector.GetComponent<Projector>().aspectRatio = ability.distance / 2;
while (!Input.GetMouseButtonDown(0) && !Input.GetMouseButtonDown(1))
yield return new WaitForFixedUpdate();
if (Input.GetMouseButtonDown(0)) { // Sometimes I haz to press twice TT-TT
if (isBehind(projector.transform.position - transform.position))
ability.Cancel();
else {
Vector3 me = doNotTouchTerrain(transform.position);
Ray ray = new Ray(me, (projector.transform.position - me).normalized);
#region Aim assist
Transform target = transform;
RaycastHit hit;
Ray rayT = GetComponentInChildren<Camera>().ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
if (Physics.Raycast(rayT, out hit, Mathf.Infinity)) {
PlayerStats ps = hit.collider.GetComponent<PlayerStats>();
if (ps)
target = ps.transform;
}
#endregion
ability.Throw(ray.GetPoint(distance), target);
//ability.Throw(Vector3.MoveTowards(transform.position, projector.transform.position, distance));
}
} else
ability.Cancel();
yield return new WaitForFixedUpdate(); // Makes sure you don't activate anything else when you click
Activate(false);
}
开发者ID:PeWiNi,项目名称:MonguinsAndBeyond,代码行数:37,代码来源:Aim.cs
示例17: Update
// Update is called once per frame
void Update()
{
Ray playerRay = new Ray (transform.position + new Vector3(0f, 30, 0f), transform.forward);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast (playerRay, out hit, 100f)) {
//npcTextPrefab.text = "LOL";
Debug.DrawRay ( playerRay.origin, playerRay.direction * hit.distance, Color.blue);
//if the item the raycast is hitting the TV
if (hit.transform.gameObject.tag == "TV") {
//if player has object required to interact with TV then...
if (girlItems.ContainsKey ("Scissors")) {
if (Input.GetKeyDown (KeyCode.G)) {
Destroy (hit.transform.gameObject);
Destroy (transform.Find ("Scissors").gameObject);
girlItems.Remove ("Scissors");
//Destroy
}
}
}
if ( hit.transform.gameObject.tag == "toilet" ) {
if (girlItems.ContainsKey("Cake")) {
if ( Input.GetKeyDown (KeyCode.E) ) {
Destroy (hit.transform.gameObject);
Destroy (transform.Find ("Cake").gameObject);
girlItems.Remove ("Cake");
}
}
}
}
}
开发者ID:neemar,项目名称:Final-Project,代码行数:31,代码来源:ObjectInteractions.cs
示例18: FixedUpdate
void FixedUpdate()
{
cooldownRemaining -= Time.deltaTime;
var player = GetPlayer();
if (player == null) { return; }
var canShoot = false;
var eyePos = gameObject.transform.position;
var playerPos = player.transform.position;
var rayDir = (playerPos - eyePos).normalized;
RaycastHit hitInfo;
if (Physics.Raycast (eyePos, rayDir, out hitInfo)) {
if (hitInfo.collider == player.GetComponent<Collider>()) {
if (Vector3.Distance(playerPos, eyePos) < maxShootDistance) {
canShoot = true;
}
}
}
if (canShoot && cooldownRemaining <= 0) {
cooldownRemaining = cooldown;
var direction = (rayDir + Random.insideUnitSphere * inaccuracy).normalized;
var gunPosition = gameObject.GetComponent<Collider>().transform.TransformPoint(gunOffset);
var startPoint = gunPosition + rayDir * 0.1f;
var ray = new Ray(startPoint, direction);
Shoot(ray, gunPosition);
}
}
开发者ID:msm-code,项目名称:PitaGore,代码行数:31,代码来源:ShootsToEnemyUnits.cs
示例19: Update
// Update is called once per frame
void Update()
{
// Walking & Straffing Movement
if (Input.GetButton ("Forward")) {
transform.Translate(Vector3.forward * movementSpeed * Time.deltaTime);
}
if (Input.GetButton ("Back")) {
transform.Translate(Vector3.back * movementSpeed * Time.deltaTime);
}
if (Input.GetButton ("Left")) {
transform.Translate(Vector3.left * movementSpeed * Time.deltaTime);
}
if (Input.GetButton ("Right")) {
transform.Translate(Vector3.right * movementSpeed * Time.deltaTime);
}
// Camera View Movement
transform.Rotate(Vector3.up, Input.GetAxis ("Mouse X") * viewSensitivity, Space.World);
playerHead.transform.Rotate(Vector3.left, Input.GetAxis ("Mouse Y") * viewSensitivity, Space.Self);
// Jump Movement
Debug.DrawRay(transform.position,Vector3.down);
if (Input.GetButtonDown ("Jump")){
Ray ray = new Ray(transform.position, Vector3.down);
if(Physics.Raycast(ray,1)){
rigidbody.AddForce(Vector3.up * jumpForce);
}
}
}
开发者ID:Nerd-LoungeDev,项目名称:DefenseSystem,代码行数:30,代码来源:PlayerControll.cs
示例20: Shoot
void Shoot(Ray ray, Vector3 gunPosition)
{
Vector3 hitPoint;
RaycastHit info;
if (Physics.Raycast (ray, out info)) {
hitPoint = info.point;
ray = new Ray (gunPosition, hitPoint - gunPosition);
if (Physics.Raycast (ray, out info)) {
if (info.collider == null) { return; }
GameObject obj = info.collider.gameObject;
hitPoint = info.point;
var hp = obj.GetComponent<HasHealth> ();
if (hp != null) {
if (info.collider.tag == "Player") {
hp.ReceiveDamage (this.damage);
}
} else {
if (debrisPrefab != null) {
Instantiate (debrisPrefab, hitPoint, Quaternion.identity);
}
}
} else { return; }
} else { return; }
if (bulletTracePrefab != null) {
var bulletTrace = (GameObject)Instantiate(bulletTracePrefab);
var lineComponent = bulletTrace.GetComponent<LineRenderer>();
lineComponent.SetVertexCount(2);
lineComponent.SetPosition(0, gunPosition);
lineComponent.SetPosition(1, hitPoint);
}
}
开发者ID:msm-code,项目名称:PitaGore,代码行数:34,代码来源:ShootsToEnemyUnits.cs
注:本文中的Ray类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论