Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Oliv3r885 · Oct 10, 2018 at 07:55 PM · collisioncollider2d gamenot workingdetection

2D collider problems

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.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RatMoleRat · Oct 11, 2018 at 03:56 PM

A list of basic things to check:

If you want collision to register through script:

  • You need an OnCollisionEnter2D (if a collider isn't set to trigger) or OnTriggerEnter2D (if the collider is set to trigger) function in a script attached to the object that has the collider

In any case:

  • You need some kind of 2D collider on both objects, not set to trigger on either of them

  • You need a dynamic rigidbody2D on (at least) one object

Another thing that I noticed that is quite possibly an issue is that you are translating kopija (is that the weapon?) rather than moving it with a rigidbody. Depending on the scale, it might be teleporting through the enemy collider, rather than actually hitting it.

It might work better for you to use a Raycast in the direction of the spear, to check if the enemy would get hit. For example (note, I haven't tested this):

 private static void CheckIfSpearHit (Vector2 direction) { //pass in direction of the attack
      int range = 5; //or however far the weapon can reach
      RaycastHit2D hit = Physics2D.Raycast(transform.position, direction, range);
 
      //check if the raycast hit something
      if (hit.collider.gameObject.name == "monster") { //or whatever its name would be
           //do whatever you want here. maybe access script attached to enemy to decrease health
      }
 }
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

172 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Particle Collision / Trigger not being reported to On_xx Event 1 Answer

Problem with collision with two colliders simultaneously in Unity 2D 1 Answer

Collision Enter one of the objects ? 1 Answer

Weapon System with collide detection (Helps with script pls)!!! 0 Answers

2D:I have a movementController script on my character but it doesn't move forward 0 Answers


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