Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Lucifer95 · Oct 02, 2014 at 12:40 PM · touchcollidemaze

Moving object with touch input and detecting collision

i am making a game where player moves a cube with finger on android through 2d maze.i can move he cube,but how to check for collision with walls and obstacles which are cubes??

 Scripts:
 
 Gamescript:
 
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GameScript : MonoBehaviour
 {
 
     private Dictionary<int, GameObject> trails = new Dictionary<int, GameObject>();
     private Touch pinchFinger1, pinchFinger2;
     private ParticleSystem vortex;
     
     void Update()
     {
             
     
                 // Look for all fingers
     for (int i = 0; i < Input.touchCount; i++) {
         Touch touch = Input.GetTouch (i);
             // -- Drag
         // ------------------------------------------------
         if (touch.phase == TouchPhase.Began) {
             // Store this new value
         if (trails.ContainsKey (i) == false) {
         Vector3 position = Camera.main.ScreenToWorldPoint (touch.position);
         position.z = 0; // Make sure the trail is visible
                             
         GameObject trail = SpecialEffectsScript.MakeTrail (position);
                             
         if (trail != null) {
         Debug.Log (trail);
         trails.Add (i, trail);
                 }
             }
         } 
             else if (touch.phase == TouchPhase.Moved) {
             // Move the trail
         if (trails.ContainsKey (i)) {
         GameObject trail = trails [i];
                             
         Camera.main.ScreenToWorldPoint (touch.position);
         Vector3 position = Camera.main.ScreenToWorldPoint (touch.position);
         position.z = 0; // Make sure the trail is visible
                             
         trail.transform.position = position;
             
         } else if (touch.phase == TouchPhase.Ended) {
         // Clear known trails
         if (trails.ContainsKey (i)) {
         GameObject trail = trails [i];
             // Let the trail fade out
         Destroy (trail, trail.GetComponent<TrailRenderer> ().time);
         trails.Remove (i);
                 }
             }
         }
             
         }
         }
         }
 
        

SpecialEffectsScript

using UnityEngine;

/// /// Helper class to handle particles effects /// public class SpecialEffectsScript : MonoBehaviour { private static SpecialEffectsScript instance;

 // Prefabs
 public ParticleSystem explosionEffect, vortexEffect;
 public GameObject trailPrefab;
 
 void Awake()
 {
     instance = this;
 }
 
 void Start()
 {

     if (trailPrefab == null)
         Debug.LogError("Missing Trail Prefab!");
 }

public static GameObject MakeTrail(Vector3 position) { if (instance == null) { Debug.LogError("There is no SpecialEffectsScript in the scene!"); return null; }

     GameObject trail = Instantiate(instance.trailPrefab) as GameObject;
     trail.transform.position = position;
     
     return trail;
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Josh Naylor · Oct 02, 2014 at 12:41 PM

http://docs.unity3d.com/ScriptReference/Collider2D.OnCollisionEnter2D.html

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
0

Answer by Baste · Oct 02, 2014 at 02:19 PM

You REALLY need to format your code when you're posting it! Kinda hard to see what's going on.

The general way to move a thing about and have it detect collisions is to give it a rigidbody, and using rigidBody.MovePosition instead of setting the transform's position. If you use MovePosition, your thing will be stopped by walls, whereas if you set the transform's position, it will just move straight through them.

Comment
Add comment · 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

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

Convert WASD (or arrows) to touch input for ball maze game 0 Answers

How to check if specific objects touch 0 Answers

How can I make my character collide with physics objects? 2 Answers

check user touching a particular position or not 2 Answers

Touch and slide GUI box & grid selected buttons cSharp 1 Answer


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