// Returns the ground based on spherecast RaycastHit GetGroundHit() { RaycastHit hitInfo; if (Physics.SphereCast(transform.position, controller.radius + controller.skinWidth, Vector3.down, out hitInfo, controller.height / 2 - controller.radius)) { isGrounded = true; if (Vector3.Angle(Vector3.up, hitInfo.normal) >= controller.slopeLimit) { groundTooSloped = true; } else { groundTooSloped = false; } } else { isGrounded = false; groundTooSloped = false; hitInfo = default(RaycastHit); } return(hitInfo); }
private bool CheckWithinFOV(Vector3 position, float fov) { // check if provided position is within this unit's FOV (within a cone coming out of unit) Vector3 towardsPosition = position - transform.position; float angleToPosition = Vector3.Angle(transform.forward, towardsPosition); return(angleToPosition <= fov); }
bool IsLookingAtObject(Vector3 from, Vector3 to) { float angle = Vector3.Angle(from, to); if (angle < 2f || angle > 358f) { return(true); } return(false); }
bool TargetCanSeeMe() { Vector3 toAgent = transform.position - target.transform.position; float lookingAngle = Vector3.Angle(target.transform.forward, toAgent); if (lookingAngle < 60) { return(true); } return(false); }
static private float Angle(Vector3 pos1, Vector3 pos2) { Vector3 from = pos2 - pos1; Vector3 to = new Vector3(1, 0); float result = Vector3.Angle(from, to); Vector3 cross = Vector3.Cross(from, to); if (cross.z > 0) { result = 360f - result; } return(result); }
void Pursue() { Vector3 targetDir = target.transform.position - transform.position; float relativeHeading = Vector3.Angle(transform.forward, transform.TransformVector(target.transform.forward)); float toTarget = Vector3.Angle(transform.forward, transform.TransformVector(targetDir)); if ((toTarget > 90 && relativeHeading < 20) || target.GetComponent <Drive>().currentSpeed < 0.01f) { Seek(target.transform.position); return; } float lookAhead = targetDir.magnitude / (agent.speed + target.GetComponent <Drive>().currentSpeed); Seek(target.transform.position + target.transform.forward * lookAhead * 5); }
//--------------------- win condition ------------------------- void CheckGuardCaughtRobber(PlayerServerState guardState, PlayerServerState robberState) { if (isGameOver) { return; } Vector3 guardForward = new Vector3(Mathf.Cos(Mathf.Deg2Rad * (guardState.rotation.y - 90)), 0, -Mathf.Sin(Mathf.Deg2Rad * (guardState.rotation.y - 90))); Vector3 guardToRobber = robberState.position - guardState.position; float visionAngle = Mathf.Abs(Vector3.Angle(guardForward, guardToRobber)); if (visionAngle > 90) { return; } //visionAngle = 180 - visionAngle; float distance = Vector3.Distance(guardState.position, robberState.position); if (distance < 20f && visionAngle < 20f) { RaycastHit hit; if (Physics.Raycast(guardState.position + new Vector3(0, 0.3f, 0), guardToRobber, out hit, 100f)) { if (hit.distance < distance) { return; } } Debug.Log(visionAngle); isGameOver = true; cmge.EndGameInAllClients(0); } }
/* * Method for Handling Zoom and Rotate */ private void ZoomRotate() { var touchZero = Input.touches[0]; var touchOne = Input.touches[1]; if (touchZero.phase == TouchPhase.Began || touchOne.phase == TouchPhase.Began) { _initPinchDist = Vector3.Distance(touchZero.position, touchOne.position); _initRotation = touchZero.position - touchOne.position; } if (touchZero.phase == TouchPhase.Moved || touchOne.phase == TouchPhase.Moved) { var newPinchDist = Vector3.Distance(touchZero.position, touchOne.position); _pinchDistDelta = newPinchDist - _initPinchDist; // save pinch difference var rotationVector = touchZero.position - touchOne.position; var rotationAngle = Vector3.Angle(rotationVector, _initRotation); var cross = Vector3.Cross(_initRotation, rotationVector); if (rotationAngle > RotationThreshold) { if (cross.z > 0) { RotateCamera(rotationAngle); } else if (cross.z < 0) { RotateCamera(-rotationAngle); } } else if (Math.Abs(_pinchDistDelta) >= PinchThreshold) { ZoomCamera(touchZero, touchOne); } } }
private void FixedUpdate() { // control crouching Height = Mathf.SmoothDamp(Height, targetHeight, ref heightVelocity, heightChangeSpeed); // 2d input vector Vector2 input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")); // moving diagonally isn't faster // not a good movement tech to make it faster since it's not intuitive and would complicate the movement for AI // since you would have to calculate speed based on direction of body relative to head, and their pathfinding // may not accomodate it (i.e. they may swing past the target by accident if they move diagonally at one point // while moving.) input = Vector2.ClampMagnitude(input, 1); if (grounded) { bool sliding = false; // See if surface immediately below should be slid down. We use this normally rather than a ControllerColliderHit point, // because that interferes with step climbing amongst other annoyances if (Physics.Raycast(m_Transform.position, -Vector3.up, out slideRaycastHit, rayDistance)) { if (Vector3.Angle(slideRaycastHit.normal, Vector3.up) > slideLimit) { sliding = true; } } // However, just raycasting straight down from the center can fail when on steep slopes // So if the above raycast didn't catch anything, raycast down from the stored ControllerColliderHit point instead else { Physics.Raycast(contactPoint + Vector3.up, -Vector3.up, out slideRaycastHit); if (Vector3.Angle(slideRaycastHit.normal, Vector3.up) > slideLimit) { sliding = true; } } // If we were falling, and we fell a vertical distance greater than the threshold, run a falling damage routine if (falling) { falling = false; // We just landed right? Do a short crouch-uncrouch to give a bounce to landing if (Input.GetButton("Crouch")) { height = crouchHeight - landRecoilHeightChange; } else { height = standHeight - landRecoilHeightChange; } } // If running isn't on a toggle, then use the appropriate speed depending on whether the run button is down if (!toggleRun) { if (Input.GetButton("Crouch") && grounded) { currentSpeed = crouchSpeed; } else { currentSpeed = Input.GetKey(KeyCode.LeftShift) ? walkSpeed : moveSpeed; } } // If sliding (and it's allowed), or if we're on an object tagged "Slide", get a vector pointing down the slope we're on if ((sliding && slideWhenOverSlopeLimit) || (slideOnTaggedObjects && slideRaycastHit.collider.CompareTag("Slide"))) { Vector3 hitNormal = slideRaycastHit.normal; moveDirection = new Vector3(hitNormal.x, -hitNormal.y, hitNormal.z); Vector3.OrthoNormalize(ref hitNormal, ref moveDirection); moveDirection *= slideSpeed; playerControl = false; } // Otherwise recalculate moveDirection directly from axes, adding a bit of -y to avoid bumping down inclines else { moveDirection = new Vector3(input.x, -antiBumpFactor, input.y); moveDirection = m_Transform.TransformDirection(moveDirection) * currentSpeed; playerControl = true; } // Jump! But only if the jump button has been released and player has been grounded for a given number of frames if (!Input.GetButton("Jump")) { jumpTimer++; } else if (jumpTimer >= antiBunnyHopFactor) { moveDirection.y = jumpSpeed; jumpTimer = 0; } } else { // TODO: Decide if we can get walljump working. Leaning no right now... // if (canWallJump) // { // // Jump! But only if the jump button has been released and player has been grounded for a given number of frames // if (!Input.GetButton("Jump")) // { // m_JumpTimer++; // } // else if (m_JumpTimer >= m_AntiBunnyHopFactor) // { // if(playerAnimation != null) // playerAnimation.jumpPoint = transform.position; // // m_MoveDirection.y = m_JumpSpeed; // m_JumpTimer = 0; // } // // canWallJump = false; // } // If we stepped over a cliff or something, set the height at which we started falling if (!falling) { falling = true; } // If air control is allowed, check movement but don't touch the y component if (airControl && playerControl) { moveDirection.x = input.x * currentSpeed; moveDirection.z = input.y * currentSpeed; moveDirection = m_Transform.TransformDirection(moveDirection); } } if (thrusting) { thrustDirection.x = thrustInputX * currentThrustSpeed; thrustDirection.y = 0; thrustDirection.z = thrustInputY * currentThrustSpeed; } else { thrustDirection = Vector3.zero; } thrustDirection = m_Transform.TransformDirection(thrustDirection); // Apply gravity moveDirection.y -= gravity * Time.deltaTime; // Move the controller, and set grounded true or false depending on whether we're standing on something grounded = (controller.Move((moveDirection + thrustDirection) * Time.deltaTime) & CollisionFlags.Below) != 0; }
public void GenerateVisual() { List <Vector3> ineqs = new List <Vector3>(); int num = 1; while (true) { string tmps = "InpEqn2d" + num; var t = GameObject.Find(tmps); if (t == null) { break; } string ta = "a"; string tb = "b"; string tc = "c"; float fa, fb, fc; foreach (Transform child in t.transform) { if (child.name == "InputA") { ta = child.gameObject.GetComponent <InputField>().text; } else if (child.name == "InputB") { tb = child.gameObject.GetComponent <InputField>().text; } else if (child.name == "InputC") { tc = child.gameObject.GetComponent <InputField>().text; } } if (float.TryParse(ta, out fa) && float.TryParse(tb, out fb) && float.TryParse(tc, out fc)) { Vector3 ineqt = new Vector3(fa, fb, fc); ineqs.Add(ineqt); } num++; } ineqs.Add(new Vector3(-1, 0, 0)); ineqs.Add(new Vector3(0, -1, 0)); List <Vector2> points = new List <Vector2>(); int lineCount = ineqs.Count; for (int i = 0; i < lineCount; i++) { for (int j = 0; j < i; j++) { int spot = (i * (i - 1)) / 2 + j; float a1 = ineqs[i].x; float a2 = ineqs[j].x; float b1 = ineqs[i].y; float b2 = ineqs[j].y; float c1 = ineqs[i].z; float c2 = ineqs[j].z; if (Math.Abs(a1 * b2 - a2 * b1) > 0.0001) { points.Add(new Vector2((b2 * c1 - b1 * c2) / (a1 * b2 - a2 * b1), (a1 * c2 - a2 * c1) / (a1 * b2 - a2 * b1))); } } } float maxmag = 1; for (int i = points.Count - 1; i >= 0; i--) { bool tmpb = false; for (int j = 0; j < lineCount; j++) { if (ineqs[j].x * points[i].x + ineqs[j].y * points[i].y > (ineqs[j].z + 0.0001)) { tmpb = true; } } maxmag = Math.Max(maxmag, Math.Max(Math.Abs(points[i].x), Math.Abs(points[i].y))); if (tmpb) { points.RemoveAt(i); } } maxmag = (int)(maxmag * 100) + 1000; Vector3[] boundList = new Vector3[4]; boundList[0] = new Vector3(1, 0, maxmag); boundList[1] = new Vector3(0, 1, maxmag); boundList[2] = new Vector3(-1, 0, maxmag); boundList[3] = new Vector3(0, -1, maxmag); for (int i = 0; i < lineCount; i++) { for (int j = 0; j < 4; j++) { int spot = (i * (i - 1)) / 2 + j; float a1 = ineqs[i].x; float a2 = boundList[j].x; float b1 = ineqs[i].y; float b2 = boundList[j].y; float c1 = ineqs[i].z; float c2 = boundList[j].z; if (Math.Abs(a1 * b2 - a2 * b1) > 0.0001) { points.Add(new Vector2((b2 * c1 - b1 * c2) / (a1 * b2 - a2 * b1), (a1 * c2 - a2 * c1) / (a1 * b2 - a2 * b1))); } } } for (int i = points.Count - 1; i >= 0; i--) { bool tmpb = false; for (int j = 0; j < lineCount; j++) { if (ineqs[j].x * points[i].x + ineqs[j].y * points[i].y > (ineqs[j].z + 0.0001)) { tmpb = true; } } if (tmpb) { points.RemoveAt(i); } } Vector2 centroid = new Vector2(0, 0); foreach (Vector2 p in points) { centroid += p; } centroid /= points.Count; List <float> angles = new List <float>(); List <float> angles2 = new List <float>(); foreach (Vector2 p in points) { Vector3 tmpa = new Vector3(-centroid.x + p.x, -centroid.y + p.y, 0); float ang = Vector3.Angle(tmpa, new Vector3(1, 0, 0)); if (tmpa.y < 0) { ang = 360 - ang; } angles.Add(ang); angles2.Add(ang); } angles.Sort(); List <Vector2> finalPoints = new List <Vector2>(); foreach (float a in angles) { finalPoints.Add(points[angles2.IndexOf(a)]); } //Add points that intersect with very far away DontDestroyOnLoad(shape); for (int i = 0; i < finalPoints.Count; i++) //Vector2 p in finalPoints) { var k = Instantiate(linep, shape.transform); LineRenderer ltmp = k.GetComponent <LineRenderer>(); ltmp.SetPosition(0, finalPoints[i]); ltmp.SetPosition(1, finalPoints[(i + 1) % finalPoints.Count]); } //write into txt file string cx = GameObject.Find("c-x").GetComponent <InputField>().text; string cy = GameObject.Find("c-y").GetComponent <InputField>().text; string txtPath = "Assets/Scripts/testdata.txt"; using (FileStream fs = File.Create(txtPath)) { } StreamWriter writer = new StreamWriter(txtPath, true); int n = 2; int m = lineCount; writer.WriteLine(n); writer.WriteLine(m); //writing A for (int i = 0; i < ineqs.Count; i++) { writer.WriteLine(ineqs[i].x.ToString() + " " + ineqs[i].y.ToString()); } //writing b string tmpss = ""; for (int i = 0; i < ineqs.Count; i++) { tmpss += ineqs[i].z.ToString() + " "; } writer.WriteLine(tmpss); //writing c string tmpsc = cx + " " + cy; writer.WriteLine(tmpsc); writer.Close(); //run python file string fileName = "Assets/Scripts/simplex.py"; string pythonPtxt = "Assets/Scripts/python-path.txt"; string[] txtlines = System.IO.File.ReadAllLines(pythonPtxt); string text = txtlines[0]; string pythonP = @text; string progToRun = fileName; char[] splitter = { '\r' }; Process proc = new Process(); proc.StartInfo.FileName = pythonP; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.UseShellExecute = false; // call hello.py to concatenate passed parameters proc.StartInfo.Arguments = progToRun; proc.Start(); proc.WaitForExit(); StreamReader sReader = proc.StandardOutput; string[] output = sReader.ReadToEnd().Split(splitter); if (output[0] == "Optimal") { float val = float.Parse(output[1]); float[] soll = new float[2]; soll[0] = float.Parse(cx); //float.Parse(output[2]); soll[1] = float.Parse(cy); //float.Parse(output[3]); int stepl = int.Parse(output[4]); Vector2[] steps = new Vector2[stepl]; for (int i = 0; i < stepl; i++) { steps[i].x = float.Parse(output[5 + i * 2]); steps[i].y = float.Parse(output[6 + i * 2]); } //create optimal line List <Vector2> solpoints = new List <Vector2>(); if (Math.Abs(soll[1]) >= 0.0001) { solpoints.Add(new Vector2(-maxmag, (val + soll[0] * maxmag) / soll[1])); solpoints.Add(new Vector2(maxmag, (val - soll[0] * maxmag) / soll[1])); } if (Math.Abs(soll[0]) >= 0.0001) { solpoints.Add(new Vector2((val + soll[1] * maxmag) / soll[0], -maxmag)); solpoints.Add(new Vector2((val - soll[1] * maxmag) / soll[0], maxmag)); } solpoints.Sort((a, b) => a.x.CompareTo(b.x)); for (int i = 0; i < solpoints.Count; i++) //Vector2 p in finalPoints) { var k = Instantiate(linep, shape.transform); LineRenderer ltmp = k.GetComponent <LineRenderer>(); ltmp.SetPosition(0, new Vector2(solpoints[i].x, solpoints[i].y)); ltmp.SetPosition(1, new Vector2(solpoints[(i + 1) % solpoints.Count].x, solpoints[(i + 1) % solpoints.Count].y)); ltmp.material = optmat; ltmp.material.color = Color.blue; print(solpoints[i]); } print(solpoints.Count); //step points for (int i = 0; i < stepl; i++) { var k = Instantiate(circ, new Vector3(steps[i].x, steps[i].y, 0), UnityEngine.Quaternion.identity, shape.transform); var t = Instantiate(numbering, new Vector3(steps[i].x, steps[i].y, -1), UnityEngine.Quaternion.identity, shape.transform); Text ttmp = t.transform.GetChild(0).gameObject.GetComponent <Text>(); ttmp.text = i.ToString(); } } proc.WaitForExit(); //read from txt file SceneManager.LoadScene("2DVisual"); }