Unity3d让物体成弧线运动(可以制作炮弹发射的轨迹)

作者:雨辰 发布于:2016-9-1 22:22 Thursday 分类:Unity3D

第一种实现
using UnityEngine;
using System.Collections;

public class MoveCurve : MonoBehaviour {

    public GameObject t1;    //开始位置
    public GameObject t2;     //结束位置
  // Update is called once per frame
    void Update () {

//两者中心点
        Vector3 center = (t1.transform .position + t2.transform.position) * 0.5f;
        center -= new Vector3(0, 1, 0);
        Vector3 start = t1.transform.position - center;
        Vector3 end = t2.transform.position - center;
        //弧形插值
        transform.position = Vector3.Slerp(start,end,Time.time);
        transform.position += center;
    }
}

第二中实现
using UnityEngine;
using System.Collections;

public class ProjectileTest : MonoBehaviour
{
    public GameObject target;   //要到达的目标
    public float speed = 10;    //速度
    private float distanceToTarget;   //两者之间的距离
    private bool move = true;    

    void Start()
    {
        //计算两者之间的距离
        distanceToTarget = Vector3.Distance(this.transform.position, target.transform.position);  
        StartCoroutine(StartShoot());
    }

    IEnumerator StartShoot()
    {

        while (move)
        {
            Vector3 targetPos = target.transform.position;

            //让始终它朝着目标
            this.transform.LookAt(targetPos);

            //计算弧线中的夹角
            float angle = Mathf.Min(1, Vector3.Distance(this.transform.position, targetPos) / distanceToTarget) * 45;
            this.transform.rotation = this.transform.rotation * Quaternion.Euler(Mathf.Clamp(-angle, -42, 42), 0, 0);
            float currentDist = Vector3.Distance(this.transform.position, target.transform.position);
            if (currentDist < 0.5f)
                move = false;
            this.transform.Translate(Vector3.forward * Mathf.Min(speed * Time.deltaTime, currentDist));
            yield return null;
        }
    }


}

第三种方法
public class Bullet : MonoBehaviour
{
    public const float g = 9.8f;
 
    public GameObject target;
    public float speed = 10;
    private float verticalSpeed;
    private Vector3 moveDirection;
 
    private float angleSpeed;
    private float angle;
    void Start()
    {
       float tmepDistance = Vector3.Distance(transform.position, target.transform.position);
        float tempTime = tmepDistance / speed;
        float riseTime, downTime;
        riseTime = downTime = tempTime / 2;
         verticalSpeed = g * riseTime;
        transform.LookAt(target.transform.position);
 
        float tempTan = verticalSpeed / speed;
        double hu = Math.Atan(tempTan);
        angle = (float)(180 / Math.PI * hu);
        transform.eulerAngles = new Vector3(-angle, transform.eulerAngles.y, transform.eulerAngles.z);
        angleSpeed = angle / riseTime;
 
        moveDirection = target.transform.position - transform.position;
    }
    private float time;
    void Update()
    {
        if (transform.position.y < target.transform.position.y)
        {
            //finish
            return;
        }
        time += Time.deltaTime;
        float test = verticalSpeed - g * time;
        transform.Translate(moveDirection.normalized * speed * Time.deltaTime, Space.World);
        transform.Translate(Vector3.up * test * Time.deltaTime,Space.World);
        float testAngle = -angle + angleSpeed * time;
        transform.eulerAngles = new Vector3(testAngle, transform.eulerAngles.y, transform.eulerAngles.z);
    }
}








	

标签: Unity3D

发表评论:

雨辰 joyimp|@2011-2018 京ICP备16030765号