Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by DemonGamesLab · Jan 01, 2016 at 07:20 AM · c#transformpositionvector3door

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.

Comment
Add comment · Show 2
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 tanoshimi · Jan 01, 2016 at 07:59 AM 0
Share

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.

avatar image marfa4321 · Jan 01, 2016 at 08:14 AM 1
Share

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).

2 Replies

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

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.

Comment
Add comment · Show 4 · 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 DemonGamesLab · Jan 02, 2016 at 08:33 PM 0
Share

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 :)

avatar image DemonGamesLab · Jan 02, 2016 at 08:35 PM 0
Share

just to add i tried the ontriggerstay and it works :)

avatar image DemonGamesLab · Jan 02, 2016 at 08:39 PM 0
Share

problem is i want to be able to get back in but it dont work because the script is on the player ? any notes:) ?

avatar image DemonGamesLab · Jan 02, 2016 at 09:11 PM 0
Share

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);

     }
     
 }
     

}

avatar image
0

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

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

Trying to Generate Different Random Values for Position of Game Object Instances [C#] 1 Answer

How would someone go about creating multiple triggers in one script 0 Answers

Get Vector3s in range 1 Answer

Make object move back and forth 2 Answers

Can’t keep the position 0 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