如何让我的游戏对象与墙壁碰撞?

时间:2017-04-06 10:43:15

标签: android touch

所以即时使用此代码移动对象,但我不知道如何使它与墙壁碰撞。无论我做什么游戏对象只是通过墙壁或如果它击中它们他们反弹。对不起,如果它太简单我就是新人。

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

     public class controller : MonoBehaviour {
         private float dist;
         private bool dragging = false;
         private Vector3 offset;
         private Transform toDrag;
         public Text score;
         public int counterofbeers;
         public Touch touch;
         public bool lampa=false;
         void Awake(){
             counterofbeers = 0;
         }
         void Update() {
             score.text= counterofbeers.ToString ();
             Vector3 v3;

             if (Input.touchCount != 1) {
                 dragging = false; 
                 return;
             }

             touch = Input.touches[0];
             Vector3 pos = touch.position;

             if(touch.phase == TouchPhase.Began) {
                 RaycastHit hit;
                 Ray ray = Camera.main.ScreenPointToRay(pos); 
                 if(Physics.Raycast(ray, out hit) && (hit.collider.tag == "Draggable"))
                 {
                     Debug.Log ("Here");
                     toDrag = hit.transform;
                     dist = hit.transform.position.z - Camera.main.transform.position.z;
                     v3 = new Vector3(pos.x,dist);
                     v3 = Camera.main.ScreenToWorldPoint(v3);
                     offset = toDrag.position - v3;
                     dragging = true;
                 }
             }
             if (dragging && touch.phase == TouchPhase.Moved) {
                 v3 = new Vector3(Input.mousePosition.x,dist);
                 v3 = Camera.main.ScreenToWorldPoint(v3);
                 toDrag.position = v3 + offset;
             }
             if (dragging && (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)) {
                 dragging = false;
             }

         }
         void OnTriggerEnter(Collider other)    {
             if (other.gameObject.CompareTag ("pickup")) {
                 Destroy (other.gameObject);    counterofbeers++;
             }
             if(other.gameObject.CompareTag("wall")){
                 lampa=true;
             }
         }
     }

0 个答案:

没有答案
相关问题