- Home /
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 and here is it for the ending, floating, 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.
Your answer
Follow this Question
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