1. 程式人生 > 其它 >unity筆記(指令碼5)

unity筆記(指令碼5)

public class Bullet : MonoBehaviour
{
    //滿足碰撞條件  碰撞第一幀執行
    private void OnCollisionEnter(Collision other)
    {
        //other.collider  對方的碰撞器元件引用
        Debug.Log("OnCollisionEnter:"+other.collider);
        //other.contacts[0].point 碰撞點座標
    }

    //滿足觸發條件  接觸第一幀執行
    private void OnTriggerEnter(Collider other)
    { 
//other 就是對方碰撞器元件 Debug.Log("OnTriggerEnter:" + other); Destroy(other.gameObject); Destroy(gameObject); } //如果移動速度過快,將導致碰撞檢測失敗。 public float moveSpeed = 100; private void Update1() { transform.Translate(0, 0, moveSpeed * Time.deltaTime); Debug.Log(transform.position); }
private RaycastHit hit; public LayerMask layer; private Vector3 targetPos; //解決方案: private void Start() { if (Physics.Raycast(transform.position, transform.forward, out hit, 500, layer)) { //檢測到目標 targetPos = hit.point; } else {
//沒有目標 //targetPos = transform.TransformPoint(0, 0, 500); targetPos = transform.position + transform.forward * 500; } } private void Update() { transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime); if ((transform.position - targetPos).sqrMagnitude < 0.1f) { Destroy(hit.collider.gameObject); Destroy(gameObject); } } }
public class CharcterMoto : MonoBehaviour
{
    private void FixedUpdate()
    {
        //如果當前位置  座標 在其他碰撞器內  則執行OnTriggerEnter方法
    }

    private void Update()
    {
        float hor = Input.GetAxis("Horizontal");
        float ver = Input.GetAxis("Vertical");
        if (hor != 0 || ver != 0)
        {
            MovementRotation(hor, ver);
        }
    }

    private void MovementRotation(float hor, float ver)
    {
        var dir = Quaternion.LookRotation(new Vector3(hor, 0, ver));
        transform.rotation = Quaternion.Lerp(transform.rotation, dir, rotateSpeed * Time.deltaTime);
        //transform.rotation = dir;
         
        //transform.Translate(0, 0, moveSpeed * Time.deltaTime);
        //transform.Translate(hor, 0, ver, Space.World);
        transform.Translate(transform.forward * Time.deltaTime * moveSpeed , Space.World);
    } 

    public float rotateSpeed = 10;

    public float moveSpeed = 10;
}
public class QuaternionAPI : MonoBehaviour
{
    private void Start()
    {  
        Quaternion qt = transform.rotation;
        //1. 四元數  -->  尤拉角
        Vector3 euler = qt.eulerAngles;
        //2.尤拉角 --> 四元數
        Quaternion qt02 = Quaternion.Euler(0, 90, 0);
        //3.軸 / 角 旋轉
        //transform.rotation = Quaternion.AngleAxis(30, Vector3.up);
        //transform.localRotation = Quaternion.AngleAxis(30, Vector3.up);  
    }

    public Transform target;
    private void Update()
    {
        //4. 注視旋轉
        //Quaternion dir = Quaternion.LookRotation(target.position - transform.position);
        //transform.rotation = dir;
        //transform.LookAt(target.position);

        //5.Lerp 差值旋轉  由快到慢
        //transform.rotation = Quaternion.Lerp(transform.rotation, dir, 0.1f);

        //6.RotateTowards    勻速旋轉
        //transform.rotation = Quaternion.RotateTowards(transform.rotation, dir, 0.1f);
         
        //Quaternion dir = Quaternion.Euler(0, 180, 0);
        //transform.rotation = Quaternion.Lerp(transform.rotation, dir, 0.005f);
        ////7. 四元數計算角度差
        //if (Quaternion.Angle(transform.rotation, dir) < 30)
        //    transform.rotation = dir;

        //8. 從?到?的旋轉
        transform.rotation = Quaternion.FromToRotation(Vector3.right, target.position - transform.position);
      
    }
 
}
public class VectorAPI : MonoBehaviour
{
    private void Start1()
    {
        //因為 position 是屬性,所以返回資料副本,直接修改z無效。所以編譯錯誤。
        //transform.position.z = 1;

        //方案1:
        //複製(資料)
        Vector3 pos = transform.position;
        pos.z = 1;//如果僅僅修改位置的副本,物體位置不會發生改變
        transform.position = pos;

        //方案2: 
        transform.position = new Vector3(transform.position.x, transform.position.y, 1);


        //Vector3.Distance(位置1,位置2)        (位置1 - 位置2).模長 
        //建議  (位置1 - 位置2).模長平方  sqrMagnitude
    }

    public Transform t1;
    private Vector3 tangent;
    private Vector3 binNormal;
    private void Update()
    { 
        Vector3 norm = t1.position;
        ////計算垂直向量
        //Vector3.OrthoNormalize(ref norm, ref tangent, ref binNormal);

        //Debug.DrawLine(Vector3.zero, norm);
        //Debug.DrawLine(Vector3.zero, tangent,Color.red);
        //Debug.DrawLine(Vector3.zero, binNormal, Color.yellow);  

        //計算t1物體在地面上的投影
        Vector3 project = Vector3.ProjectOnPlane(norm, Vector3.up);
        Debug.DrawLine(Vector3.zero, norm);
        Debug.DrawLine(Vector3.zero, project,Color.red);

        //計算反射向量:Vector3.Reflect 
    }

    public Vector3 currentSpeed;
    //移動類API
    private void OnGUI()
    {
        if (GUILayout.RepeatButton("Lerp"))
        {
            //由快到慢      無限接近目標點
            //起點改變    終點、比例不變
            transform.position =
                Vector3.Lerp(transform.position, new Vector3(0, 0, 10), 0.1f);
        }

        if (GUILayout.RepeatButton("MoveTowards"))
        {
            //勻速     無限接近目標點 
            transform.position =
                Vector3.MoveTowards(transform.position, new Vector3(0, 0, 10), 0.1f);
        }

        if (GUILayout.RepeatButton("SmoothDamp"))
        {
            //平滑阻尼
            transform.position =
                Vector3.SmoothDamp(transform.position, new Vector3(0, 0, 10), ref currentSpeed, 2);
        }

        if (GUILayout.RepeatButton("變速運動"))
        {
            x += Time.deltaTime / time; 
            //由快到慢      無限接近目標點
            //起點 、終點 不變     比例改變
            Vector3 begin = Vector3.zero;
            //transform.position =
            //    Vector3.Lerp(begin, new Vector3(0, 0, 10), curve.Evaluate(x));
            transform.position =
                Vector3.LerpUnclamped(begin, new Vector3(0, 0, 10), curve.Evaluate(x));
        } 
    }

    public AnimationCurve curve;
    private float x;
    public float time =1;

    //練習:物體閃爍
    private void Start()
    {
        Material mt;
        //mt.SetFloat("_Shininess",    ?  );
    }
}