// ============================================================================= // METHODS STATIC -------------------------------------------------------------- public static void SetVanishingPoint( Camera cam, float offset, ClientCameraScreen screen ) { Transform t = cam.transform; NearPlane plane = new NearPlane(); Vector3 nearCenter = t.position + t.forward*cam.nearClipPlane; Plane nearPlane = new Plane ( -t.forward, nearCenter ); float distance = 0f; Vector3 direction; Ray ray; Vector3 screenTL = t.TransformPoint ( new Vector3 ( ( -screen.Width/2.0f ) + offset, screen.Height/2.0f, screen.Distance ) ); direction = ( screenTL - t.position ).normalized; ray = new Ray ( t.position, direction ); nearPlane.Raycast ( ray, out distance ); Vector3 nearTL = -( t.InverseTransformPoint ( nearCenter ) - t.InverseTransformPoint ( ( t.position + direction*distance ) ) ); Vector3 screenBR = t.TransformPoint ( new Vector3 ( ( screen.Width/2.0f ) + offset, -screen.Height/2.0f, screen.Distance ) ); direction = ( screenBR - t.position ).normalized; ray = new Ray ( t.position, direction ); nearPlane.Raycast ( ray, out distance ); Vector3 nearBR = -( t.InverseTransformPoint ( nearCenter ) - t.InverseTransformPoint ( ( t.position + direction*distance ) ) ); plane.left = nearTL.x; plane.top = nearTL.y; plane.right = nearBR.x; plane.bottom = nearBR.y; plane.near = cam.nearClipPlane; plane.far = cam.farClipPlane; cam.projectionMatrix = PerspectiveOffCenter ( plane ); }
void Update() { // Movement Input Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); Vector3 moveVelocity = moveInput.normalized * moveSpeed; pc.Move(moveVelocity); // Look/Aim Input Ray ray = cam.ScreenPointToRay(Input.mousePosition); Plane groundPlane = new Plane(Vector3.up, Vector3.zero); float rayDist; if(groundPlane.Raycast(ray, out rayDist)) { Vector3 point = ray.GetPoint(rayDist); Debug.DrawLine(ray.origin, point, Color.red); pc.LookAt(point); } // Shooting Input if(Input.GetMouseButton(0)) { gc.Shoot(); } }
void Update () { // Movement input Vector3 moveInput = new Vector3 (Input.GetAxisRaw ("Horizontal"), 0, Input.GetAxisRaw ("Vertical")); Vector3 moveVelocity = moveInput.normalized * moveSpeed; controller.Move (moveVelocity); // Look input Ray ray = viewCamera.ScreenPointToRay (Input.mousePosition); Plane groundPlane = new Plane (Vector3.up, Vector3.up * gunController.GunHeight); float rayDistance; if (groundPlane.Raycast(ray,out rayDistance)) { Vector3 point = ray.GetPoint(rayDistance); //Debug.DrawLine(ray.origin,point,Color.red); controller.LookAt(point); crosshairs.transform.position = point; crosshairs.DetectTargets(ray); if ((new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude > 1) { gunController.Aim(point); } } // Weapon input if (Input.GetMouseButton(0)) { gunController.OnTriggerHold(); } if (Input.GetMouseButtonUp(0)) { gunController.OnTriggerRelease(); } if (Input.GetKeyDown (KeyCode.R)) { gunController.Reload(); } }
IEnumerator DragObject(float distance) { var oldDrag = springJoint.connectedBody.drag; var oldAngularDrag = springJoint.connectedBody.angularDrag; springJoint.connectedBody.drag = drag; springJoint.connectedBody.angularDrag = angularDrag; var mainCamera = FindCamera(); while (Input.GetMouseButton (0)) { Plane plane = new Plane(Vector3.back, constraintPlaneObject.transform.position); Ray ray = mainCamera.ScreenPointToRay (Input.mousePosition); float constraintPlaneDistance = 0.0f; plane.Raycast(ray, out constraintPlaneDistance); springJoint.transform.position = ray.GetPoint(constraintPlaneDistance); line.SetPosition(0, springJoint.transform.TransformPoint(springJoint.anchor)); line.SetPosition(1, springJoint.transform.position); yield return null; } if (springJoint.connectedBody) { springJoint.connectedBody.drag = oldDrag; springJoint.connectedBody.angularDrag = oldAngularDrag; springJoint.connectedBody.freezeRotation = false; //springJoint.connectedBody = null; Destroy(go); } }
void Update() { var plane = new Plane(Vector3.up, transform.position); var ray = Camera.main.ScreenPointToRay(Input.mousePosition); float hit; if (plane.Raycast(ray, out hit)) { var aimDirection = Vector3.Normalize(ray.GetPoint(hit) - transform.position); var targetRotation = Quaternion.LookRotation(aimDirection); transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, 360 * Time.deltaTime); if (Input.GetMouseButtonDown(0)) bulletPrefab.Spawn(gun.position, gun.rotation); } if (Input.GetKeyDown(KeyCode.Space)) { bulletPrefab.DestroyPooled(); } if (Input.GetKeyDown(KeyCode.Z)) { bulletPrefab.DestroyAll(); } }
public void OnMouseMove( dfControl control, dfMouseEventArgs args ) { if( animating || !dragging ) return; this.momentum = ( momentum + args.MoveDelta.Scale( 1, -1 ) ) * 0.5f; args.Use(); if( args.Buttons.IsSet( dfMouseButtons.Left ) ) { var ray = args.Ray; var distance = 0f; var direction = Camera.main.transform.TransformDirection( Vector3.back ); var plane = new Plane( direction, lastPosition ); plane.Raycast( ray, out distance ); var pos = ( ray.origin + ray.direction * distance ).Quantize( control.PixelsToUnits() ); var offset = pos - lastPosition; var transformPos = ( control.transform.position + offset ).Quantize( control.PixelsToUnits() ); control.transform.position = transformPos; lastPosition = pos; } }
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; } } }
void Caminar() { // Da seguimiento a la distancia entre este gameObject y posicionDestino. distanciaDestino = Vector3.Distance(posicionDestino, esteTransform.position); // Mueve al jugador si el click izquierdo del mouse fue clickeado. if (Input.GetMouseButtonDown(0) && GUIUtility.hotControl == 0) { Plane planoJugador = new Plane(Vector3.up, esteTransform.position); Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow); float longitudRayo = 0.0f; // Obtenemos la longitud del rayo. if (planoJugador.Raycast(ray, out longitudRayo)) { posicionDestino = ray.GetPoint(longitudRayo); // Coordenada destino Quaternion rotacionDestino = Quaternion.LookRotation(posicionDestino - transform.position); esteTransform.rotation = rotacionDestino; } } // Evita que el codigo se ejecute si no es necesario. if(distanciaDestino > .5f){ esteTransform.position = Vector3.MoveTowards(esteTransform.position, posicionDestino, velocidad * Time.deltaTime); } }
protected override bool Continue() { float single; float single1 = (float)(HUDIndicator.stepTime - this.startTime); if (single1 > this.curve[this.curve.length - 1].time) { return false; } this.material.Set("_AlphaValue", Mathf.Clamp(this.curve.Evaluate(single1), 0.003921569f, 0.996078432f)); if (this.followPoint) { Vector3 vector3 = base.transform.position; Vector3 point = base.GetPoint(HUDIndicator.PlacementSpace.World, this.worldPosition); if (vector3.z != point.z) { Plane plane = new Plane(-base.transform.forward, vector3); Ray ray = new Ray(point, Vector3.forward); if (!plane.Raycast(ray, out single)) { ray.direction = -ray.direction; point = (!plane.Raycast(ray, out single) ? vector3 : ray.GetPoint(single)); } else { point = ray.GetPoint(single); } } if (point != vector3) { base.transform.position = point; } } return true; }
void Update() { Plane plane = new Plane(Vector3.up, 0.0f); if (Input.GetMouseButtonDown(0)) { Ray ray = camera.ScreenPointToRay(Input.mousePosition); float distance = 0.0f; plane.Raycast(ray, out distance); mBaseAim = mAim; mBasePoint = ray.GetPoint(distance); } if (Input.GetMouseButton(0)) { SetAim(mBaseAim, mRotation, mDistance); Ray ray = camera.ScreenPointToRay(Input.mousePosition); float distance = 0.0f; plane.Raycast(ray, out distance); Vector3 newPoint = ray.GetPoint(distance); SetAim(mBaseAim + mBasePoint - newPoint, mRotation, mDistance); } else if (Input.GetMouseButton(1)) { mRotation.y = mRotation.y + 1.0f; SetAim(mAim, mRotation, mDistance); } }
// ConvertScreenToWorld // // Converts Vector3's from screen to world space - by raycasting from the passed camera // // to a plane parallel to the camera, outputs an array of 2 Vector3's // public static Vector3[] ConvertScreenToWorld( Vector3 point_a, Vector3 point_b, Vector3 position, Camera camera ) { // Convert screen touches into rays from main camera Ray ray_a = camera.ScreenPointToRay( point_a ); Ray ray_b = camera.ScreenPointToRay( point_b ); // Create a plane parallel to camera, facing the screen - then rotated by the cameras y rotation Vector3 parallel_to_camera = RotateY( new Vector3( 0, 0, -1 ), camera.transform.rotation.y ); Plane plane = new Plane( parallel_to_camera, position ); // Hit distances float hit1; float hit2; // Hit point Vector3's Vector3 hit_point_a = Vector3.zero; Vector3 hit_point_b = Vector3.zero; // Shoot ray_a at plane if( plane.Raycast( ray_a, out hit1 ) ) { // Get where ray_a collides with the plane hit_point_a = ray_a.GetPoint( hit1 ); } // Shoot ray_b at plane if( plane.Raycast( ray_b, out hit2 ) ) { // Get where ray_b collides with the plane hit_point_b = ray_b.GetPoint( hit2 ); } // Return two vectors as array return new Vector3[ 2 ] { hit_point_a, hit_point_b }; }
public static void DrawLineBetweenWorldPositions(Vector3 worldPosA, Vector3 worldPosB, float width, Color color) { Camera cam = GetMainCamera(); GUI.matrix = Matrix4x4.identity; bool aBehind = false; Plane clipPlane = new Plane(cam.transform.forward, cam.transform.position + cam.transform.forward * 0.05f); if(Vector3.Dot(cam.transform.forward, worldPosA-cam.transform.position) < 0) { Ray ray = new Ray(worldPosB, worldPosA - worldPosB); float dist; if(clipPlane.Raycast(ray, out dist)) { worldPosA = ray.GetPoint(dist); } aBehind = true; } if(Vector3.Dot(cam.transform.forward, worldPosB-cam.transform.position) < 0) { if(aBehind) return; Ray ray = new Ray(worldPosA, worldPosB - worldPosA); float dist; if(clipPlane.Raycast(ray, out dist)) { worldPosB = ray.GetPoint(dist); } } Vector3 screenPosA = cam.WorldToViewportPoint(worldPosA); screenPosA.x = screenPosA.x*Screen.width; screenPosA.y = (1-screenPosA.y)*Screen.height; Vector3 screenPosB = cam.WorldToViewportPoint(worldPosB); screenPosB.x = screenPosB.x*Screen.width; screenPosB.y = (1-screenPosB.y)*Screen.height; screenPosA.z = screenPosB.z = 0; float angle = Vector2.Angle(Vector3.up, screenPosB - screenPosA); if(screenPosB.x < screenPosA.x) { angle = -angle; } Vector2 vector = screenPosB - screenPosA; float length = vector.magnitude; Rect upRect = new Rect(screenPosA.x - (width / 2), screenPosA.y-length, width, length); GUIUtility.RotateAroundPivot(-angle+180, screenPosA); DrawRectangle(upRect, color); GUI.matrix = Matrix4x4.identity; }
// Converts mouse coords to world coords private void ConvertDragToWorldSpace( Vector3 dragStartPos, Vector3 dragEndPos, out Vector3 dragStartWorld, out Vector3 dragEndWorld ) { Ray dragStartRay = Camera.main.ScreenPointToRay( dragStartPos ); Ray dragEndRay = Camera.main.ScreenPointToRay( dragEndPos ); Plane groundPlane = new Plane( Vector3.up, 0 ); float dist = 0f; groundPlane.Raycast( dragStartRay, out dist ); dragStartWorld = dragStartRay.GetPoint( dist ); groundPlane.Raycast( dragEndRay, out dist ); dragEndWorld = dragEndRay.GetPoint( dist ); }
private void Awake() { // Debug.Log ("!!!!!????"); sky_ = new Plane(Vector3.forward, _z); Ray ray1 = _camera.ScreenPointToRay (new Vector3 (0, 0, 0)); Ray ray2 = _camera.ScreenPointToRay (new Vector3 (0, Screen.height, 0)); float dist1 = 0; sky_.Raycast (ray1, out dist1); float dist2 = 0; sky_.Raycast (ray2, out dist2); y_ = (ray2.GetPoint (dist2) - ray1.GetPoint (dist1)).y; }
void FixedUpdate() { // Generate a plane that intersects the transform's position with an upwards normal. Plane playerPlane = new Plane(Vector3.up, transform.position); // Generate a ray from the cursor position Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); // Determine the point where the cursor ray intersects the plane. // This will be the point that the object must look towards to be looking at the mouse. // Raycasting to a Plane object only gives us a distance, so we'll have to take the distance, // then find the point along that ray that meets that distance. This will be the point // to look at. float hitdist = 0.0f; // If the ray is parallel to the plane, Raycast will return false. if (playerPlane.Raycast (ray, out hitdist)) { // Get the point along the ray that hits the calculated distance. Vector3 targetPoint = ray.GetPoint(hitdist); // Determine the target rotation. This is the rotation if the transform looks at the target point. Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position); // Smoothly rotate towards the target point. transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.time); } }
public static bool RaycastSmoke(Ray ray) { if(!CMDropper.smokePool) { return false; } for(int i = 0; i < CMDropper.smokePool.size; i++) { Transform smokeTf = CMDropper.smokePool.GetPooledObject(i).transform; if(smokeTf.gameObject.activeInHierarchy) { Plane smokePlane = new Plane((ray.origin-smokeTf.position).normalized, smokeTf.position); float enter; if(smokePlane.Raycast(ray, out enter)) { float dist = (ray.GetPoint(enter)-smokeTf.position).magnitude; if(dist < 16) { return true; } } } } return false; }
public Vector3 PositionUnderMouseLine(out bool fail) { fail = false; if (!Camera.main) { return(transform.position); } Ray r = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0)); Vector3 n = WorldNormal(); n = Vector3.Cross(n, Vector3.Cross(r.direction, n)); if (n == Vector3.zero) { fail = true; return(n); } n = n.normalized; Plane p = new Plane(n, atom.transform.position); p.Raycast(r, out var d); Vector3 hitGlobal = r.GetPoint(d); Vector3 hitLocal = moleculeSpace.transform.InverseTransformPoint(hitGlobal); Vector3 currentLocal = atom.transform.localPosition; currentLocal[axis] = hitLocal[axis]; return(moleculeSpace.transform.TransformPoint(currentLocal)); }
private void _GetMovementLoc() { // Get the position of the click in the game world Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); Debug.DrawRay(ray.origin, ray.direction, Color.black, 10000); Plane xy = new Plane(Vector3.up, new Vector3(0, 0, 0)); float point; xy.Raycast(ray, out point); m_targetPosition = ray.GetPoint(point); m_targetPosition.y = transform.position.y; if (m_targetPosition.z < -10.07f) { m_targetPosition.z = -10.07f; } else if (m_targetPosition.z > 10.07f) { m_targetPosition.z = 10.07f; } if (m_targetPosition.x > 4.57f) { m_targetPosition.x = 4.57f; } m_soccerBallRig.AI.WorkingMemory.SetItem<Vector3>("MovePos", m_targetPosition); }
// Update is called once per frame void Update() { if(Input.GetButtonDown("Fire1")) { Shot(); } if(Input.GetButtonDown("Fire2")) { Blow(); } { Vector3 move = Vector3.zero; move.x = Input.GetAxis ("Horizontal"); move.z = Input.GetAxis ("Vertical"); trans.position += move * 0.1f; } { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); Plane plane = new Plane(Vector3.up, Vector3.zero); float distance = 0; if (plane.Raycast(ray, out distance)){ trans.rotation = Quaternion.LookRotation(ray.GetPoint(distance) - trans.position); } } { Matrix4x4 ts = Matrix4x4.identity; ts.SetColumn (3, new Vector4(0.0f,0.0f,0.5f,1.0f)); ts = Matrix4x4.Scale(new Vector3(5.0f, 5.0f, 10.0f)) * ts; blowMatrix = trans.localToWorldMatrix * ts; } }
// Update is called once per frame void Update() { //Create target plane targetPlane = new Plane (-this.transform.forward, new Vector3(transform.position.x, transform.position.y, transform.position.z) + this.transform.forward*2); float dist = 10.0f; //Cast a ray from mouse Ray ray = Camera.current.ScreenPointToRay(Input.mousePosition); //Check if it hits the plane if (targetPlane.Raycast (ray, out dist)) { //Get targetpoint as a point on the plane var targetpoint = ray.GetPoint (dist); //Set appropriate rotation targetrotation = Quaternion.LookRotation (targetpoint - transform.position, transform.up ); //Rotate arm transform.rotation = targetrotation; } else { //Should not(!) be possible Debug.Log("Not hitting the plane"); } }
protected internal override void OnMouseMove( dfMouseEventArgs args ) { args.Use(); if( args.Buttons.IsSet( dfMouseButtons.Left ) ) { var ray = args.Ray; var distance = 0f; var direction = GetCamera().transform.TransformDirection( Vector3.back ); var plane = new Plane( direction, lastPosition ); plane.Raycast( ray, out distance ); var pos = ( ray.origin + ray.direction * distance ).Quantize( parent.PixelsToUnits() ); var offset = pos - lastPosition; var transformPos = ( parent.transform.position + offset ).Quantize( parent.PixelsToUnits() ); parent.transform.position = transformPos; lastPosition = pos; } base.OnMouseMove( args ); }
internal bool Internal_RaycastRef(Ray ray, ref UIHotSpot.Hit hit) { float single; Vector2 vector2 = new Vector2(); if (this.radius == 0f) { return false; } Plane plane = new Plane(UIHotSpot.forward, this.center); if (!plane.Raycast(ray, out single)) { hit = new UIHotSpot.Hit(); return false; } hit.point = ray.GetPoint(single); hit.normal = (!plane.GetSide(ray.origin) ? UIHotSpot.backward : UIHotSpot.forward); vector2.x = hit.point.x - this.center.x; vector2.y = hit.point.y - this.center.y; float single1 = vector2.x * vector2.x + vector2.y * vector2.y; if (single1 >= this.radius * this.radius) { return false; } hit.distance = Mathf.Sqrt(single1); return true; }
void Update() { if (navMeshAgent.remainingDistance < 0.5f) { if (idle != null && anim != null) anim.CrossFade(idle.name); } else { if (run != null && anim != null) anim.CrossFade(run.name); } // Moves the Player if the Left Mouse Button was clicked: if (Input.GetMouseButtonDown((int) mouseButton) && GUIUtility.hotControl == 0) { Plane playerPlane = new Plane(Vector3.up, myTransform.position); Ray ray = UnityEngine.Camera.main.ScreenPointToRay(Input.mousePosition); float hitdist = 0.0f; if (playerPlane.Raycast(ray, out hitdist)) { navMeshAgent.SetDestination(ray.GetPoint(hitdist)); } } // Moves the player if the mouse button is held down: else if (Input.GetMouseButton((int) mouseButton) && GUIUtility.hotControl == 0) { Plane playerPlane = new Plane(Vector3.up, myTransform.position); Ray ray = UnityEngine.Camera.main.ScreenPointToRay(Input.mousePosition); float hitdist = 0.0f; if (playerPlane.Raycast(ray, out hitdist)) { navMeshAgent.SetDestination(ray.GetPoint(hitdist)); } } }
void Update(){ guiIsActive = false; foreach(Canvas cn in GameObject.FindObjectsOfType<Canvas>()){ if(cn.gameObject.activeSelf){ guiIsActive = true; } } foreach(NpcActionsTemplateScript cn in GameObject.FindObjectsOfType<NpcActionsTemplateScript>()){ if(cn.gameObject.activeSelf){ guiIsActive = true; } } if (!guiIsActive && Input.GetMouseButtonDown(0) && GUIUtility.hotControl ==0) { Plane playerPlane = new Plane(Vector3.up, transform.position); Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); float hitdist = 0.0f; if (playerPlane.Raycast(ray, out hitdist)) { targetPos= ray.GetPoint(hitdist); } agent.SetDestination (targetPos); } }
public void OnBeginDrag(int modelIndex) { if (_isTracking) { /* If we're tracking, instantiate a new model prefab based on the button index and */ GameObject modelPrefab = Models[modelIndex]; Transform model = Instantiate(modelPrefab).transform; _activeModels.Add(model.gameObject); /* Set model position at touch position */ var cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition); Plane p = new Plane(Vector3.up, Vector3.zero); float enter; if (p.Raycast(cameraRay, out enter)) { model.position = cameraRay.GetPoint(enter); } /* Set model orientation to face toward the camera */ Quaternion modelRotation = Quaternion.LookRotation(Vector3.ProjectOnPlane(-Camera.main.transform.forward, Vector3.up), Vector3.up); model.rotation = modelRotation; /* Assign the new model to the move controller, so that it can be further dragged after it leaves the button area. */ _moveController.SetMoveObject(model); } }
// Update is called once per frame void Update() { Plane targetPlane = new Plane(transform.up, transform.position); foreach (Touch touch in Input.touches) { anim.SetBool("isWalking",true); //Gets the ray at position where the screen is touched Ray ray = Camera.main.ScreenPointToRay(touch.position); //Gets the position of ray along plane float dist = 0.0f; //Intersects ray with the plane. Sets dist to distance along the ray where intersects targetPlane.Raycast(ray, out dist); //Returns point dist along the ray. Vector3 planePoint = ray.GetPoint(dist); //Debug.Log("Point=" + planePoint); //True if finger touch began. If ray intersects collider, set pickedObject to transform //of collider object if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Stationary) { //anim.SetBool("isWalking",true); player.transform.LookAt(planePoint); player.transform.localPosition = Vector3.MoveTowards(playerPos, planePoint, 0.5F * Time.deltaTime); //playerPos = player.transform.position; playerPos = player.transform.localPosition; } else if (touch.phase == TouchPhase.Ended){ anim.SetBool("isWalking",false); } } }
void Update() { if (null != mirrorPlane) { if (null != objectBeforeMirror) { transform.position = objectBeforeMirror.transform.position; transform.rotation = objectBeforeMirror.transform.rotation; Vector3 positionInMirrorSpace = mirrorPlane.transform.InverseTransformPoint( objectBeforeMirror.transform.position ); // move camera forward positionInMirrorSpace.y = -positionInMirrorSpace.y; positionInMirrorSpace.z = (positionInMirrorSpace.z)/2.5f; transform.position = mirrorPlane.transform.TransformPoint( positionInMirrorSpace ); /** object is now in correct position, but looking in parallel look direction to original */ /* So, cast a ray from the original object along look-direction until you hit the plane, * then make the cloned object "look at" that position */ Vector3 mirrorsNormal = mirrorPlane.transform.localRotation * new Vector3( 0f, 1, 0f ); // Unity planes always start with normal pointing up Plane planeOfMirror = new Plane( mirrorsNormal, mirrorPlane.transform.position ); float intersectionDistance; Ray rayToMirror = new Ray( objectBeforeMirror.transform.position, objectBeforeMirror.transform.forward ); planeOfMirror.Raycast( rayToMirror, out intersectionDistance ); Vector3 hitPoint = rayToMirror.GetPoint( intersectionDistance ); transform.LookAt( hitPoint ); } } }
// Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0) && isTracking && !EventSystem.current.IsPointerOverGameObject()) { var cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition); UnityEngine.Plane groundPlane = new UnityEngine.Plane(Vector3.up, Vector3.zero); float touchPos; if (groundPlane.Raycast(cameraRay, out touchPos)) { Vector3 position = cameraRay.GetPoint(touchPos); GameObject pizza = Instantiate(PizzaPrefab, position, Quaternion.identity); } } if (trackerState == InstantTrackingState.Initializing) { if (Tracker.CanStartTracking()) { grid.TargetColor = Color.green; } else { grid.TargetColor = GridRenderer.DefaultTargetColor; } } else { grid.TargetColor = GridRenderer.DefaultTargetColor; } }
Vector3 GetPositionTouch() { //Create a Vector to store the position where the player will be moving to Vector3 moveTo = Vector3.zero; //Get the position where it was touched on the screen Vector3 positionTouch = Input.GetTouch(0).position; //Create a ray from the camera going through the position touched on the screen Ray ray = Camera.main.ScreenPointToRay(positionTouch); //Create a plane perpendicular to the players's position Plane plane = new Plane(Vector3.up, transform.position); //Create variable to store distance between the camera and the plane float distance = 0; //Verify if it ray hits the plane if (plane.Raycast(ray, out distance)) { //Get the position where it was hit moveTo = ray.GetPoint(distance); return moveTo; } else { return transform.position; } }
void Update() { if (Input.GetKey(KeyCode.W)) { rigidbody.velocity = new Vector3(rigidbody.velocity.x, speed, 0); renderer.material.mainTextureOffset = new Vector2(0.0f, 0.0f); } else if (Input.GetKey(KeyCode.S)) { rigidbody.velocity = new Vector3(rigidbody.velocity.x, -speed, 0); renderer.material.mainTextureOffset = new Vector2(0.0f, 0.0f); } if (Input.GetKey(KeyCode.A)) { rigidbody.velocity = new Vector3(-speed, rigidbody.velocity.y, 0); renderer.material.mainTextureOffset = new Vector2(0.0f, 0.0f); } else if (Input.GetKey(KeyCode.D)) { rigidbody.velocity = new Vector3(speed, rigidbody.velocity.y, 0); renderer.material.mainTextureOffset = new Vector2(0.0f, 0.0f); } if (Input.GetKeyUp(KeyCode.W)) { rigidbody.velocity = new Vector3(rigidbody.velocity.x, 0, 0); renderer.material.mainTextureOffset = new Vector2(0.5f, 0.0f); } else if (Input.GetKeyUp(KeyCode.S)) { rigidbody.velocity = new Vector3(rigidbody.velocity.x, 0, 0); renderer.material.mainTextureOffset = new Vector2(0.5f, 0.0f); } if (Input.GetKeyUp(KeyCode.A)) { rigidbody.velocity = new Vector3(0, rigidbody.velocity.y, 0); renderer.material.mainTextureOffset = new Vector2(0.5f, 0.0f); } else if (Input.GetKeyUp(KeyCode.D)) { rigidbody.velocity = new Vector3(0, rigidbody.velocity.y, 0); renderer.material.mainTextureOffset = new Vector2(0.5f, 0.0f); } Vector3 targetPos = new Vector3(); Plane playerPlane = new Plane(Vector3.forward, transform.position); Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); float hitdist = 0.0f; if (playerPlane.Raycast(ray, out hitdist)) { targetPos = ray.GetPoint(hitdist); } float diffX = targetPos.x - transform.position.x; float diffY = targetPos.y - transform.position.y; float angle = Mathf.Atan2(diffY, diffX) * Mathf.Rad2Deg; transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle - 90)); }
void Update() { // Movement input Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); Vector3 moveVelocity = moveInput.normalized * moveSpeed; controller.Move (moveVelocity); // Look input Ray ray = viewCamera.ScreenPointToRay (Input.mousePosition); Plane groundPlane = new Plane (Vector3.up, Vector3.zero); float rayDistance; // out keyword assign a value to the rayDistance variable if (groundPlane.Raycast (ray, out rayDistance)) { Vector3 point = ray.GetPoint(rayDistance); // Debug.DrawLine(ray.origin, point, Color.red); controller.LookAt(point); } // Weapon input // left mouse button if(Input.GetMouseButton(0)) { gunController.Shoot(); } }
private static Vector3 LineCast(Plane plane, Vector3 a, Vector3 b) { float dis; Ray ray = new Ray(a, b-a); plane.Raycast( ray, out dis ); return ray.GetPoint(dis); }
// Update is called once per frame public override void OnUpdate() { var vrcam = SteamVR_Render.Top(); GameObject go = Fsm.GetOwnerDefaultTarget(gazeObject); Ray r = new Ray(vrcam.transform.position, vrcam.transform.forward); Plane p = new Plane(vrcam.transform.forward, go.transform.position); float enter = 0.0f; if (p.Raycast(r, out enter)) { Vector3 intersect = vrcam.transform.position + vrcam.transform.forward * enter; float dist = Vector3.Distance(intersect, go.transform.position); //Debug.Log("Gaze dist = " + dist); if (dist < gazeInCutoff.Value && !isInGaze.Value) { isInGaze.Value = true; GazeEventArgsPlaymaker e; e.distance = dist; OnGazeOn(e); } else if (dist >= gazeOutCutoff.Value && isInGaze.Value) { isInGaze.Value = false; GazeEventArgsPlaymaker e; e.distance = dist; OnGazeOff(e); } } }
void Start() { UnityEngine.Plane worldPlane = new UnityEngine.Plane(); worldPlane.SetNormalAndPosition(Vector3.back, Vector3.zero); Ray ray; float distance; ray = followCam.ViewportPointToRay(Vector3.zero); worldPlane.Raycast(ray, out distance); Vector3 viewMin = ray.GetPoint(distance); ray = followCam.ViewportPointToRay(Vector3.one); worldPlane.Raycast(ray, out distance); Vector3 viewMax = ray.GetPoint(distance); cameraBounds.min = viewBounds.min - viewMin; cameraBounds.max = viewBounds.max - viewMax; }
public Vector3 GetWorldPositionOnPlane(Vector3 screenPosition, float y) { Ray ray = Camera.main.ScreenPointToRay(screenPosition); UnityEngine.Plane xy = new UnityEngine.Plane(Vector3.up, new Vector3(0, y, 0)); float distance; xy.Raycast(ray, out distance); return(ray.GetPoint(distance)); }
public Vector2 GetSeaPosition() { touch = Input.GetTouch(0); Ray touchRay; touchRay = mainCamera.ScreenPointToRay(touch.position); Plane ground = new Plane(Vector3.up, new Vector3(0, 0, 0)); float distance; ground.Raycast(touchRay, out distance); return(Coordinates.ConvertWorldToVector2(touchRay.GetPoint(distance))); }
public Vector3 PositionUnderMouseLineAndSphere(out bool fail) { fail = false; UpdateSliceNormal(); Ray r = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0)); Vector3 fatherPos = atom.molecularParent.transform.position; Plane p = new Plane(sliceNormal, fatherPos); p.Raycast(r, out var d); Vector3 hitGlobal = r.GetPoint(d); return(ProjectOnSphere(hitGlobal)); }
private Vector3 GetGroundPosition(Vector2 touchPosition) { var groundPlane = new Plane(Vector3.up, Vector3.zero); var touchRay = _mainCamera.ScreenPointToRay(touchPosition); float enter; if (groundPlane.Raycast(touchRay, out enter)) { return(touchRay.GetPoint(enter)); } return(Vector3.zero); }
public void Execute() { Plane plane = new Plane(Vector3.up, Vector3.zero); Vector3 mousePosition = Input.mousePosition; Ray ray = _camera.ScreenPointToRay(mousePosition); if (plane.Raycast(ray, out float enter)) { foreach (var entity in _group) { Vector3 lookDirection = ray.GetPoint(enter) - entity.position.Position; entity.ReplaceRotation(Quaternion.LookRotation(lookDirection)); } } }
private void MoveVertexY(Ray ray) { UnityEngine.Plane plane = new UnityEngine.Plane(Vector3.right, Vector3.right); float distance; if (plane.Raycast(ray, out distance)) { if (set) { offset = ray.GetPoint(distance).y - transform.localPosition.y; set = false; } transform.position = new Vector3(transform.localPosition.x, ray.GetPoint(distance).y - offset, transform.localPosition.z); Vertex.transform.position = new Vector3(transform.localPosition.x, ray.GetPoint(distance).y - offset, transform.localPosition.z); } }
static public int Raycast(IntPtr l) { try{ UnityEngine.Plane self=(UnityEngine.Plane)checkSelf(l); UnityEngine.Ray a1; checkType(l,2,out a1); System.Single a2; System.Boolean ret=self.Raycast(a1,out a2); pushValue(l,ret); pushValue(l,a2); return 2; } catch(Exception e) { LuaDLL.luaL_error(l, e.ToString()); return 0; } }
private void Update() { if (!_controlsOn) { return; } if (Input.touchCount > 0) { Touch touch = Input.GetTouch(0); /* If we're currently not dragging any augmentation, set _touched true. */ if (!_touched) { SetMoveObject(); } /* If we are dragging an augmentation, raycast against the ground plane to find how the augmentation should be moved. */ if (_touched) { var screenPosForRay = (touch.position - _startTouchPosition) + _touchOffset; Ray cameraRay = _mainCamera.ScreenPointToRay(screenPosForRay); Plane p = new Plane(Vector3.up, Vector3.zero); float enter; if (p.Raycast(cameraRay, out enter)) { var position = cameraRay.GetPoint(enter); /* Clamp the new position within reasonable bounds and make sure the augmentation is firmly placed on the ground plane. */ position.x = Mathf.Clamp(position.x, -15.0f, 15.0f); position.y = 0.0f; position.z = Mathf.Clamp(position.z, -15.0f, 15.0f); /* Lerp the position of the dragged augmentation so that the movement appears smoother */ _activeObject.localPosition = Vector3.Lerp(_activeObject.localPosition, position, 0.25f); } } } else { /* If there are no touches, stop dragging the currently dragged augmentation, if there is one. */ _touched = false; } }
void HandleMouseInput() { //scroll if (Input.mouseScrollDelta.y != 0) { if (Input.mouseScrollDelta.y > 0 && newZoom.z < maxZoom) { newZoom += Input.mouseScrollDelta.y * zoomAmount; } if (Input.mouseScrollDelta.y < 0 && newZoom.z > minZoom) { newZoom += Input.mouseScrollDelta.y * zoomAmount; } } //drag if (Input.GetMouseButtonDown(2)) { Plane plane = new Plane(Vector3.up, Vector3.zero); Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); float entry; if (plane.Raycast(ray, out entry)) { dragStartPosition = ray.GetPoint(entry); } } if (Input.GetMouseButton(2)) { Plane plane = new Plane(Vector3.up, Vector3.zero); Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); float entry; if (plane.Raycast(ray, out entry)) { dragCurrentPosition = ray.GetPoint(entry); newPosition = transform.position + dragStartPosition - dragCurrentPosition; } } cameraTransform.localPosition = Vector3.Lerp(cameraTransform.localPosition, newZoom, Time.deltaTime * movementTime); }
private void MoveCylinderVertexZ(Ray ray) { UnityEngine.Plane plane = new UnityEngine.Plane(Vector3.up, Vector3.up * 0f); float distance; if (plane.Raycast(ray, out distance)) { if (set) { offset = ray.GetPoint(distance).z - transform.localPosition.z; set = false; } for (int i = cMesh.mSelectedIndex; i < cMesh.mSelectedIndex + cMesh.cResolution; i++) { Vector3 dir = cMesh.mControllers[i].transform.position; float dist = ray.GetPoint(distance).z - offset; Vector3 move = dir.normalized * dist; transform.position = new Vector3(transform.localPosition.x, transform.localPosition.y, dist); cMesh.mControllers[i].transform.position = new Vector3(move.x, cMesh.mControllers[i].transform.localPosition.y, move.z); } } }
public Vector3 PositionUnderMousePlane(out bool fail) { fail = false; if (!Camera.main) { return(transform.position); } Ray r = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0)); Vector3 n = WorldNormal(); if (Mathf.Abs(Vector3.Dot(n, r.direction)) < 0.1f) { fail = true; } Plane p = new Plane(n, atom.transform.position); p.Raycast(r, out var d); Vector3 hit = r.GetPoint(d); return(hit); }
public void MoveObject() { Touch touch = Input.GetTouch(0); var screenPosForRay = (touch.position - _startTouchPosition) + _touchOffset; Ray cameraRay = _mainCamera.ScreenPointToRay(screenPosForRay); Plane p = new Plane(Vector3.up, Vector3.zero); float enter; if (p.Raycast(cameraRay, out enter)) { var position = cameraRay.GetPoint(enter); /* Clamp the new position within reasonable bounds and make sure the augmentation is firmly placed on the ground plane. */ //position.x = Mathf.Clamp(position.x, -15.0f, 15.0f); position.y = 0.0f; //position.z = Mathf.Clamp(position.z, -15.0f, 15.0f); /* Lerp the position of the dragged augmentation so that the movement appears smoother */ _objectTransform.position = Vector3.Lerp(_objectTransform.position, position, 0.25f); } }
public void OnBeginDrag(int modelIndex) { if (_isTracking) { // Create object GameObject modelPrefab = Models[modelIndex]; Transform model = Instantiate(modelPrefab).transform; _activeModels.Add(model.gameObject); // Set model position at touch position var cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition); Plane p = new Plane(Vector3.up, Vector3.zero); float enter; if (p.Raycast(cameraRay, out enter)) { model.position = cameraRay.GetPoint(enter); } // Set model orientation to face toward the camera Quaternion modelRotation = Quaternion.LookRotation(Vector3.ProjectOnPlane(-Camera.main.transform.forward, Vector3.up), Vector3.up); model.rotation = modelRotation; _moveController.SetMoveObject(model); } }
// Update is called once per frame void Update() { float minX = Random.Range(0, 10); float minY = Random.Range(0, 10); float minZ = Random.Range(0, 10); float maxX = Random.Range(10.1f, 20); float maxY = Random.Range(10.1f, 20); float maxZ = Random.Range(10.1f, 20); float originX = Random.Range(0, 10); float originY = Random.Range(0, 10); float originZ = Random.Range(0, 10); float directionX = Random.Range(1, 10); float directionY = Random.Range(1, 10); float directionZ = Random.Range(1, 10); MBounds myB = new MBounds(MVector3.zero, MVector3.zero); myB.SetMinMax(new MVector3(minX, minY, minZ), new MVector3(maxX, maxY, maxZ)); float mD = 0; var mRay = new MRay(new MVector3(originX, originY, originZ), new MVector3(directionX, directionY, directionZ)); bool mR = myB.IntersectRay(mRay, out mD); Bounds B = new Bounds(Vector3.zero, Vector3.zero); B.SetMinMax(new Vector3(minX, minY, minZ), new Vector3(maxX, maxY, maxZ)); float D = 0; var uRay = new UnityEngine.Ray(new Vector3(originX, originY, originZ), new Vector3(directionX, directionY, directionZ)); bool R = B.IntersectRay(uRay, out D); if (Mathf.Abs(mD - D) < 0.00001f && mR == R) { } else { if (originX < minX || originX > maxX || originY < minY || originY > maxY || originZ < minZ || originZ > maxZ) { mR = myB.IntersectRay(mRay, out mD); } } float normalX = Random.Range(1, 10); float normalY = Random.Range(1, 10); float normalZ = Random.Range(1, 10); float distance = Random.Range(1, 10); var ray = new Ray(new Vector3(originX, originY, originZ), new Vector3(directionX, directionY, directionZ)); var mray = new MRay(new MVector3(originX, originY, originZ), new MVector3(directionX, directionY, directionZ)); var plane = new Plane(new Vector3(normalX, normalY, normalZ), -distance); var mplane = new MPlane(new MVector3(normalX, normalY, normalZ), distance); D = 0; mD = 0; R = false; mR = false; R = plane.Raycast(ray, out D); mR = mplane.Raycast(mray, out mD); if (Mathf.Abs(mD - D) < 0.00001f && mR == R) { } else { var a = 1; } Matrix44 rotate = Matrix44.AngleAxis(mAngle, mAxis); for (int i = 0; i < mVertices.Length; i++) { var item = mVertices[i]; var v = new MVector3(item.x, item.y, item.z); var t = v * rotate; mVertices[i].x = t.x; mVertices[i].y = t.y; mVertices[i].z = t.z; var item2 = mNormals[i]; var v2 = new MVector3(item2.x, item2.y, item2.z); var t2 = v2 * rotate; mNormals[i].x = t2.x; mNormals[i].y = t2.y; mNormals[i].z = t2.z; } mMesh.vertices = mVertices; mMesh.normals = mNormals; }
// Update is called once per frame void Update() { direction.x = Input.GetAxis("Horizontal"); direction.z = Input.GetAxis("Vertical"); if (movenent) { playerMovement.Move(direction); } var plane = new UnityEngine.Plane(Vector3.up, transform.position); var dis = 0.0f; var ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (plane.Raycast(ray, out dis)) { var worldPosition = ray.GetPoint(dis); direction.x = worldPosition.x - transform.position.x; direction.z = worldPosition.z - transform.position.z; } //direction.x = Input.GetAxis("HorizontalLook"); playerMovement.Look(direction); if (playerRodControl.hookInstance) { if (cameraStick.parent == transform) { lookAtFish = true; cameraStick.extraOffset = playerRodControl.hookInstance.transform.position - transform.position; } } else if (lookAtFish) { //re zero the camera but do not constantly override the extra offset cameraStick.extraOffset = Vector3.zero; lookAtFish = false; } if (Input.GetButtonDown("Cast")) { bool didCast = playerRodControl.StartedCasting(); if (catchFish != null) { catchFish(); catchFish = null; } } if (Input.GetButtonUp("Cast")) { bool finishedCast = playerRodControl.EndedCasting(); } }
// ************************************************* // Main logic // ************************************************* void Update() { if (ReachedGameOverState()) { _gameOverGroup.SetActive(true); return; } if (ReachedWinState()) { _winScreenGroup.SetActive(true); return; } if (!_mouse_down && Input.GetMouseButtonDown(0)) { _mouse_down = true; } if (_mergeAnimationsRunning && LerpAnimationsCompleted()) { _mergeAnimationsRunning = false; if (_mergeTarget.ReachedMaxValue()) { RemoveBallFromGrid(_mergeTarget.GetGridXCoord(), _mergeTarget.GetGridYCoord()); TriggerExplosion(_mergeTarget.GetGridXCoord(), _mergeTarget.GetGridYCoord()); // Trigger animation Destroy(_mergeTarget.gameObject); NextTurn(); } else { MergeBalls(_mergeTarget.GetGridXCoord(), _mergeTarget.GetGridYCoord()); _mergeAnimationsRunning = !LerpAnimationsCompleted(); } if (!_mergeAnimationsRunning) { RemoveFreeFloatingBall(); _mergeTarget = null; } } if (!CanShoot()) { return; } if (_mouse_down) { Vector3 mouseCoordsWorldSpace = Camera.main.ScreenToWorldPoint(Input.mousePosition); mouseCoordsWorldSpace.z = 0; Vector3 shootDirection = (mouseCoordsWorldSpace - _ballShooter.GetPosition()).normalized; Ray ray = new Ray(_ballShooter.GetPosition(), shootDirection); List <Vector3> linesToDraw = new List <Vector3>(); linesToDraw.Add(_ballShooter.GetPosition()); GameObject hitBall; RaycastHit hitInfo; List <Vector3> animationPath = new List <Vector3>(); if (!IntersectsBalls(ray, out hitBall, out hitInfo)) { float closestDistance = Single.PositiveInfinity; Plane closestPlane = _boundsPlanes[0]; Vector3 closestHitPoint = new Vector3(); bool didHitPlane = false; // => max 1 reflection // track nearest intersection point and store "next ray" here, then after the loop we do 1 more intersection test vs balls for (int i = 0; i < _boundsPlanes.Length; ++i) { Plane plane = _boundsPlanes[i]; if (plane.Raycast(ray, out float enter)) { didHitPlane = true; Vector3 hitPoint = ray.GetPoint(enter); float distance = Vector3.Distance(_ballShooter.GetPosition(), hitPoint); if (distance < closestDistance) { closestDistance = distance; closestPlane = plane; closestHitPoint = hitPoint; } } } if (didHitPlane) { Vector3 reflect = Vector3.Reflect(ray.direction, closestPlane.normal); // add plane intersection point to path animationPath.Add(closestHitPoint); linesToDraw.Add(closestHitPoint); Ray nextRay = new Ray(closestHitPoint, reflect); IntersectsBalls(nextRay, out hitBall, out hitInfo); _ballShooter.HidePreviewBall(); } } if (hitBall) { linesToDraw.Add(hitInfo.point); _lineRenderer.positionCount = linesToDraw.Count; for (int i = 0; i < linesToDraw.Count; ++i) { _lineRenderer.SetPosition(i, linesToDraw[i]); } var hitBallComp = hitBall.GetComponent <Ball>(); var gridPosition = _ballSpawner.GeneratePosition(hitBallComp.GetGridXCoord(), hitBallComp.GetGridYCoord()); var centerToHitDir = (hitInfo.point - gridPosition).normalized; if (PlaceOnGrid(hitBallComp.GetGridXCoord(), hitBallComp.GetGridYCoord(), centerToHitDir, out var gridX, out var gridY)) { Vector3 nextPosition = _ballSpawner.GeneratePosition(gridX, gridY); _ballShooter.ShowPreviewBall(nextPosition); // add next ball position to path animationPath.Add(nextPosition); if (Input.GetMouseButtonUp(0)) { _canShoot = false; _ballShooter.HidePreviewBall(); StartCoroutine(ShootBallAnimation(_ballShooter.GetCurrentBall().GetComponent <Ball>(), gridX, gridY, animationPath)); _lineRenderer.positionCount = 0; } } else { _ballShooter.HidePreviewBall(); } } else { _lineRenderer.positionCount = 0; } }
static bool Plane_Raycast__Ray__Single(JSVCall vc, int argc) { int len = argc; if (len == 2) { UnityEngine.Ray arg0 = (UnityEngine.Ray)JSMgr.datax.getObject((int)JSApi.GetType.Arg); int r_arg1 = JSApi.incArgIndex(); System.Single arg1; UnityEngine.Plane argThis = (UnityEngine.Plane)vc.csObj; JSApi.setBooleanS((int)JSApi.SetType.Rval, (System.Boolean)(argThis.Raycast(arg0, out arg1))); JSMgr.changeJSObj(vc.jsObjID, argThis); JSApi.setArgIndex(r_arg1); JSApi.setSingle((int)JSApi.SetType.ArgRef, arg1); } return(true); }