void Update() { if (this.characterScript == null) { return; } if (this.speechScript != null && this.speechScript.Active) { // Player can't move or do anything else but interact with the current speech... this.characterScript.Moving = false; if (Input.GetButtonDown("Action")) { this.speechScript.Advance(); } return; } if (Input.GetButtonDown("Action")) { this.characterScript.Action(); } this.characterScript.Running = Input.GetButton("Run"); var targetPoint = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0.0f); if (Vector3.Distance(Vector3.zero, targetPoint) < 0.5f) { this.characterScript.Moving = false; } else { this.characterScript.Direction = 360.0f - (MathUtils.AngleBetween(Vector3.zero, targetPoint) + 270.0f); this.characterScript.Moving = true; } if (this.coordinatesText != null) { if (this.character.transform.parent != null) { this.coordinatesText.text = "X: " + this.character.transform.parent.gameObject.transform.position.x + Environment.NewLine + "Y: " + this.character.transform.parent.gameObject.transform.position.y + Environment.NewLine + "Z: " + this.character.transform.position.z + Environment.NewLine + "A: " + this.character.transform.rotation.eulerAngles.y; } else { this.coordinatesText.text = "X: " + this.character.transform.position.x + Environment.NewLine + "Y: " + this.character.transform.position.y + Environment.NewLine + "Z: " + this.character.transform.position.z + Environment.NewLine + "A: " + this.character.transform.rotation.eulerAngles.y; } } }
public void AngleBetween_Tests(int x, int y, int expectedAngle) { var angles = Enumerable .Range(1, 5) .Select(n => new Vector2(x * n * 100, y * n * 100)) .Select(v => { Console.WriteLine(v); return(MathUtils.AngleBetween(Vector2.UnitY, v)); }) .ToArray(); angles.Should().AllBeEquivalentTo(expectedAngle); }
void Update() { if (this.characterScript == null) { return; } if (!Input.GetMouseButton(0)) { this.characterScript.Moving = this.characterScript.Running = false; return; } var targetPoint = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f)); targetPoint.z = 0.0f; this.characterScript.Direction = 360.0f - (MathUtils.AngleBetween(this.character.transform.position, targetPoint) + 270.0f); this.characterScript.Moving = true; this.characterScript.Running = Vector3.Distance(this.character.transform.position, targetPoint) >= this.RunDistance; }
public void GetChildEdge(ref EdgeShape edge, int index) { Debug.Assert(2 <= this.Vertices.Count); Debug.Assert(0 <= index && index < this.Vertices.Count - 1); edge.ShapeType = ShapeType.Edge; edge._radius = this._radius; edge.Vertex1 = this.Vertices[index + 0]; edge.Vertex2 = this.Vertices[index + 1]; if (index > 0) { edge.Vertex0 = this.Vertices[index - 1]; edge.HasVertex0 = true; } else { edge.Vertex0 = this.PrevVertex; edge.HasVertex0 = this.HasPrevVertex; } if (index < this.Vertices.Count - 2) { edge.Vertex3 = this.Vertices[index + 2]; edge.HasVertex3 = true; } else { edge.Vertex3 = this.NextVertex; edge.HasVertex3 = this.HasNextVertex; } // Old hack-fix for jittery collision at sharp (<90°) angles. // See here: https://github.com/AdamsLair/duality/commit/924b25119a6634c77e71175e7f275db3d3d4e9dd edge.HasVertex0 = edge.HasVertex0 && MathUtils.AngleBetween(edge.Vertex0, edge.Vertex1) > MathHelper.PiOver2; edge.HasVertex3 = edge.HasVertex3 && MathUtils.AngleBetween(edge.Vertex2, edge.Vertex3) > MathHelper.PiOver2; }
public void SetAnimationSetForCamera(Camera camera) { //Work out angle between character face direction and camera forward. //Choose a animation set based on horizontal angle between camera forward and character forward Transform graphicsOrigin = _graphicsOrigin; if (graphicsOrigin == null) { graphicsOrigin = this.transform; } //Convert camera pos and forward into character space Vector3 localspaceCameraPos = _graphicsOrigin.InverseTransformPoint(camera.transform.position); Vector3 localspaceCameraDir = _graphicsOrigin.InverseTransformDirection(-camera.transform.forward); //Get forward in XY space Vector2 localspaceCameraDirXY = new Vector2(localspaceCameraDir.x, localspaceCameraDir.z).normalized; Vector2 forwardXY = Vector2.up; //The angle between camera forward and character forward float horizAngle = MathUtils.AngleBetween(forwardXY, localspaceCameraDirXY); //Work out which animations to use int bestAnimationSet = -1; float nearestAngleDif = 0.0f; for (int i = 0; i < _animationSets.Length; i++) { //Disable the sets renderer SetAnimationSetActive(_animationSets[i], false); //Never use sprites that direction is more than 90 degrees to camera Vector2 spriteForwardXY = MathUtils.Rotate(forwardXY, _animationSets[i]._fowardAngle); float spriteAngle = MathUtils.AngleBetween(localspaceCameraDirXY, spriteForwardXY); if (Mathf.Abs(spriteAngle) < _animationSets[i]._maxAngle) { float angleDiff = MathUtils.AngleDiff(horizAngle, MathUtils.DegreesTo180Range(_animationSets[i]._faceAngle)); if (bestAnimationSet == -1 || Mathf.Abs(angleDiff) < Mathf.Abs(nearestAngleDif)) { bestAnimationSet = i; nearestAngleDif = angleDiff; } } } if (bestAnimationSet != -1) { Spine3DAnimationSet animationSet = _animationSets[bestAnimationSet]; SetAnimationSetActive(animationSet, true); if (_onRenderAnimationSet != null) { _onRenderAnimationSet.Invoke(camera, animationSet); } //Horizontal sprite rotation { //Rotate this forward to face sprite towards its face angle. Vector2 localSpaceSpriteForwardXY = MathUtils.Rotate(forwardXY, -animationSet._faceAngle); //When hit max view angle maintain that relative angle diff. float spriteAngle = MathUtils.AngleBetween(localspaceCameraDirXY, localSpaceSpriteForwardXY); float spriteMaxAngle = animationSet._maxViewAngle; if (Mathf.Abs(spriteAngle) > spriteMaxAngle) { //Rotate camera forward by max angle in correct direction float clampedAngle = spriteMaxAngle; if (spriteAngle < 0) { clampedAngle = -clampedAngle; } localSpaceSpriteForwardXY = MathUtils.Rotate(localspaceCameraDirXY, -clampedAngle); } //Set rotation matrix based off adjusted up and the correct sprite forward vector { Vector3 spriteForward = new Vector3(localSpaceSpriteForwardXY.x, 0.0f, localSpaceSpriteForwardXY.y).normalized; animationSet.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, -spriteForward); } } //Vertical sprite rotation { Vector3 localSpaceCamerDirFlat = new Vector3(localspaceCameraDir.x, 0.0f, localspaceCameraDir.z).normalized; //Work out roll angle between sprite and camera float rollAngle = Vector3.Angle(localSpaceCamerDirFlat, localspaceCameraDir); if (localspaceCameraDir.y < 0) { rollAngle = -rollAngle; } //If roll angle is too severe then rotate sprite to align better with the camera if (Mathf.Abs(rollAngle) > _minRollAngle) { //ifs over min angle then rotate by amount over float angle = Mathf.Abs(rollAngle) - _minRollAngle; if (angle > _maxRollAngle) { angle = _maxRollAngle; } if (rollAngle < 0.0f) { angle = -angle; } Vector3 spriteAxis = Vector3.Cross(Vector3.up, localspaceCameraDir).normalized; animationSet.transform.localRotation *= Quaternion.AngleAxis(angle, Vector3.right); } } } }