Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by todorv · Apr 07, 2017 at 02:52 PM · androidcollidertouch

How to make my gabeobject collide?

So im using this code to move around an object, but i dont know how to make it collide with the walls. No matter what I do the gameobject just goes through the walls or if it hits them they bounce. Sorry if its too simple I am new.

 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;
             }
         }
     }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by shadowpuppet · Apr 09, 2017 at 12:46 AM

does the object have a rigidbody and a collider? What bounces the wall or the object?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image todorv · Apr 09, 2017 at 07:48 AM 0
Share

By bounce I meant that when they collide the "walls" just start "flying" around. And yes I attached a box collider and a rigidbody to them I disabled the "use gravity" option too. But nothing seems to be able to stop that gameobject that I move with this script.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Touch deltaPosition Android 1 Answer

How do I rotate an object on one axis to face android touch? 0 Answers

screenCanDarken for Android (Backlight going dark) 1 Answer

How to drag object along with dragging touch? 1 Answer

I can't change the sprite using the Touch class 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges