Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 pjlx911 · Mar 30, 2014 at 11:15 AM · ontriggerentertriggersplatformsinput.getkey

Platform OnTriggerEnter movement with User Input

Hello,

I have been looking for help on trying to get this idea I'm working on for a student project working. The premise is to have a platform that can be moved via user input.

I want to have it so that when the player is on a platform, has a controller, he can press the 'C' key and the platform will move up to a certain spot.

After hours of research, I came upon a neat idea from Here (it's the 2nd answer). The issue is that it's in Java/Unity script. I am a C# programmer (and the values for the controller counter I need to reference is in C#).

So, I THINK I modified the code correctly so that I could use it in C#. The reason I said "think" is due to the fact that once a player is on the platform, I press the 'C' key, but nothing.

Here is the code:

 using System.Collections;
 
 public class PlatformTrigger : MonoBehaviour {
     
     public Transform targetA;   //Start pos
     public Transform targetB;    //End pos
     public float speed = 0.1f;    //speed of movement from A to B
     
     private Transform currentTarget;  //Current platform position
     
     private bool bIsMoving; //is the platform moving?
     
     // Use this for initialization
     void Start () {
         
         //currrent target is the start pos
         currentTarget = targetA; 
         
         //Mover/setter of the platform (unless already there
         MovePlatform(transform.parent, currentTarget, 10.0f);
         
         //platform is not moving
         bIsMoving = false;
     }
     
     IEnumerator MovePlatform (Transform platform, Transform destination, float velocity){
         
         //platform is now moving
         bIsMoving = true;
         
         //get the start & end pos for movement
         Vector3 fromStart = platform.position;
         Vector3 toEnd = destination.position;
         //initalizes a timer
         float timer = 0.0f;
         
         //a cycle to end movement once the timer reaches 1
         while (timer < 1.0f){
             //timer count up
             timer += Time.deltaTime * velocity;
             //if statement to smooth movement from 1 to 0
             if (timer > 1.0f){
                 timer = 1.0f;        //clamps the timer at 1 to end movement
                 //cosine routime to smooth the movement near the end
                 float weight = 0.5f * (1.0f - Mathf.Cos(timer * Mathf.PI)); 
                 // set the platform position according to weight (0=from, 1=to)
                 platform.transform.position = Vector3.Lerp (fromStart, toEnd, weight);
                 yield return new WaitForFixedUpdate(); // let Unity free till the next fixed update
             }
         } 
         
         //movement settled, reset!
         bIsMoving = false;
     }
     
     void OnTriggerEnter(Collider other){
         //if the platform is not moving
         if (!bIsMoving){
             //if the player pressed the 'C' key AND he has a controller
             if(Input.GetKeyUp(KeyCode.C) && Timer.controllerCount > 0){
                 //swap targets
                 if (currentTarget == targetA){
                     currentTarget = targetB;
                 }else {
                     currentTarget = targetA;
                 }
             }
         // and move platform to the new target:
         MovePlatform(transform.parent, currentTarget, speed);
         }
     }
 }

Maybe I'm doing something wrong, could someone be so kind to look over my code and help me try to figure this out? I believe I have set up everything correctly. Here is the setup for the actual platform Platform Start and here is it for the ending, floating, point Ending Point . Does this look like a correct set-up for this?

Please help me as soon as you can? This is one of the main mechanics in my game, and I would LOVE to get it to work! If anyone has a better way of trying to accomplish the same method (trigger move a player from PointA to PointB if he presses a key and has a controller) let me know?

Thanks.

2014-03-30_4-34-34.png (119.8 kB)
2014-03-30_4-35-02.png (119.0 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Decreasing a variable with triggers? 0 Answers

OnTriggerEnter2D working only on original prefab and not on its copies 1 Answer

OnTriggerEnter is not working! I have tried absolutely everything I can think of. 1 Answer

Peculiar multiple-rigidbody-trigger problem. 2 Answers

How to make Parent object's script ignore trigger component on children 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