• Unity 刚体移动,自方向移动


     场景

    发射点

    代码

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

    public class Trajectory : MonoBehaviour
    {
    public GameObject target;
    public GameObject rotatepos;
    public GameObject game;
    public float time = 1;
    private GameObject pos;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hitInfo;
    if (Physics.Raycast(ray, out hitInfo))
    {
    Debug.DrawLine(ray.origin, hitInfo.point);//scene視圖可看到 DrawLine(Vector3 origin,Vector3 end,Color col):衹有儅發生碰撞時,在Scene視圖才可以看到畫出的射綫。
    this.transform.position = new Vector3(hitInfo.point.x, 0f, hitInfo.point.z);
    }

    Vector3 dir = target.transform.position - rotatepos.transform.position;
    Quaternion lookRotation = Quaternion.LookRotation(dir);
    Vector3 rotation = Quaternion.Lerp(rotatepos.transform.rotation, lookRotation, 10).eulerAngles;
    rotatepos.transform.rotation = Quaternion.Euler(0f, rotation.y, 0f);

    time -= Time.deltaTime;
    if (time<0)
    {
    time = 0.25f;
    pos= Instantiate(game);
    pos.transform.position = this.transform.position;
    pos.transform.eulerAngles = rotatepos.transform.eulerAngles;
    print(rotatepos.transform.eulerAngles);


    }



    }
    }

     

     子弹属性

     

    刚体的移动  方向,是通过  获取移动对象来判断移动方向的

    手动创建的值,是世界方向

      body.velocity = new Vector3(50,0,0);

    获取对象的值在加值,可以选择物体方向还是世界方向

        body.velocity = this.transform.forward * 50;

     效果

     

     

     都是朝向目标点发射移动的

  • 相关阅读:
    SQL Server未找到或无法訪问server问题解决
    cmd 控制台 提示:请求的操作须要提升!
    网站反爬虫
    辨异 —— 近义词(词组)
    推理集 —— 现象与观察
    推理集 —— 现象与观察
    生活中的物理、化学(四)
    生活中的物理、化学(四)
    中医我还是持赞成的态度
    文言的训练和积累
  • 原文地址:https://www.cnblogs.com/suiyi78/p/13512314.html
Copyright © 2020-2023  润新知