- Home /
Why is my character not colliding with other objects.
This is the code i am using to run a 2d game like "The Escapists", but my character has a collider on him and does not seem to be colliding with other objects. This is in a 3d environment. Thanks. using System.Collections; using System.Collections.Generic; using UnityEngine; public class TemporaryMove : MonoBehaviour { public float movementSpeed = 10; public GameObject Right; public GameObject Left; public GameObject Up; public GameObject Down; // Use this for initialization void Start () { Down.SetActive (true); Right.SetActive (false); Left.SetActive (false); Up.SetActive (false); } // Update is called once per frame void Update () { if (Input.GetKey(KeyCode.D)) { transform.Translate (Vector3.right * movementSpeed * Time.deltaTime); Down.SetActive (false); Right.SetActive (true); Left.SetActive (false); Up.SetActive (false); } if (Input.GetKey(KeyCode.A)) { transform.Translate (Vector3.left * movementSpeed * Time.deltaTime); Down.SetActive (false); Right.SetActive (false); Left.SetActive (true); Up.SetActive (false); } if (Input.GetKey(KeyCode.W)) { transform.Translate (Vector3.up * movementSpeed * Time.deltaTime); Down.SetActive (false); Right.SetActive (false); Left.SetActive (false); Up.SetActive (true); } if (Input.GetKey(KeyCode.S)) { transform.Translate (Vector3.down * movementSpeed * Time.deltaTime); Down.SetActive (true); Right.SetActive (false); Left.SetActive (false); Up.SetActive (false); } } void OnCollisionEnter(Collision collision) { Debug.Log ("Hit"); if(collision.gameObject.CompareTag("Wall")) // or if(gameObject.CompareTag("YourWallTag")) { Debug.Log ("Hit"); } } }
Answer by surajsirohi1008 · Oct 21, 2017 at 03:30 PM
Make sure "isTrigger"is unchecked of the collider.
Answer by Petroglyph · Oct 23, 2017 at 11:04 PM
Not sure about your code, but as bolkay mentionned, you need to add a Rigidbody to your moving object. Colliders on their own are meant to be static and do not trigger each other. Make sure you check the isKinematic option on your Rigidbody, to allow you to move it by code as you seem to be doing.
Your answer
Follow this Question
Related Questions
Problem with collision with two colliders simultaneously in Unity 2D 1 Answer
Unity 2D Collider Safe Sizes 0 Answers
2D collision not registering 1 Answer
2D Detect collisions of a 2D block only on left/right (not top/bottom) 0 Answers
How can I make sure triggers or collisions are never skipped even for high speeds? 2 Answers