- Home /
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;
}
}
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.
Your answer
Follow this Question
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