void checkGrab(HandScript hand, RaycastHit hit) { if (hit.distance < hand.armLength) { aSource.PlayOneShot(grabClip); grabbing = true; hand.grabHold(hit); Vector3 upVector = VectorFunctions.parallelVectorToNormal(transform.up, hit.normal); if (upVector == Vector3.zero) { upVector = -hand.side * Vector3.Cross(transform.forward, hit.normal); print("altered"); } Vector3 forwardVector = hand.side * Vector3.Cross(upVector, hit.normal); //print("normal: " + hit.normal); //print("upVector: " + upVector); // print(upVector.normalized); targetRotation = Quaternion.LookRotation(forwardVector, upVector); //targetPosition = hit.point + 0.7f * hit.normal - 1f*forwardVector.normalized; targetPosition = hit.point - hand.restingPoint.x * hit.normal - hand.restingPoint.y * upVector.normalized - hand.restingPoint.z * forwardVector.normalized; //print(VectorFunctions.findVectorInDirection(new Vector3(-1, 0, 0), new Vector3(1, 0, 0))); } else { grabbing = false; hand.releaseHold(); } }
protected void FixedUpdate() { if (_moving) { if (Math.Abs(MoveDuration) < 0.01f) { _moving = false; gameObject.transform.position = _finalPosition; gameObject.transform.eulerAngles = _finalRotation; return; } var deltaTime = GetDeltaTime(); _currentDuration += deltaTime; var lerpPoint = Mathf.Clamp(_currentDuration / MoveDuration, 0.0f, 1.0f); gameObject.transform.position = VectorFunctions.LerpVector(_initialLerpPosition, _finalPosition, lerpPoint); gameObject.transform.eulerAngles = VectorFunctions.LerpVector(_initialLerpRotation, _finalRotation, lerpPoint); if (_currentDuration > MoveDuration) { _moving = false; _currentDuration = 0.0f; } } }
public NavNode GetBestNode(NavNode currentNode, NavNode startNode, NavNode destinationNode) { if (currentNode != null && destinationNode != null && currentNode.NeighbourRefs != null) { var foundNewNode = false; var closestToTarget = currentNode; var closestDistance = VectorFunctions.DistanceSquared(currentNode.Position, destinationNode.Position); foreach (var neighbour in currentNode.NeighbourRefs) { var neighbourDistance = VectorFunctions.DistanceSquared(neighbour.Position, destinationNode.Position); if (neighbourDistance < closestDistance) { closestDistance = neighbourDistance; closestToTarget = neighbour; foundNewNode = true; } } if (foundNewNode) { return(closestToTarget); } } return(null); }
public NavNode GetBestNode(NavNode currentNode, NavNode startNode, NavNode destinationNode) { if (currentNode != null && startNode != null && currentNode.NeighbourRefs != null) { var foundNewNode = false; var furthestFromStart = currentNode; var furthestDistance = VectorFunctions.DistanceSquared(currentNode.Position, startNode.Position); foreach (var neighbour in currentNode.NeighbourRefs) { var neighbourDistance = VectorFunctions.DistanceSquared(neighbour.Position, startNode.Position); if (neighbourDistance > furthestDistance) { furthestDistance = neighbourDistance; furthestFromStart = neighbour; foundNewNode = true; } } if (foundNewNode) { return(furthestFromStart); } } return(null); }
public Vector3 GetCenter() { if (Walls.Count == 0) { return(VectorFunctions.Switch3D2D(Position)); } var positions = new List <Vector3>(); foreach (var w in Walls) { if (!positions.Contains(w.P1)) { positions.Add(w.P1); } if (!positions.Contains(w.P2)) { positions.Add(w.P2); } } var center = Vector3.zero; foreach (var p in positions) { center = center + p; } return(new Vector3(center.x / positions.Count, center.y / positions.Count, center.z / positions.Count)); }
/// <summary> /// Rebuild 2D and 3D objects from data /// </summary> public override void RebuildSceneData() { CharactersCreator.Instance.DestroyGameObject(associated2DObject); CharactersCreator.Instance.DestroyGameObject(associated3DObject); associated2DObject = CharactersCreator.Instance.CreateCharacter2D(Type, SpreadArms); associated3DObject = CharactersCreator.Instance.CreateCharacter3D(Type, SpreadArms); associated2DObject.transform.position = VectorFunctions.Switch3D2D(Position); associated3DObject.transform.position = Position; associated2DObject.transform.localEulerAngles = Vector3.forward * Rotation * -1f; associated3DObject.transform.localEulerAngles = Vector3.up * Rotation; //if(Type == CharacterType.WheelChairEmpty) //{ // associated3DObject.transform.localEulerAngles -= Vector3.right * 90; //} associated2DObject.tag = "Character"; associated3DObject.tag = "Character"; associated2DObject.gameObject.SetLayerRecursively((int)ErgoLayers.Top); associated3DObject.gameObject.SetLayerRecursively((int)ErgoLayers.ThreeD); var s = Size; var ms = MeshSize; associated2DObject.transform.localScale = VectorFunctions.Switch3D2D( new Vector3(s.x / ms.x, s.y / ms.y, s.z / ms.z)); associated3DObject.transform.localScale = new Vector3(s.x / ms.x, s.y / ms.y, s.z / ms.z); }
/// <summary> /// Move the opening along the wall, according to mouse. /// </summary> /// <param name="offset">The starting position when the users clicks</param> public override void Move(Vector3 offset) { Camera cam = GlobalManager.Instance.GetActiveCamera(); switch (cam.gameObject.layer) { case (int)ErgoLayers.Top: Vector3 pos2D = InputFunctions.GetWorldPoint2D(cam); Room r = SelectedObjectManager.Instance.currentRoomData; System.Single roomMod = r == null ? 1f : r.LockAngles ? 1f / 4f : 1f / 2f; SetPosition(VectorFunctions.Switch3D2D(Position) + (pos2D - offset) * roomMod); break; case (int)ErgoLayers.ThreeD: Vector3 pos3D = InputFunctions.GetWorldPoint3D(cam, false); Room room = SelectedObjectManager.Instance.currentRoomData; //System.Single roomModif = room == null ? 1f : room.LockAngles ? 1f / 4f : 1f / 2f; SetPosition(VectorFunctions.Switch3D2D(offset)); //Debug.Log(pos3D+" "+ VectorFunctions.Switch3D2D(offset) +" Result = "+ (pos3D - offset) * 1 + " final = " + VectorFunctions.Switch2D3D(Position) + (pos3D - offset) * 1); break; } }
// Update is called once per frame void FixedUpdate() { Vector2 averagePos = VectorFunctions.Midpoint(toFollow); //take the average pos and lerp towards it; add the offset afterward transform.position = Vector3.Lerp(transform.position, new Vector3(averagePos.x, averagePos.y, transform.position.z), lerpAmount) + offset; }
protected void Update() { for ( var currentNoiseIndex = 0; currentNoiseIndex < _noisesToUpdate.Count && currentNoiseIndex < NoiseServiceConstants.NoisesPerUpdate; currentNoiseIndex++ ) { var currentNoise = _noisesToUpdate[currentNoiseIndex]; foreach (var listenerEntry in _listenerEntries) { if (VectorFunctions.DistanceSquared(currentNoise.NoiseLocation, listenerEntry.Location) < currentNoise.NoiseRadius) { listenerEntry.Listener.OnNoiseHeard(currentNoise); } } } if (_noisesToUpdate.Count < NoiseServiceConstants.NoisesPerUpdate) { _noisesToUpdate.Clear(); } else { _noisesToUpdate.RemoveRange(0, NoiseServiceConstants.NoisesPerUpdate - 1); } }
private void Update() { // RIGIDBODIES foreach (var s in m_stairs) { var rb = s.associated3DObject.GetComponent <Rigidbody>(); // Freeze all except if its current selected furniture if (SelectedObjectManager.Instance.currentStairs.Contains(s)) { if (s.IsOnWall) { rb.constraints = RigidbodyConstraints.FreezeRotation; rb.useGravity = false; } else { rb.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionY; } } else { rb.constraints = RigidbodyConstraints.FreezeAll; } // update 2D position if moving if (s.associated3DObject?.GetComponent <Rigidbody>().velocity.magnitude > 0) { s.Position = s.associated3DObject.transform.position; s.associated2DObject.transform.position = VectorFunctions .Switch3D2D(s.Position); // + s.Depth / 2f * s.associated3DObject.transform.forward); rb.velocity *= 0.5f; } } }
/// <summary> /// Create a new text zone. Its a comment zone shown in both views. /// </summary> private void CreateTextZone() { if (m_isCreating) { if (!m_isDragging) { if (InputFunctions.IsClickingOutsideUI()) { m_startPoint = InputFunctions.GetWorldPoint2D(GlobalManager.Instance.GetActiveCamera()); m_endPoint = InputFunctions.GetWorldPoint2D(GlobalManager.Instance.GetActiveCamera()); m_isDragging = true; currentElement = new TextZoneElement { Text = "Commentaire", BackgroundColor = Color.gray, TextColor = Color.black, TextSize = 1 }; currentElement.associated2DObject = Instantiate(textZonePrefab); currentElement.associated3DObject = Instantiate(textZonePrefab); currentElement.associated2DObject.GetComponentInChildren <TextMesh>().fontStyle = FontStyle.Bold; currentElement.associated3DObject.GetComponentInChildren <TextMesh>().fontStyle = FontStyle.Bold; currentElement.associated2DObject.SetLayerRecursively((int)ErgoLayers.Top); currentElement.associated3DObject.SetLayerRecursively((int)ErgoLayers.ThreeD); } } else { m_endPoint = InputFunctions.GetWorldPoint2D(GlobalManager.Instance.GetActiveCamera()); if (InputFunctions.IsClickingOutsideUI()) { m_isCreating = false; m_isDragging = false; m_helpers.Add(currentElement); OperationsBufferScript.Instance.AddAutoSave("Creation zone de texte"); } currentElement.associated2DObject.transform.position = (m_startPoint + m_endPoint) / 2f; currentElement.Position = VectorFunctions.Switch2D3D((m_startPoint + m_endPoint) / 2f); currentElement.associated3DObject.transform.position = currentElement.Position; (currentElement as TextZoneElement).Size = VectorFunctions.Switch2D3D(currentElement .associated2DObject.GetComponent <TextZoneScript>().SetSize(m_startPoint, m_endPoint)); currentElement.associated3DObject.GetComponent <TextZoneScript>().SetSize(m_startPoint, m_endPoint); // CANCEL if (Input.GetMouseButtonDown(1)) { m_isCreating = false; m_isDragging = false; Destroy(currentElement.associated2DObject); currentElement = null; } } } }
public void DistSquared2D_ReturnsDistanceBetween2Vectors() { var firstVector = new Vector2(-2.0f, 101.1f); var secondVector = new Vector2(12.0f, -30.0f); Assert.AreEqual(Mathf.Pow((firstVector.x - secondVector.x), 2) + Mathf.Pow((firstVector.y - secondVector.y), 2), VectorFunctions.DistanceSquared(firstVector, secondVector)); }
/// <summary> /// Update position from wanted one /// </summary> /// <param name="newPosition"></param> public virtual void SetPosition(Vector3 newPosition) { Position = newPosition; associated2DObject.transform.position = VectorFunctions.Switch3D2D(newPosition); if (associated3DObject) { associated3DObject.transform.position = newPosition; } }
private void UpdatePointTarget() { if (VectorFunctions.DistanceSquared(_navMeshAgent.destination, gameObject.transform.position) < TargetCompleteRadiusSquared) { _delegate(); PlotCourse(gameObject.transform.position); _delegate = null; } }
private void UpdateFollowTarget() { Vector2 followPosition = _followTarget.transform.position; if (VectorFunctions.DistanceSquared(followPosition, _currentTargetLocation) > DistanceSquaredThreshold) { SetTargetLocation(_followTarget.transform.position, () => {}); } }
public void Update_Trigger_LerpsToExpectedValue() { const float expectedLerp = 0.5f; BeginTriggerResponse(); _triggerResponse.TestUpdate(_triggerResponse.MoveDuration * expectedLerp); Assert.AreEqual(VectorFunctions.LerpVector(Vector3.zero, _triggerResponse.FinalPosition.transform.position, expectedLerp), _triggerResponse.gameObject.transform.position); Assert.AreEqual(VectorFunctions.LerpVector(Vector3.zero, _triggerResponse.FinalPosition.transform.eulerAngles, expectedLerp), _triggerResponse.gameObject.transform.eulerAngles); }
/// <summary> /// Set element size, updates 2d and 3d objects /// </summary> /// <param name="newSize">Wanted size</param> public virtual void SetSize(Vector3 newSize) { Size = newSize; var s = Size; var ms = MeshSize; var sOnMs = new Vector3(s.x / ms.x, s.y / ms.y, s.z / ms.z); associated3DObject.transform.localScale = sOnMs; associated2DObject.transform.localScale = VectorFunctions.Switch3D2D(sOnMs); }
/// <summary> /// Adapt sprite scaling according to mesh size and user's wanted size /// </summary> private void SetFurni2DSize() { var s = m_currentFurniture.Size; var ms = m_currentFurniture.MeshSize; var sOnMs = new Vector3(s.x / ms.x, s.y / ms.y, s.z / ms.z); m_currentFurniture.associated2DObject.transform.localScale = VectorFunctions.Switch3D2D(sOnMs); m_currentFurniture.AdjustSpriteSize(); }
protected void Update() { var currentLocation = gameObject.transform.position; if (VectorFunctions.DistanceSquared(currentLocation, _previousLocation) > NoiseListenerConstants.UpdateDistanceSquared) { UpdateListenerLocation(currentLocation); } _previousLocation = currentLocation; }
/// <summary> /// Set element size, updates 2d and 3d objects /// </summary> /// <param name="newSize">Wanted size</param> public override void SetSize(Vector3 newSize) { Size = new Vector3 { x = Mathf.Abs(Size.x), y = Mathf.Abs(Size.y), z = Mathf.Abs(Size.z) }; Size = newSize; associated2DObject.GetComponent <TextZoneScript>().bg.size = VectorFunctions.Switch3D2D(Size); associated3DObject.GetComponent <TextZoneScript>().bg.size = VectorFunctions.Switch3D2D(Size); }
/// <summary> /// Get perpendicular to adjust furniture on the wall /// </summary> public Vector3 GetPerpendicularFromPos(Vector3 pos) { var pos2D = VectorFunctions.Switch3D2D(pos); if (Vector3.Dot(Center - pos2D, Perpendicular) < 0) { //Debug.Log("Perpendicular"); return(VectorFunctions.Switch2D3D(Perpendicular)); } //Debug.Log("Pas Perpendicular"); return(VectorFunctions.Switch2D3D(-Perpendicular)); }
private void UpdateNodeStatus() { if (_pathNodes.Count == 0) { UpdateRegionPathStatus(); if (_pathNodes.Count == 0) { return; } } var targetNode = _pathNodes[0]; var currentPosition = new Vector2(gameObject.transform.position.x, gameObject.transform.position.y); if (VectorFunctions.DistanceSquared(currentPosition, targetNode.Position) < DistanceSquaredThreshold) { _pathNodes.RemoveAt(0); if (_pathNodes.Count == 0) { UpdateRegionPathStatus(); } } else { var deltaX = targetNode.Position.x - gameObject.transform.position.x; var deltaY = targetNode.Position.y - gameObject.transform.position.y; if (deltaX > 0.2f) { _movement.ApplySidewaysMotion(1.0f); } else if (deltaX < -0.2f) { _movement.ApplySidewaysMotion(-1.0f); } if (deltaY > 0.2f) { _movement.ApplyForwardMotion(1.0f); } else if (deltaY < 0.2f) { _movement.ApplyForwardMotion(-1.0f); } } }
private void Start() { keyMap = DirectionVectorMap.KeyMap( keyBind(KeyCode.W), keyBind(KeyCode.D), keyBind(KeyCode.S), keyBind(KeyCode.A) ); newPosition = VectorFunctions.move( () => (Vector2)gameObject.transform.position, keyMap, VectorFunctions.scale(0.1f) ); }
public void LerpVector_ClampsAt0() { var firstVector = new Vector3(-2.0f, 101.1f, 30.0f); var secondVector = new Vector3(12.0f, -30.0f, 27.0f); Assert.AreEqual (new Vector3 ( Mathf.Lerp(firstVector.x, secondVector.x, 0.0f), Mathf.Lerp(firstVector.y, secondVector.y, 0.0f), Mathf.Lerp(firstVector.z, secondVector.z, 0.0f) ), VectorFunctions.LerpVector(firstVector, secondVector, -1.2f) ); }
/// <summary> /// adjust 2d and 3d objects transform according to data /// </summary> /// <param name="pos3D"></param> /// <param name="go"></param> /// <param name="startingPos"></param> private void AdjustCurrentFurnitureWallPos3D(Vector3 pos3D, GameObject go, Vector3 startingPos) { if (IsOnWall) { //GetComponent<Rigidbody>().MovePosition associated3DObject.transform.position = new Vector3( pos3D.x, Mathf.Clamp(pos3D.y, 0.1f, 3), pos3D.z ); associated2DObject.transform.position = VectorFunctions.GetExactPositionFrom3DObject(associated2DObject, associated3DObject, associated3DObject.transform.position); //VectorFunctions.Switch3D2D(associated3DObject.transform.position); var w = WallsCreator.Instance.GetWallFromGameObject(go); var perp = w.GetPerpendicularFromPos(pos3D); associated3DObject.transform.rotation = Quaternion.FromToRotation(Vector3.forward, perp); associated3DObject.transform.localEulerAngles = new Vector3( 0, associated3DObject.transform.localEulerAngles.y, EulerAngles.z ); associated2DObject.transform.localEulerAngles = VectorFunctions.Switch2D3D(new Vector3( 0, -associated3DObject.transform.localEulerAngles.y, associated3DObject.transform.localEulerAngles.z )); EulerAngles = associated3DObject.transform.localEulerAngles; AssociatedElement = w; } else { if (pos3D.magnitude != float.PositiveInfinity) { associated3DObject.transform.position = pos3D; //.GetComponent<Rigidbody>().MovePosition(associated3DObject.transform.position + (pos3D - startingPos)); } associated2DObject.transform.position = this is HelperElement ? VectorFunctions.Switch3D2D(associated3DObject.transform.position) : VectorFunctions.GetExactPositionFrom3DObject(associated2DObject, associated3DObject, associated3DObject.transform.position); } }
// Update is called once per frame void Update() { if (isGrabbed) { anchorRestricted.transform.position = hand.transform.position; Vector3 handToBase = anchorRestricted.localPosition - anchorBase.localPosition; Vector3 anchorToBase = anchor.localPosition - anchorBase.localPosition; float xDifference = VectorFunctions.PolarAngleToFrom(handToBase, anchorToBase); transform.Rotate(xDifference / movementSensetivity, 0, 0, Space.Self); float localX = transform.localEulerAngles.x; if (localX > 180) { localX = -360 + localX; } if (localX > rotationLimitPositive) { transform.Rotate(-xDifference / movementSensetivity, 0, 0, Space.Self); } else if (localX < rotationLimitNegative) { transform.Rotate(-xDifference / movementSensetivity, 0, 0, Space.Self); } difference = zeroPoint.localPosition - anchor.localPosition; if (difference.z > xLimit || difference.z < -zLimit) { if ((difference.z > zLimit * 2 || difference.z < -zLimit * 2)) { handHaptics.Vibrate(VibrationForce.Hard); } else if (difference.z > zLimit * 1.5 || difference.z < -zLimit * 1.5) { handHaptics.Vibrate(VibrationForce.Medium); } else { handHaptics.Vibrate(VibrationForce.Light); } } } }
public NavNode GetNearestNavNode(NavRegion region, Vector2 position) { NavNode nearestNode = null; var nearestDistance = 100000.0f; foreach (var node in region.Nodes) { var distance = VectorFunctions.DistanceSquared(position, node.Position); if (distance < nearestDistance) { nearestNode = node; nearestDistance = distance; } } return(nearestNode); }
private void UpdateFollowTarget() { var deltaTime = GetDeltaTime(); _currentFollowDelay += deltaTime; if (_currentFollowDelay > FollowReplotDelay) { _currentFollowDelay = 0.0f; var newLocation = GetSurroundingFollowPoint(_followTarget); if (VectorFunctions.DistanceSquared(_navMeshAgent.destination, newLocation) > FollowReplotRangeSquared) { PlotCourse(newLocation); } } }
public List <LocalWildlifeResult> GetWildlifeInRadius(Vector3 inLocation, float inRadius) { var thresholdSquared = Mathf.Pow(inRadius, 2); _currentQueryResult = new List <LocalWildlifeResult>(_registeredWildlife.Count); foreach (var registeredAnimal in _registeredWildlife) { var distanceSquared = VectorFunctions.DistanceSquared(inLocation, registeredAnimal.WildlifeGameObject.transform.position); if (distanceSquared < thresholdSquared) { _currentQueryResult.Add(new LocalWildlifeResult(registeredAnimal, distanceSquared)); } } return(_currentQueryResult); }
public void GetWildlifeInRadius_ReturnsCorrectDistanceSquared() { var checkLocation = new Vector3(2.0f, 20.0f, -10.0f); _wildlife.RegisterWildlife(_species.gameObject); var retrievedWildlife = _wildlife.GetWildlifeInRadius(checkLocation, 1000f); Assert.AreEqual ( VectorFunctions.DistanceSquared ( checkLocation, _species.gameObject.transform.position ), retrievedWildlife[0].DistanceSquared ); }