0

in my project i have a bridge at the moment when i colide with the bridge it rotates and don't stop rotate, what i want is to rotate that bridge trough a specific point and stop rotate, that bridge is inside a gameobject that is my pivotPoint in general i rotate the pivotPoint not the bridge, so i did this:

using UnityEngine;
using System.Collections;

public class fallBridge : MonoBehaviour {

    private Rigidbody ball;
    public GameObject bridgePivot;
    private bool colided; 
    private bool rotating = true;
    // Update is called once per frame

    void Start(){
        colided = false;
    }

    void Update () {
        if (colided) {
            if (rotating) {
                Vector3 to = new Vector3 (0, 0, -85);
                if (Vector3.Distance (bridgePivot.transform.eulerAngles, to) > 0.01f) {
                    bridgePivot.transform.eulerAngles = Vector3.Lerp (bridgePivot.transform.rotation.eulerAngles, to, Time.deltaTime);
                } else {
                    bridgePivot.transform.eulerAngles = to;
                    rotating = false;
                }
            }
        }
    }

    void OnCollisionEnter(Collision other)
    {
        ball = GameObject.FindWithTag ("Player").GetComponent<Rigidbody> ();
        if (other.gameObject.tag == "Player" && ball.transform.localScale == new Vector3(2.0f,2.0f,2.0f)) {
            Debug.Log("ENTER");
            colided = true;
        }
    }
}

what am i doing wrong?? the colision detection works perfectly but on the update method it never stops rotate :S

  • debug.log it and see why the condition not met `if (Vector3.Distance (bridgePivot.transform.eulerAngles, to) > 0.01f)` – Bizhan Jan 14 '17 at 15:18

2 Answers2

1

You should do this with coroutine. Call the coroutine function and pass in the GameOjbect to rotate and the angle to rotate to when OnCollisionEnter is called. You can read how this function work here.

Te example below will rotate the GameObject -85 degree in z-axis within 3 seconds. You can change that to whatever suits you.

public class fallBridge : MonoBehaviour
{
    private Rigidbody ball;
    public GameObject bridgePivot;
    bool rotating = false;

    void OnCollisionEnter(Collision other)
    {
        ball = GameObject.FindWithTag("Player").GetComponent<Rigidbody>();
        if (other.gameObject.CompareTag("Player") && ball.transform.localScale == new Vector3(2.0f, 2.0f, 2.0f))
        {
            Debug.Log("ENTER");
            Vector3 rotationAngle = new Vector3(0, 0, -85);
            StartCoroutine(RotateObject(bridgePivot, rotationAngle, 3f));
        }
    }

IEnumerator RotateObject(GameObject gameObjectToMove, Vector3 eulerAngles, float duration)
{
    if (rotating)
    {
        yield break;
    }
    rotating = true;

    Vector3 newRot = gameObjectToMove.transform.eulerAngles + eulerAngles;

    Vector3 currentRot = gameObjectToMove.transform.eulerAngles;

    float counter = 0;
    while (counter < duration)
    {
        counter += Time.deltaTime;
        gameObjectToMove.transform.eulerAngles = Vector3.Lerp(currentRot, newRot, counter / duration);
        yield return null;
    }
    rotating = false;
}
Community
  • 1
  • 1
Programmer
  • 121,791
  • 22
  • 236
  • 328
0

The error is in this line:

bridgePivot.transform.eulerAngles = Vector3.Lerp (bridgePivot.transform.rotation.eulerAngles, to, Time.deltaTime);

When you use Lerp you typically have a fixed beginning and end, and you make the last parameter (t) go from 0 to 1. In your case the endpoint is fixed, the t parameters is fixed also and you vary your start. This means that each update you take a small step towards your goal, but this step is a percentage of the way. So each update you come closer, and your next step will be smaller.

What you should do is remember where you were when you started, and at what time you started. You also need to define a speed for your bridge. Then you can move the bridge from start to end and you get control over the speed.

public class fallBridge : MonoBehaviour {

    private Rigidbody ball;
    public GameObject bridgePivot;
    public float rotateDuration = 2; // Time in seconds
    // When you begin rotating you fill these three variables
    private float startTime;
    private Vector3 from;
    private bool rotating = false;
    // If the target doesn't change you might as well define it beforehand
    private Vector3 to = new Vector3 (0, 0, -85);

    void Update () {
        if (rotating) {
            float t = (Time.time - startTime) / rotateDuration;
            if (t < 1) {
                bridgePivot.transform.eulerAngles = Vector3.Lerp (from, to, t);
            } else {
                bridgePivot.transform.eulerAngles = to;
                rotating = false;
            }
        }
    }

    void OnCollisionEnter(Collision other)
    {
        ball = GameObject.FindWithTag ("Player").GetComponent<Rigidbody> ();
        if (other.gameObject.tag == "Player" && ball.transform.localScale == new Vector3(2.0f,2.0f,2.0f)) {
            Debug.Log("ENTER");
            startTime = Time.time; // Begin now
            from = bridgePivot.transform.eulerAngles; // Remember the start position
            rotating = true; // Signal to start rotating
        }
    }
}
Tubeliar
  • 836
  • 7
  • 20