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 /
This question was closed Jul 14, 2018 at 10:30 PM by Broccoliguy04 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Broccoliguy04 · Jun 29, 2018 at 09:16 PM · ontriggerenter2d

OnTriggerEnter2D is not being called

I am trying to get the camera to move when I collide with another Sprite, but when I collide, OnTriggerEnter2D is not being called, someone help me. Both of the sprites are set as Triggers. Here is the script associated with the problem.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour {
     public float speed;
     private Camera playerCamera;
     public GameObject door;
     private GameObject player;
 
     void Start () {
         player = this.gameObject;
         playerCamera = Camera.main;
     }
     void Update () {
         //moving the player
         if (Input.GetKey(KeyCode.S)) {
             player.transform.position += Vector3.down * Time.deltaTime * speed;
         }if (Input.GetKey(KeyCode.W)){
             player.transform.position += Vector3.up * Time.deltaTime * speed;
         }if (Input.GetKey(KeyCode.A)) {
             player.transform.position += Vector3.left * Time.deltaTime * speed;
         }if (Input.GetKey(KeyCode.D)) {
             player.transform.position += Vector3.right * Time.deltaTime * speed;
         }
     }
     public void OnTriggerEnter2D(Collider2D collision)
     {
         print("Testing to see if hit door");
         if (collision.gameObject.CompareTag("right"))
         {
             for (Vector3 camPrevPos = playerCamera.transform.position; playerCamera.transform.position.x < camPrevPos.x + 22.7; playerCamera.transform.position += (Vector3.right * Time.deltaTime))
             {
                 playerCamera.transform.position += Vector3.right * Time.deltaTime;
             }
             print("has hit a door");
         }
     }
 
     //senses if player has hit a door
     public void MoveCamera(Collision2D collision){
         print("Testing to see if hit door");
         if (collision.gameObject.CompareTag("right")) {
             for (Vector3 camPrevPos = playerCamera.transform.position;playerCamera.transform.position.x < camPrevPos.x + 22.7;playerCamera.transform.position+=(Vector3.right * Time.deltaTime))
             { playerCamera.transform.position += Vector3.right * Time.deltaTime;
             }
             print("has hit a door");
         }
     }
 
 }
 

Let me know of you need more info!

Comment
Add comment · Show 1
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 Hellium · Jun 30, 2018 at 07:50 AM 0
Share

Are you sure all the condition for collision detection are met?

  • Colliders on the both object

  • One rigidbody on one of the two objects (or both)

  • Script attached to the object with the collider set to trigger

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Adarsh2303 · Jun 30, 2018 at 09:02 AM

@Broccoliguy04 One of the Sprites needs Rigidbody 2d...

Comment
Add comment · Show 1 · 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
avatar image Broccoliguy04 · Jul 14, 2018 at 10:30 PM 0
Share

Sorry, forgot about that

Follow this Question

Answers Answers and Comments

87 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

Related Questions

Unexpected token: collider? 1 Answer

OnTriggerEnter2D is being called multiple times.... 1 Answer

OnTriggerEnter2d gets called twice sometimes. 3 Answers

score counter is not accurate 1 Answer

removing duplicates in list 2 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