Search Unity

Moving platform

Discussion in 'Scripting' started by healym, Jun 19, 2017.

  1. healym

    healym

    Joined:
    May 17, 2016
    Posts:
    19
    Hi,

    I'm making a variation on the tilting maze with a ball, but instead of using keystroke input to tilt the platform I'd like the platform to tilt automatically when the ball enters one of the colliders on all four sides. I'd like the ball to start in the middle of a flat platform and then once hit by a paddle, enter one of the four colliders. Once the ball triggers a collider the platform would tilt, sending the ball back into the opposite side. I've figured out how to make the basic tilt table using arrow keys to move it, but I'm not sure how to use colliders to accomplish the same thing. Is it possible to use colliders in this way? Here's the script I started working with:

    public class tilt1 : MonoBehaviour {

    public Vector3 currentRot;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    currentRot = GetComponent<Transform> ().eulerAngles;

    if ((Input.GetAxis ("Horizontal") > .2) && (currentRot.z <=10 || currentRot.z >=347))
    {
    transform.Rotate (0, 0, 1);

    }
    if ((Input.GetAxis ("Horizontal") < -.2) && (currentRot.z >=349 || currentRot.z <=11))
    {
    transform.Rotate (0, 0, -1);

    }
    if ((Input.GetAxis ("Vertical") > .2) && (currentRot.x <=10 || currentRot.x >=347))
    {
    transform.Rotate (1, 0, 0);

    }
    if ((Input.GetAxis ("Vertical") < -.2) && (currentRot.x >=349 || currentRot.x <=11))
    {
    transform.Rotate (-1, 0, 0);

    }
    }
    }
     
  2. Seppi

    Seppi

    Joined:
    Nov 10, 2012
    Posts:
    162
    You basically need to put your rotate code in OnCollisionEnter (other : Collision){} and remove the Input methods. It's probably easiest to put different scripts on each collider individually.
     
  3. healym

    healym

    Joined:
    May 17, 2016
    Posts:
    19
    Thank you for responding Seppi! It sounds pretty straight forward when you say it, but I'm not sure how to implement it. Can you show me what you mean in a brief script?
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Make sure and use Code Tags when pasting code snippets.
     
  5. Seppi

    Seppi

    Joined:
    Nov 10, 2012
    Posts:
    162
    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class ExampleClass : MonoBehaviour
    7. {
    8.  
    9.     void OnCollisionEnter(Collision collision)
    10.    {
    11.      
    12.           //Rotate Code Here
    13.    }
    14. }
    15.  
    16.  
    Then make a new one of those for every collider with the corresponding behavior.
     
  6. healym

    healym

    Joined:
    May 17, 2016
    Posts:
    19
    Thanks, but for some reason it didn't work. Here is the code I used on a collider that the ball passes through.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class ExampleClass : MonoBehaviour
    7. {
    8.  
    9.     void OnCollisionEnter(Collision collision)
    10.     {
    11.  
    12.         transform.Rotate (0, 0, 1);
    13.     }
    14. }
    I still have the input controls for the arrow keys being used on the table. Would they be creating a conflict with the collider?
     
  7. Seppi

    Seppi

    Joined:
    Nov 10, 2012
    Posts:
    162
    It's probably working. OnCollisionEnter is called on;y when the collision happens. So your code is rotating the object by one degree when the collision occurs. You need to have it lerp to whatever rotation you need.
     
  8. healym

    healym

    Joined:
    May 17, 2016
    Posts:
    19
    I wish it was, but even after changing rotation values nothing happens. I have the collider set to isTrigger, is that correct?
     
  9. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807