Hello,
I have got a problem with 2D collididers. I don’t know why but Unity doesn’t detect colliders. I am working on 2d game and i am trying to do the attack by player by throwing a spear and I want it to deal damage to enemy what it is in collision with it. Here is the code:
{
public float playerSpeed = 4f;
public float playerPosX = 0;
public float playerPosY = 0;
public float swordRotateSpeed = 0.5f;
private bool attack = false;
public GameObject player;
public GameObject kopija;
void Start()
{
}
void FixedUpdate()
{
Vector2 targetVelocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
GetComponent<Rigidbody2D>().velocity = targetVelocity * playerSpeed;
}
// Update is called once per frame
void Update()
{
StartCoroutine(PlayerSavePos());
if(Input.GetKey(KeyCode.Mouse0))
{
attack = true;
}
if (attack==true)
{
//sword.transform.Rotate(Vector2.right, swordRotateSpeed * Time.deltaTime);
kopija.transform.Translate(Vector2.up * Time.deltaTime);
}
}
IEnumerator PlayerSavePos()
{
yield return new WaitForSeconds(5.0f);
playerPosX = player.transform.position.x;
PlayerPrefs.SetFloat("PlayerX", playerPosX);
PlayerPrefs.Save();
playerPosY = player.transform.position.y;
PlayerPrefs.SetFloat("PlayerY", playerPosY);
PlayerPrefs.Save();
}
void Awake()
{
if (PlayerPrefs.HasKey("PlayerX"))
{
playerPosX = PlayerPrefs.GetFloat("PlayerX");
}
if (PlayerPrefs.HasKey("PlayerY"))
{
playerPosY = PlayerPrefs.GetFloat("PlayerY");
}
}
I tried different types of colliders and i tried to play a little bit with a Rigidbody 2d settings and nothing is working. I am using Unity 2018.2.10f1. Please if anyone could help, then help me.