Unity3d空间与运动

  1. 游戏对象运动的本质是什么?
    运动的本质是游戏对象通过脚本变化其(position)位置,(rotation)欧拉角,(scale)形状。
  2. 请用三种方法以上方法,实现物体的抛物线运动。(如,修改Transform属性,使用向量Vector3的方法…)
  3. 改变Transform属性:

    public float Power = 10;//这个代表发射时的速度/力度等,可以通过此来模拟不同的力大小
    
    public float Angle = 45;//发射的角度,这个就不用解释了吧
    
    public float Gravity = -10;//这个代表重力加速度
    
    
    
    private Vector3 MoveSpeed;//初速度向量
    
    private Vector3 GritySpeed = Vector3.zero;//重力的速度向量,t时为0
    
    private float dTime;//已经过去的时间
    
    private Vector3 currentAngle;
    
    // Use this for initialization
    
    void Start()
    
    {
    
        //通过一个公式计算出初速度向量
    
        //角度*力度
    
        MoveSpeed = Quaternion.Euler(new Vector3(0, 0, Angle)) * Vector3.right * Power;
    
        currentAngle = Vector3.zero;
    
    }
    
    // Update is called once per frame
    
    void FixedUpdate()
    
    {
    
        //计算物体的重力速度
    
        //v = at ;
    
        GritySpeed.y = Gravity * (dTime += Time.fixedDeltaTime);
    
        //位移模拟轨迹
    
        transform.position += (MoveSpeed + GritySpeed) * Time.fixedDeltaTime;
    
        currentAngle.z = Mathf.Atan((MoveSpeed.y + GritySpeed.y) / MoveSpeed.x) * Mathf.Rad2Deg;
    
        transform.eulerAngles = currentAngle;
    
    }
    1. 使用Translate平移坐标,同时改变x,y方向的位置以及通过重力改变y方向的位置:

      public float g=-10;//重力加速度
      private Vector3 speed;//初速度向量
      private Vector3 Gravity;//重力向量
      void Start () {
      Gravity = Vector3.zero;//重力初始速度为0
      speed = new Vector3(10,10,0);
      }
      private float dTime=0;
      // Update is called once per frame
      void FixedUpdate () {
      
      Gravity.y = g * (dTime += Time.fixedDeltaTime);//v=at
      //模拟位移
      transform.Translate(speed*Time.fixedDeltaTime);
      transform.Translate(Gravity * Time.fixedDeltaTime);
      }
  4. 使用Vector3的方法:
    C#
    Vector3 center = (sunrise.position + sunset.position) * 0.5F;
    //找中心点
    center -= new Vector3(0, 1, 0);
    //求出新的中心点到向量a和向量b的向量
    Vector3 riseRelCenter = sunrise.position - center;
    Vector3 setRelCenter = sunset.position - center;
    transform.position = Vector3.Slerp(riseRelCenter, setRelCenter, journeyTime);
    //对起始点和结束点修正
    transform.position += center;
    //说实话,这个方法使用起来是真的麻烦。

  5. 使用rigid body对物体施加一个重力效果而不用自己实现重力使物体做抛物线:

    private Vector3 speed;
    public float power;
    public float angle;
    // Use this for initialization
    void Start () {
        speed = Quaternion.Euler (new Vector3 (0, 0, angle)) * Vector3.right * power;
    }
    
    // Update is called once per frame
    void Update () {
        transform.position += speed * Time.deltaTime;
    }
    
    ![这里写图片描述](https://img-blog.csdn.net/20180330102443677?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2hlbGxvd2FuZ2xk/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
    
  6. 写一个程序,实现一个完整的太阳系, 其他星球围绕太阳的转速必须不一样,且不在一个法平面上。
public Transform mercury;
    public Transform venus;
    public Transform earth;
    public Transform mars;
    public Transform jupiter;
    public Transform saturn;
    public Transform uranus;
    public Transform neptune;
    void Start () {
        mercury.position = new Vector3 (3, 0, 0);
        venus.position = new Vector3 (-5, 0, 0);
        earth.position = new Vector3 (7, 0, 0);
        mars.position = new Vector3 (-9, 0, 0);
        jupiter.position = new Vector3 (-11, 0, 0);
        saturn.position = new Vector3 (13, 0, 0);
        uranus.position = new Vector3 (15, 0, 0);
        neptune.position = new Vector3 (-17, 0, 0);
    }

    void Update () {
        earth.RotateAround (this.transform.position, new Vector3(0, 0.99f, 0), 30 * Time.deltaTime);
        mercury.RotateAround (this.transform.position, new Vector3(0, 2.11f, 0), 47 * Time.deltaTime);
        venus.RotateAround (this.transform.position, new Vector3(0, 3.23f, 0), 35 * Time.deltaTime);
        mars.RotateAround (this.transform.position, new Vector3(0, 4.34f, 0), 24 * Time.deltaTime);
        jupiter.RotateAround (this.transform.position, new Vector3(0, 1.02f, 0), 13 * Time.deltaTime);
        saturn.RotateAround (this.transform.position, new Vector3(0, 0.98f, 0), 9 * Time.deltaTime);
        uranus.RotateAround (this.transform.position, new Vector3(0, 0.97f, 0),  6 * Time.deltaTime);
        neptune.RotateAround (this.transform.position, new Vector3(0, 0.96f, 0), 5 * Time.deltaTime);
        earth.Rotate (Vector3.up * Time.deltaTime * 250);
        mercury.Rotate (Vector3.up * Time.deltaTime * 300);
        venus.Rotate (Vector3.up * Time.deltaTime * 280);
        mars.Rotate (Vector3.up * Time.deltaTime * 220);
        jupiter.Rotate (Vector3.up * Time.deltaTime * 180);
        saturn.Rotate (Vector3.up * Time.deltaTime * 160);
        uranus.Rotate (Vector3.up * Time.deltaTime * 150);
        neptune.Rotate (Vector3.up * Time.deltaTime * 140);
    }

对于游戏牧师与魔鬼

  1. 游戏中提及的对象有牧师,魔鬼,船,河岸,河水。
  2. 玩家动作表:
项目 条件
上船 船已经靠岸且船上至少有一名角色
下船 船上至少有一名角色
游戏胜利 所有牧师与魔鬼均到达对岸
游戏失败 任意一边的魔鬼数量大于牧师数量

关于游戏的详细信息请参考我的具体的博客:牧师与魔鬼

猜你喜欢

转载自blog.csdn.net/hellowangld/article/details/79746246
今日推荐