• 大小: 230KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-24
  • 语言: 其他
  • 标签: 抛物线  Unity  物理  

资源简介

整个Unity项目,使用简单项目展示如何在给定抛物线起点、终点及高度的情况下,运算抛物线抛射力,实现击中目标的最终效果

资源截图

代码片段和文件信息

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShootBall : MonoBehaviour {

    public Gameobject BallPrefab;

    public Transform HighestPosT;
    public Transform StartT;
    public Transform AimT;

    float jumpgravity;
    private void Awake()
    {
        jumpgravity = Mathf.Abs(Physics.gravity.y);
    }
    // Update is called once per frame
    void Update () {

        if(Input.GetKeyDown(KeyCode.Space))
        {
            Gameobject ball = Instantiate(BallPrefab);
            ball.transform.position = StartT.position;

            float height1 = HighestPosT.position.y - StartT.position.y;
            float height2 = HighestPosT.position.y - AimT.position.y;

            float time1 = Mathf.Sqrt(2f * height1 / jumpgravity);

            float time = Mathf.Sqrt(2f * height1 / jumpgravity) + Mathf.Sqrt(2f * height2 / jumpgravity);
            //Debug.Log(“To Arc “ + time);

            Vector3 XZDist = AimT.position - StartT.position;
            XZDist.y = 0;
            Vector3 vel = XZDist / time;
            float t = 0;


            Vector3 velvalue = vel + time1 * jumpgravity * Vector3.up;
            StartCoroutine(AddForceLater(ball.GetComponent() velvalue));

        }
}

    IEnumerator AddForceLater(Rigidbody BallRig Vector3 velvalue)
    {
        yield return new WaitForFixedUpdate();


        BallRig.transform.rotation = Quaternion.LookRotation(velvalue);
        BallRig.GetComponent().velocity = Vector3.zero;
        BallRig.GetComponent().AddForce(velvalue ForceMode.VelocityChange);
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    ..A..H.     14336  2019-01-08 11:07  ShootTarget\.vs\ShootTarget\v15\.suo

    I.A....      2054  2019-01-08 10:59  ShootTarget\Assets\Mat\Grey.mat

    I.A....       188  2019-01-08 10:57  ShootTarget\Assets\Mat\Grey.mat.meta

    I.A....      2027  2019-01-08 10:57  ShootTarget\Assets\Mat\Red.mat

     文件        188  2019-01-08 10:57  ShootTarget\Assets\Mat\Red.mat.meta

    I.A....      3099  2019-01-08 11:00  ShootTarget\Assets\Mat\Sphere.prefab

     文件        154  2019-01-08 11:00  ShootTarget\Assets\Mat\Sphere.prefab.meta

    I.A....      2038  2019-01-08 11:00  ShootTarget\Assets\Mat\Yellow.mat

    I.A....       188  2019-01-08 10:59  ShootTarget\Assets\Mat\Yellow.mat.meta

     文件        172  2019-01-08 10:57  ShootTarget\Assets\Mat.meta

    I.A....     39840  2019-01-08 11:06  ShootTarget\Assets\Scenes\SampleScene.unity

     文件        155  2019-01-08 10:54  ShootTarget\Assets\Scenes\SampleScene.unity.meta

     文件        172  2019-01-08 10:54  ShootTarget\Assets\Scenes.meta

     文件       1690  2018-12-06 16:02  ShootTarget\Assets\ShootBall.cs

     文件        243  2019-01-08 10:59  ShootTarget\Assets\ShootBall.cs.meta

     文件        377  2018-12-11 00:23  ShootTarget\ProjectSettings\AudioManager.asset

     文件        120  2018-12-11 00:23  ShootTarget\ProjectSettings\ClusterInputManager.asset

     文件       1105  2018-12-11 00:23  ShootTarget\ProjectSettings\DynamicsManager.asset

     文件        268  2018-12-11 00:23  ShootTarget\ProjectSettings\EditorBuildSettings.asset

     文件        643  2018-12-11 00:23  ShootTarget\ProjectSettings\EditorSettings.asset

     文件       2283  2018-12-11 00:23  ShootTarget\ProjectSettings\GraphicsSettings.asset

     文件       6088  2018-12-11 00:23  ShootTarget\ProjectSettings\InputManager.asset

     文件       1399  2018-12-11 00:23  ShootTarget\ProjectSettings\NavMeshAreas.asset

     文件        159  2018-12-11 00:23  ShootTarget\ProjectSettings\NetworkManager.asset

     文件       1469  2018-12-11 00:23  ShootTarget\ProjectSettings\Physics2DSettings.asset

     文件        824  2018-12-11 00:23  ShootTarget\ProjectSettings\PresetManager.asset

    I.A....     17619  2019-01-08 10:55  ShootTarget\ProjectSettings\ProjectSettings.asset

     文件         28  2019-01-08 10:54  ShootTarget\ProjectSettings\ProjectVersion.txt

     文件       5031  2018-12-11 00:23  ShootTarget\ProjectSettings\QualitySettings.asset

     文件        435  2018-12-11 00:23  ShootTarget\ProjectSettings\TagManager.asset

............此处省略21个文件信息

评论

共有 条评论