Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Timm638 · Oct 30, 2014 at 02:43 PM · collsion

2d Collision not working?

Hello, i have the problem that i have Several Objects with 2d Box-Collision and 2d Rigidbodies. But the Player and the test-objects aren't colliding. Also a 2d-ray doesn't collide with the objects.

Movement.js #pragma strict

 var MovSpeed : float;
 function Start () {
 
 }
 
 var damage : int = 5;
 
 function Update () {
     if (Input.GetKey(KeyCode.W)) {
     transform.position.y = transform.position.y + MovSpeed;
     }
     else if (Input.GetKey(KeyCode.S)) {
     transform.position.y = transform.position.y - MovSpeed;
     }
     if (Input.GetKey(KeyCode.D)) {
     transform.position.x = transform.position.x + MovSpeed;
     }
     else if (Input.GetKey(KeyCode.A)) {
     transform.position.x = transform.position.x - MovSpeed;
     }
     if (Input.GetKeyDown(KeyCode.Mouse0)) {
         var ray = new Ray (GameObject.Find("Pistol").transform.position, transform.TransformDirection(Vector3(0,2,0)));
         var hit : RaycastHit;
         Debug.DrawRay(GameObject.Find("Pistol").transform.position,transform.TransformDirection(Vector3(0, 2, 0)), Color.white, 0.1, false);
         if (Physics2D.Raycast(GameObject.Find("Pistol").transform.position, Vector2(2,0), 2) == true) {
         print("Got it");
         hit.collider.transform.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
         };
     };
 }

Enemy_test.js

 #pragma strict
 var hp : int;
 
 function ApplyDamage(damage: int) {
     hp = hp - damage;
     if (hp <= 0) {
         Destroy(this);
     }
 }

If you need more information, I attached the project. I hope that at least one will try to help me.

2d game.zip (134.0 kB)
2d game.zip (134.0 kB)
Comment
Add comment · Show 5
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
avatar image Mayank Ghanshala · Oct 30, 2014 at 03:13 PM 0
Share

change your physics2d.raycast part to

 if ((Physics2D.Raycast(GameObject.Find("Pistol").transform.position, new Vector2(1,1), 2)).collider)

avatar image Timm638 · Nov 01, 2014 at 06:54 AM 0
Share

Even when the Raycast/Enemy Collision is now working, the player/enemy don't collide. But thanks for helping me atleast at this problem

avatar image Itaros · Nov 01, 2014 at 10:25 AM 0
Share

Is player Is$$anonymous$$inematic true?

avatar image Timm638 · Nov 01, 2014 at 12:42 PM 0
Share

No, i haven't enabled $$anonymous$$inematic at Player.

avatar image Timm638 · Nov 01, 2014 at 07:24 PM 0
Share

To Itaros, neither with $$anonymous$$inematic on or off, it doesn't collide with the things. And i reworked the raycast part a bit, but i get a object.reference error?

var collidern : RaycastHit; Debug.DrawRay(GameObject.Find("Pistol").transform.position,transform.TransformDirection(Vector3(0, 2, 0)), Color.white, 0.1, false);

if ((Physics2D.Raycast(GameObject.Find("Pistol").transform.position, new Vector2(1,1), 2)).collider) {

collidern.collider.transform.Send$$anonymous$$essage("ApplyDamage", damage, Send$$anonymous$$essageOptions.DontRequireReceiver); };

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rigidbodies move slightly into each other 0 Answers

Is possible detect collision on gameobject created in an empty gameobject? 1 Answer

Activating child triggers? 0 Answers

collistion detection with moving gameObject 0 Answers

Player and Enemy Collision Problem 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