管理不同汽车动画的正确方法

时间:2016-06-03 11:27:48

标签: c# animation unity3d

我有几个动画车对象,它们有自己的动画和动画控制脚本。汽车对象在道路上运行,我想管理  所有汽车动画,即

  1. 如果有任何车辆在前方,那么我会慢下来倒车而不是 碰撞或绕过前方车。
  2. 如果前面的车停了,那么我就会停下来的车,玩耍和速度 相应的。
  3. 我尝试使用这种技术的光线投影,然后我在每辆车上添加了对撞机,然后我从每辆车上进行了光线投射。  它部分工作,有时候没有检测到前方对象的相关性,因为我的代码其他声明看起来并不正确。

    I think that it is not the right way to do this job. what is the correct way to do as Raycast is also expensive
    
        if (Physics.Raycast(transform.position, transformForwad, out rayHit, 10f))
                {
                    if (rayHit.transform.tag == "Car")
                    {
                        Debug.Log("car collide,  current object: "+ transform.name +" : hitted "+ rayHit.transform.name);
                        isCarNearToAnotherCar = true;
                        ANIMATION_OBJECT.GetComponent<Animation>()[ClipName].speed = rayHit.transform.GetComponent<AnimationControlSpeed>().Speed/2;
    
                    }
                    else {
                        Debug.Log("car collide end,  current object: " + transform.name + " : hitted " + rayHit.transform.name);
                        isCarNearToAnotherCar = false;
                    }
    
                }
    
    //Update speed only when isCarNearToAnotherCaris false.
            if (!isCarNearToAnotherCar)
            {//normal the speed, when there is no car ahead
                ANIMATION_OBJECT.GetComponent<Animation>()[ClipName].speed = Speed;
            }
    

1 个答案:

答案 0 :(得分:1)

我看到两个选项:
1)当您控制所有车辆并且您知道他们的订单时(正如您所说,永远不会改变),为什么不根据车辆进行数学计算?&#39;位置和尺寸,而不是光线投射?比较铅笔的尺寸和尺寸。位置到第二辆车并采取相应的行动,然后是第二辆,然后是第三辆,依此类推.-或者,恕我直言,甚至更好 2)为什么不使用对撞机?您可以在汽车的前保险杠上添加扳机碰撞器,并在碰到前车的刚体时触发事件。您可以根据需要放置对撞机,以设置反应范围&#39;你喜欢什么。
你甚至可以设置不同的防撞箱#34;不同车型的尺寸,基于&#34;勇敢&#34;司机是 - 因为距离前方太近了。

相关问题