- Home /
2D coliders not coliding
im trying to make a mobile 2d shooter and my coliders are not working. heres my script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Joystick : MonoBehaviour { public Transform player; public float speed = 5.0f; private bool touchStart = false; private Vector2 pointA; private Vector2 pointB;
public Transform circle;
public Transform outerCircle;
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonDown(0)){
pointA = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
circle.transform.position = pointA * -1;
outerCircle.transform.position = pointA * -1;
circle.GetComponent<SpriteRenderer>().enabled = true;
outerCircle.GetComponent<SpriteRenderer>().enabled = true;
}
if(Input.GetMouseButton(0)){
touchStart = true;
pointB = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
}else{
touchStart = false;
}
}
private void FixedUpdate(){
if(touchStart){
Vector2 offset = pointB - pointA;
Vector2 direction = Vector2.ClampMagnitude(offset, 1.0f);
moveCharacter(direction * -1);
circle.transform.position = new Vector2(pointA.x + direction.x, pointA.y + direction.y) * -1;
}else{
circle.GetComponent<SpriteRenderer>().enabled = false;
outerCircle.GetComponent<SpriteRenderer>().enabled = false;
}
}
void moveCharacter(Vector2 direction){
player.Translate(direction * speed * Time.deltaTime);
}
} everything is on the same layer. Thanks in advance!
Comment
Answer by SteveHoward · Nov 20, 2020 at 04:14 PM
@htkdavid, I don't see see any collision code here. Do you have OnCollisionEnter2D() or OnTriggerEnter2D or other Collider2D methods?
it was just a wall that i was not colliding with, but i fixed it i just needed a rigidbody
Your answer
![](https://koobas.hobune.stream/wayback/20220613020154im_/https://answers.unity.com/themes/thub/images/avi.jpg)