How to turn off isTrigger when on Entering and turn on isTrigger on Exiting.
Here is my code:
The object has trigger collider. I want it to turn off its collider and to make a little shift when player hit it. But result is not what I want with the following code.
public Vector3 pos;
private bool pause = false;
private Collider col;
void Start()
{
col = GetComponent<Collider>();
pos = transform.position;
Debug.Log("pos is " + pos);
}
void OnTriggerEnter(Collider other)
{
col.isTrigger = false;
if(other.gameObject.tag == "Player")
{
this.transform.position = new Vector3 (pos.x, pos.y + 0.2f, pos.z);
StartCoroutine(PauseTime());
this.transform.position = new Vector3(pos.x, pos.y, pos.z);
StartCoroutine(PauseTime());
col.isTrigger = true;
}
}
IEnumerator PauseTime()
{
yield return new WaitForSeconds(0.02f);
}
isTrigger won't turn off when player enters the object and object won't move as it supposed to do. Please help.
Thank you!
I'm confused by a lot of it. "this" refers to the SCRIPT on the gameobject not the gameobject itself so you are trying to send a script to a new Vector3
Thank you for your response. Then what should I do to change the position of the game object? Thank you!
It doesn't refer to the script. It refers to it's instance, which is a monobehaviour. He could remove it.
You usually need this to differentiate variables with the same name in different scopes (fields, constructor)
I don't understand the expected behavior. Can you explain better? I think somethings wrong in that, maybe you have to implement that in different way.
Answer by hexagonius · Jun 21, 2017 at 05:17 AM
You need to change void to IEnumerator and call those waits like yield return StartCoroutine(PauseTime)
Your answer
Follow this Question
Related Questions
Object moves when game starts, but shouldn't until player collides with trigger 0 Answers
Resetting my cannon fire to its original transform 0 Answers
How can i make my object go back to its start position? 3 Answers
How do I trigger a game object to move a desired distance at a desired speed? 1 Answer
Animation transition problem 1 Answer