- Home /
quaternion.lerp not working properly after loading from menu scene to play scene
hi there, i m new to unity i have problem after loading from menu scene to play scene.
here i have 2 scripts, one script instantiate the prefab and the prefab is attached the script that have translate and lerp between the quaternion angle, when i play the "play" scene in the editor its working fine but when i start from the menu scene lerp between quaternion angle not working properly please suggest me to get things correctly.
this is the generator script that instantiate the prefab
public class ObstacleGenerator : MonoBehaviour
{
private GameObject obstacleGenerator;
public GameObject[] obstaclePrefab;
private Vector2 obstPos;
public static ObstacleGenerator Instance;
public float speed = 7;
private GameObject fishGenerator;
public GameObject[] fishPrefab;
private Vector2 fishPos;
void OnEnable ()
{
Instance = this;
//if (PlayerMovement.stopObstacle == false)
//InvokeRepeating ("Spawn", 0f, Random.Range (2f, 5f));
InvokeRepeating ("SpeedMovement", 2f, 5f);
InvokeRepeating ("FishMove", 2f, 3f);
InvokeRepeating ("JumpFish", 2f, Random.Range (2, 4));
}
void SpeedMovement ()
{
speed ++;
if (speed == 14f) {
speed = 7f;
}
}
void FishMove ()
{
if (SettingsManager.instance.isPause)
return;
int size = Random.Range (0, fishPrefab.Length);
fishGenerator = fishPrefab [size];
Instantiate (fishGenerator, transform.position, Quaternion.identity);
}
private GameObject jumpFishGen;
public GameObject[] jumpFishes;
private Vector2 jumpFishPos;
void JumpFish ()
{
//int size = Random.Range (0, jumpFishes.Length);
jumpFishPos = new Vector2 (Random.Range (9f, 16f), -0.3f);
jumpFishGen = jumpFishes [Random.Range (0, jumpFishes.Length)];
Instantiate (jumpFishGen, jumpFishPos, Quaternion.identity);
}
}
Blockquote
this script is attached to the prefab
using UnityEngine;
using System.Collections;
public class JumpFish : MonoBehaviour
{
private Quaternion start;
private Quaternion end;
public float speed;
void Start ()
{
speed = Random.Range (5, 8);
start = Quaternion.Euler (0f, 0f, -50f);
end = Quaternion.Euler (0f, 0f, 50f);
}
void Update ()
{
Movement ();
}
void Movement ()
{
if (SettingsManager.instance.isPause)
return;
transform.Translate (-Time.deltaTime * speed, 0, 0);
transform.rotation = Quaternion.Lerp (start, end, Mathf.PingPong (Time.time, 1f));
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.name == "DestroyPrefab") {
Destroy (gameObject);
}
}
}
i have solved this problem by rewriting the script attached to the prefab,
i took one private variables
private startTime; void Update(){ startTime = Time.timeScenceLevelLoad; // then i have edited in transform.rotation
transform.rotation = Quaternion.Lerp (start, end, $$anonymous$$athf.PingPong (startTime, 1f)); }