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 PJonathan · Apr 21, 2016 at 04:51 AM · rigidbody2dcollision detectionaddforcetriggers

Player is only detected in trigger when moving

In my game I have a switch. The switch has a trigger on it to detect when the player is touching is so that he/she can flick it my pressing 'E'.

The weird part is I can only trigger the switch when the player is moving, I am using addforce for movement.

Below is the code for my player and my switch. I have also attached my project folder so that if someone is willing to take the time and try it they can. The reproduction rate is 100% of the time for me.

Obviously this is not the intended result, the player should be able to activate the switch even if they are standing still.

PLAYERMOVEMENT.CS:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
     private Rigidbody2D R2Dplayer;
     private bool grounded = true;
     [SerializeField] private Animator anim;
 
     // Use this for initialization
     void Start () {
         R2Dplayer = GetComponent<Rigidbody2D>();
     }
 
     // Update is called once per frame
     void Update () {
         anim.SetFloat("speed", Mathf.Abs(R2Dplayer.velocity.x));
         if(Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Space)) {
             if(grounded == true) {
                 R2Dplayer.AddForce (new Vector2(0,7.5f), ForceMode2D.Impulse);
             }
         }
         if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) {
             R2Dplayer.AddForce (new Vector2(-0.2f,0), ForceMode2D.Impulse);
             if (R2Dplayer.transform.rotation == new Quaternion (0.0f, 0.0f, 0.0f, 1.0f)) {
                 R2Dplayer.transform.Rotate (0, 180, 0);
             }
         }
         if(Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) {
 
         }
         if(Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) {
             R2Dplayer.AddForce (new Vector2(0.2f,0), ForceMode2D.Impulse);
             if (R2Dplayer.transform.rotation == new Quaternion (0.0f, -1.0f, 0.0f, 0.0f)) {
                 R2Dplayer.transform.Rotate (0, 180, 0);
             }
         }
     }
 
     void OnTriggerEnter2D(Collider2D c) {
         if(c.tag == "JumpTrigger") {
             grounded = true;
         }
     }
 
     void OnTriggerExit2D(Collider2D c) {
         if(c.tag == "JumpTrigger") {
             grounded = false;
         }
     }
 }
 

SWITCH.CS:

 using UnityEngine;
 using System.Collections;
 
 public class Switch : MonoBehaviour {
 
     [SerializeField] public bool switchPressed = false;
     private SpriteRenderer sr;
     [SerializeField] private Sprite on, off;
 
     // Use this for initialization
     void Start () {
         sr = GetComponent<SpriteRenderer> ();
     }
 
     // Update is called once per frame
     void Update () {
     
     }
 
     void OnTriggerStay2D(Collider2D c) {
         if(c.tag == "Player") {
             if(Input.GetKeyDown(KeyCode.E)) {
                 if(switchPressed == false) {
                     sr.sprite = on;
                     switchPressed = true;
                 }
                 else if(switchPressed == true) {
                     sr.sprite = off;
                     switchPressed = false;
                 }
 
             }
         }
     }
 }


ZIP of my project that can be downloaded from dropbox: https://www.dropbox.com/s/85anfa4287mciiy/Final.zip?dl=0

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Soraphis · Apr 21, 2016 at 09:36 AM

Set the sleeping mode of your switch from "start awake" to "never sleep"


BUT! you should not do it like this.

you should consider a cleaner solution where you've one place where your input is handled.

e.g. sending a raycast when E is pressed.

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 PJonathan · Apr 21, 2016 at 01:34 PM 0
Share

Thanks, you're right raycast is probably better, BUT the scope of this assignment is not worth it. Thanks again!

avatar image
0

Answer by Ali-hatem · Apr 21, 2016 at 02:00 PM

i have downloaded your project & corrected somethings :

 void Update () {
     if (Input.GetKeyDown (KeyCode.E) && switchPressed) {
         if (sr.sprite == off) {
             sr.sprite = on;
         } else {
             sr.sprite = off;
         }
     }
 }
 void OnTriggerEnter2D(Collider2D c) {
     if(c.tag == "Player") {
         switchPressed = true;
     }
 }
 void OnTriggerExit2D(Collider2D cc){
     if (cc.tag == "Player") {
         switchPressed = false;
     }
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

OnCollisionExit() firing when object has not exited 0 Answers

How Do I Make My Bullet Add Force To The Object It Hits? 1 Answer

Checking if colliding with anything in a layer 1 Answer

Did triggers inside loaded gameobjects break in a recent update? 0 Answers

rigidbody2D Addforce Problem 1 Answer


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