public void Draw() { Vector2 difference = pointB - pointA; float Rotation = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg; LineChild.transform.localScale = new Vector3(100 * (difference.magnitude / LineChild.GetComponent <SpriteRenderer> ().sprite.rect.width), lightningThickness, LineChild.transform.localScale.z); StartCapChild.transform.localScale = new Vector3(StartCapChild.transform.localScale.x, lightningThickness, StartCapChild.transform.localScale.z); EndCapChild.transform.localScale = new Vector3(EndCapChild.transform.localScale.x, lightningThickness, EndCapChild.transform.localScale.z); LineChild.transform.rotation = Quaternion.Euler(new Vector3(0, 0, Rotation)); StartCapChild.transform.rotation = Quaternion.Euler(new Vector3(0, 0, Rotation)); EndCapChild.transform.rotation = Quaternion.Euler(new Vector3(0, 0, Rotation + 180)); LineChild.transform.position = new Vector3(pointA.x, pointA.y, LineChild.transform.position.z); StartCapChild.transform.position = new Vector3(pointA.x, pointA.y, StartCapChild.transform.position.z); EndCapChild.transform.position = new Vector3(pointA.x, pointA.y, EndCapChild.transform.position.z); Rotation *= Mathf.Deg2Rad; float LineChildWorldAdjust = LineChild.transform.localScale.x * LineChild.GetComponent <SpriteRenderer> ().sprite.rect.width / 2f; float StartCapChildWorldAdjust = StartCapChild.transform.localScale.x * StartCapChild.GetComponent <SpriteRenderer> ().sprite.rect.width / 2f; float EndCapChildWorldAdjust = EndCapChild.transform.localScale.x * EndCapChild.GetComponent <SpriteRenderer> ().sprite.rect.width / 2f; LineChild.transform.position += new Vector3(0.01f * Mathf.Cos(Rotation) * LineChildWorldAdjust, 0.01f * Mathf.Sin(Rotation) * LineChildWorldAdjust, 0); StartCapChild.transform.position -= new Vector3(0.01f * Mathf.Cos(Rotation) * StartCapChildWorldAdjust, 0.01f * Mathf.Sin(Rotation) * StartCapChildWorldAdjust, 0); EndCapChild.transform.position += new Vector3(0.01f * Mathf.Cos(Rotation) * LineChildWorldAdjust * 2, 0.01f * Mathf.Sin(Rotation) * LineChildWorldAdjust * 2, 0); EndCapChild.transform.position += new Vector3(0.01f * Mathf.Cos(Rotation) * EndCapChildWorldAdjust, 0.01f * Mathf.Sin(Rotation) * EndCapChildWorldAdjust, 0); }
public void Draw() { Vector2 difference = B - A; float rotation = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg; //设置线的缩放来体现出长度和宽度 LineChild.transform.localScale = new Vector3(100 * (difference.magnitude / LineChild.GetComponent <SpriteRenderer>().sprite.rect.width), Thickness, LineChild.transform.localScale.z); StartCapChild.transform.localScale = new Vector3(StartCapChild.transform.localScale.x, Thickness, StartCapChild.transform.localScale.z); EndCapChild.transform.localScale = new Vector3(EndCapChild.transform.localScale.x, Thickness, EndCapChild.transform.localScale.z); //旋转线让它朝向正确的方向 LineChild.transform.rotation = Quaternion.Euler(new Vector3(0, 0, rotation)); StartCapChild.transform.rotation = Quaternion.Euler(new Vector3(0, 0, rotation)); EndCapChild.transform.rotation = Quaternion.Euler(new Vector3(0, 0, rotation)); //如果头尾图片一样,rotation要+180 //移动线段到以起点为中心 LineChild.transform.position = new Vector3(A.x, A.y, LineChild.transform.position.z); StartCapChild.transform.position = new Vector3(A.x, A.y, StartCapChild.transform.position.z); EndCapChild.transform.position = new Vector3(A.x, A.y, EndCapChild.transform.position.z); //需要把旋转度数转化为弧度进行运算 rotation *= Mathf.Deg2Rad; //存储这些值这样就只需访问一次 float lineChildWorldAdjust = LineChild.transform.localScale.x * LineChild.GetComponent <SpriteRenderer>().sprite.rect.width / 2f; float startCapChildWorldAdjust = StartCapChild.transform.localScale.x * StartCapChild.GetComponent <SpriteRenderer>().sprite.rect.width / 2f; float endCapChildWorldAdjust = EndCapChild.transform.localScale.x * EndCapChild.GetComponent <SpriteRenderer>().sprite.rect.width / 2f; //调整中间段到合适的位置 LineChild.transform.position += new Vector3(.01f * Mathf.Cos(rotation) * lineChildWorldAdjust, .01f * Mathf.Sin(rotation) * lineChildWorldAdjust, 0); //调整开始段到合适的位置 StartCapChild.transform.position -= new Vector3(.01f * Mathf.Cos(rotation) * startCapChildWorldAdjust, .01f * Mathf.Sin(rotation) * startCapChildWorldAdjust, 0); //调整终点段到合适的位置 EndCapChild.transform.position += new Vector3(.01f * Mathf.Cos(rotation) * lineChildWorldAdjust * 2, .01f * Mathf.Sin(rotation) * lineChildWorldAdjust * 2, 0); EndCapChild.transform.position += new Vector3(.01f * Mathf.Cos(rotation) * endCapChildWorldAdjust, .01f * Mathf.Sin(rotation) * endCapChildWorldAdjust, 0); }
public void SetColor(Color color) { StartCapChild.GetComponent <SpriteRenderer> ().color = color; StartCapChild.GetComponent <Light> ().color = color; LineChild.GetComponent <SpriteRenderer> ().color = color; LineChild.GetComponent <Light>().color = color; EndCapChild.GetComponent <SpriteRenderer> ().color = color; EndCapChild.GetComponent <Light> ().color = color; // this.GetComponent<Light> ().color = color; }
public void Draw() { Vector2 difference = EndPos - StartPos; float rotation = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg; //Set the scale of the line to reflect length and thickness LineChild.transform.localScale = new Vector3(100 * (difference.magnitude / LineChild.GetComponent <SpriteRenderer>().sprite.rect.width), Thickness, LineChild.transform.localScale.z); StartCapChild.transform.localScale = new Vector3(StartCapChild.transform.localScale.x, Thickness, StartCapChild.transform.localScale.z); EndCapChild.transform.localScale = new Vector3(EndCapChild.transform.localScale.x, Thickness, EndCapChild.transform.localScale.z); //Rotate the line so that it is facing the right direction LineChild.transform.rotation = Quaternion.Euler(new Vector3(0, 0, rotation)); StartCapChild.transform.rotation = Quaternion.Euler(new Vector3(0, 0, rotation)); EndCapChild.transform.rotation = Quaternion.Euler(new Vector3(0, 0, rotation + 180)); //Move the line to be centered on the starting point LineChild.transform.position = new Vector3(StartPos.x, StartPos.y, LineChild.transform.position.z); StartCapChild.transform.position = new Vector3(StartPos.x, StartPos.y, StartCapChild.transform.position.z); EndCapChild.transform.position = new Vector3(StartPos.x, StartPos.y, EndCapChild.transform.position.z); //Need to convert rotation to radians at this point for Cos/Sin rotation *= Mathf.Deg2Rad; //Store these so we only have to access once float lineChildWorldAdjust = LineChild.transform.localScale.x * LineChild.GetComponent <SpriteRenderer>().sprite.rect.width / 2f; float startCapChildWorldAdjust = StartCapChild.transform.localScale.x * StartCapChild.GetComponent <SpriteRenderer>().sprite.rect.width / 2f; float endCapChildWorldAdjust = EndCapChild.transform.localScale.x * EndCapChild.GetComponent <SpriteRenderer>().sprite.rect.width / 2f; //Adjust the middle segment to the appropriate position LineChild.transform.position += new Vector3(.01f * Mathf.Cos(rotation) * lineChildWorldAdjust, .01f * Mathf.Sin(rotation) * lineChildWorldAdjust, 0); //Adjust the start cap to the appropriate position StartCapChild.transform.position -= new Vector3(.01f * Mathf.Cos(rotation) * startCapChildWorldAdjust, .01f * Mathf.Sin(rotation) * startCapChildWorldAdjust, 0); //Adjust the end cap to the appropriate position EndCapChild.transform.position += new Vector3(.01f * Mathf.Cos(rotation) * lineChildWorldAdjust * 2, .01f * Mathf.Sin(rotation) * lineChildWorldAdjust * 2, 0); EndCapChild.transform.position += new Vector3(.01f * Mathf.Cos(rotation) * endCapChildWorldAdjust, .01f * Mathf.Sin(rotation) * endCapChildWorldAdjust, 0); }
public void Draw() { Vector2 difference = B - A; float rotation = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg; LineChild.transform.localScale = new Vector3(100 * (difference.magnitude / LineChild.GetComponent <SpriteRenderer> ().sprite.rect.width) , Thickness , LineChild.transform.localScale.z); StartCapChild.transform.localScale = new Vector3(StartCapChild.transform.localScale.x , Thickness , StartCapChild.transform.localScale.z); EndCapChild.transform.localScale = new Vector3(EndCapChild.transform.localScale.x , Thickness , EndCapChild.transform.localScale.z); LineChild.transform.rotation = Quaternion.Euler(new Vector3(90, 0, rotation)); StartCapChild.transform.rotation = Quaternion.Euler(new Vector3(90, 0, rotation)); EndCapChild.transform.rotation = Quaternion.Euler(new Vector3(90, 0, rotation + 180)); LineChild.transform.position = new Vector3(A.x, layer, A.y); StartCapChild.transform.position = new Vector3(A.x, layer, A.y); EndCapChild.transform.position = new Vector3(A.x, layer, A.y); rotation *= Mathf.Deg2Rad; float lineChildWorldAdjust = LineChild.transform.localScale.x * LineChild.GetComponent <SpriteRenderer> ().sprite.rect.width / 2f; float startCapChildWorldAdjust = StartCapChild.transform.localScale.x * StartCapChild.GetComponent <SpriteRenderer> ().sprite.rect.width / 2f; float endCapChildWorldAdjust = EndCapChild.transform.localScale.x * EndCapChild.GetComponent <SpriteRenderer> ().sprite.rect.width / 2f; LineChild.transform.position += new Vector3(.01f * Mathf.Cos(rotation) * lineChildWorldAdjust , 0, .01f * Mathf.Sin(rotation) * lineChildWorldAdjust); StartCapChild.transform.position -= new Vector3(.01f * Mathf.Cos(rotation) * startCapChildWorldAdjust , 0, .01f * Mathf.Sin(rotation) * startCapChildWorldAdjust); EndCapChild.transform.position += new Vector3(.01f * Mathf.Cos(rotation) * lineChildWorldAdjust * 2 , 0, .01f * Mathf.Sin(rotation) * lineChildWorldAdjust * 2); EndCapChild.transform.position += new Vector3(.01f * Mathf.Cos(rotation) * endCapChildWorldAdjust , 0, .01f * Mathf.Sin(rotation) * endCapChildWorldAdjust); }