- Home /
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
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.
Thanks, you're right raycast is probably better, BUT the scope of this assignment is not worth it. Thanks again!
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;
}
}