Need help with this code? thanks.
void OnTriggerEnter(Collider player) {
Debug.Log ("Press F to leave house");
if (Input.GetKeyDown("f")) {
transform.position = new Vector3(0,0,(180));
}
}
}
so im new too coding, but im trying make it where if i click f my player then ends up on the other side of the door, i get no errors but it just simply wont work? if anyone knows i would appreciate the help thanks and happy new year.
Do you see the "press f to leave house" message? If not, please describe the combination of colliders/rigidbodies you have on the objects involved.
If you have seen the "press f to leave house" message, you must change the Input.Get$$anonymous$$eyDown("f") to Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F).
Answer by Eno-Khaon · Jan 01, 2016 at 11:24 AM
In this current state, it is nearly impossible to press the key with adequate timing. Both the OnTriggerEnter() and Input.GetKeyDown() occur on a single frame of gameplay, and you would be hard pressed to be lucky enough to press the key at that exact moment.
A possible solution to this would be to use OnTriggerStay() instead. This triggers on every frame that the collider is within the Trigger's boundaries.
Edit: Additionally, there might be some unusual logic to factor in as well. Based on the function you described, I'm guessing that script is attached to an area near the door and not to the player. Based on that assumption, rather than using transform.position
to enter the area, you would want to use player.transform.position
to move the object which moved into range of the trigger.
Hi the script is on the player and the script works but you have to keep tapping f and sprinting towards the door, its fustrating and i can not put player.transform.postion because it says it cannot find player?? thanks :)
just to add i tried the ontriggerstay and it works :)
problem is i want to be able to get back in but it dont work because the script is on the player ? any notes:) ?
Update i changed the code
using UnityEngine; using System.Collections;
public class DoorOpen : $$anonymous$$onoBehaviour {
void Start (){
}
void OnTriggerStay (Collider Player) {
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.F)) {
Debug.Log ("Press F to open the door!");
transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z+1);
}
}
}
Answer by marfa4321 · Jan 03, 2016 at 09:00 AM
Hi, @demongames666
Maybe you should write Input.GetKeyDown(Keycode.F) not Input.GetKeyDown("f")
So this is your final code : if (Input.GetKeyDown(KeyCode.F)) { transform.position = new Vector3(0,0,180f); }
Sorry for my bad english, if not working, you can ask or tell me again
- Amar Fadil
Your answer
Follow this Question
Related Questions
Trying to Generate Different Random Values for Position of Game Object Instances [C#] 1 Answer
How to find the transform position of another gameobject then move a gameobject to that position? 1 Answer
gameobject position lerp 0 Answers
I need help with the location of instatiated objects on the scene and correct interaction with this 0 Answers