物体在一定范围自有碰撞

/*              #########                       
              ############                     
              #############                    
             ##  ###########                   
            ###  ###### #####                  
            ### #######   ####                 
           ###  ########## ####                
          ####  ########### ####               
         ####   ###########  #####             
        #####   ### ########   #####           
       #####   ###   ########   ######         
      ######   ###  ###########   ######       
     ######   #### ##############  ######      
    #######  #####################  ######     
    #######  ######################  ######    
   #######  ###### #################  ######   
   #######  ###### ###### #########   ######   
   #######    ##  ######   ######     ######   
   #######        ######    #####     #####    
    ######        #####     #####     ####     
     #####        ####      #####     ###      
      #####       ###        ###      #        
        ###       ###        ###              
         ##       ###        ###               
__________#_______####_______####______________
    身是菩提树,心如明镜台,时时勤拂拭,勿使惹尘埃。
                我们的未来没有BUG              
* ==============================================================================
* Filename: AnimationFlashScript
* Created:  $time$
* Author:   WYC
* Purpose:  自有匀速碰撞
* ==============================================================================
*/
using UnityEngine;

public class RenCollier : MonoBehaviour {
    public float         speed;
    public    Rigidbody    rigidbody;
    RaycastHit hit;

    float stay;

    void Start () {
        transform.eulerAngles = new Vector3(0,0, Random.value*360);
    }
    
    void FixedUpdate () {
        transform.Translate (Vector3.right * speed * Time.deltaTime);
    }

    void OnCollisionEnter(){
        Vector3 fwd = transform.TransformDirection(Vector3.right);
        Ray ray = new Ray(transform.position,transform.right);

        if (Physics.Raycast(ray,out hit , 20f)){
            Vector3 rayDir = Vector3.Reflect(ray.direction,hit.normal);
            float rot = Mathf.Atan2(rayDir.y,rayDir.x) * Mathf.Rad2Deg;
            transform.localEulerAngles = new Vector3(0,0,rot);
        }
    }

    void OnCollisionStay(){
        stay++;

        if (stay > 10) {
            stay = 0;
            transform.eulerAngles = new Vector3(0,0, Random.value*360);
        }
    }
}
原文地址:https://www.cnblogs.com/mclll520/p/8028154.html