赞
踩
using UnityEngine;
using System.Collections;
public class ProjectileDragging : MonoBehaviour {
public float maxStretch = 3.0f;//拉伸长度
public LineRenderer catapultLineFront;//弹弓皮筋前面
public LineRenderer catapultLineBack;//弹弓皮筋后面
private SpringJoint2D spring;//弹簧铰链
private Transform catapult;//弹弓
private Ray leftCatapultTpProjectile;//用来获取皮筋与投射物接触点的位置
private Ray rayToMouse;//用来限制物体的位置
private float maxStretchSql;//拉伸长度的平方
private Rigidbody2D rb;//刚体
private CircleCollider2D circle;//圆形碰撞器
private float circleRadius;//圆形碰撞器半径
private bool clickedOn;//判断是否是鼠标按下中
private Vector2 prevVelocity;//发射出去的速度
void Awake(){
spring = GetComponent<SpringJoint2D> ();
rb = GetComponent<Rigidbody2D> ();
circle = GetComponent<CircleCollider2D> ();
catapult = spring.connectedBody.transform;
}
// Use this for initialization
void Start () {
LineRendererSetUp ();
rayToMouse = new Ray (catapult.position,Vector3.zero);
leftCatapultTpProjectile = new Ray (catapultLineFront.transform.position,Vector3.zero);
maxStretchSql = maxStretch * maxStretch;
circleRadius = circle.radius;
}
// Update is called once per frame
void Update () {
if (clickedOn)
Dragging ();
if (spring != null) {//如果弹簧铰链不为空
if (!rb.isKinematic && prevVelocity.sqrMagnitude > rb.velocity.sqrMagnitude) {//如果投射物可以受力,且投射速度大于物体自身速度
Destroy (spring);//脱离铰链
rb.velocity = prevVelocity;//投射
}
if (!clickedOn)
prevVelocity = rb.velocity;//如果拖拽完成,投射速度等于物体自身的速度
LineRendererUpdate ();//更新弹弓皮筋
} else {
catapultLineFront.enabled = false;
catapultLineBack.enabled = false;
}
}
private void LineRendererSetUp(){//初始弹弓皮筋
catapultLineFront.SetPosition (0,catapultLineFront.transform.position);//设置皮筋与弹弓相连的点的位置
catapultLineBack.SetPosition (0,catapultLineBack.transform.position);
catapultLineFront.sortingLayerName = "ForeGround";//设置皮筋的渲染层
catapultLineBack.sortingLayerName = "ForeGround";
catapultLineFront.sortingOrder = 3;//设置同一层的渲染顺序
catapultLineBack.sortingOrder = 1;
}
void OnMouseDown(){
spring.enabled = false;//拖拽过程中,弹簧铰链失效
clickedOn = true;//拖拽中
}
void OnMouseUp(){
spring.enabled = true;//拖拽完后,恢复铰链
rb.isKinematic = false;//拖拽完后,使投射物受力
clickedOn = false;
}
private void Dragging (){//拖拽中持续更新物体位置
Vector3 mouseWordPoint = (Camera.main.ScreenToWorldPoint (Input.mousePosition));
Vector2 catapultToMouse = mouseWordPoint - catapult.position;
if (catapultToMouse.sqrMagnitude > maxStretchSql) {//限制拖拽长度
rayToMouse.direction = catapultToMouse;
mouseWordPoint = rayToMouse.GetPoint (maxStretch);//在射线的方向上找一个距离射线源点maxStretch的点
}
mouseWordPoint.z = 0;
transform.position = mouseWordPoint;
}
private void LineRendererUpdate(){//更新弹弓皮筋
Vector2 catapultToProjectile = transform.position-catapultLineFront.transform.position;
leftCatapultTpProjectile.direction = catapultToProjectile;
Vector3 holdPoint = leftCatapultTpProjectile.GetPoint (catapultToProjectile.magnitude+circleRadius);
catapultLineFront.SetPosition (1,holdPoint);
catapultLineBack.SetPosition (1,holdPoint);
}
}
using UnityEngine;
using System.Collections;
public class ProjectileFollow : MonoBehaviour {
public Transform projectile;//投射物
public Transform farLeft;//相机左边界
public Transform farRight;//相机右边界
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 newPosition = transform.position;
newPosition.x = projectile.position.x;
newPosition.x = Mathf.Clamp (newPosition.x,farLeft.position.x,farRight.position.x);//限定相机的位置
transform.position = newPosition;
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class Resetter : MonoBehaviour {
public Rigidbody2D projectile;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.R))
ReSet ();
}
void OnTriggerExit2D(Collider2D col){
if (col.GetComponent<Rigidbody2D> () == projectile)
ReSet ();
}
private void ReSet(){
SceneManager.LoadScene (SceneManager.GetActiveScene().name);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。