I have used this code before in a previous project but this time something isn't working. I'm not good with c# so it might be my implementation of the array. My aim is to slerp the player from one x,z in the list to another going through all in between. Last time I tested this it made Unity crash. Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class player_control_sript : MonoBehaviour { public int position = 0; private int current_position = 0; private float[,] positions = {{16,16},{16,10},{16,6},{16,2},{16,-2},{16,-6},{16,-10},{16,-16}}; private Vector3 next_pos = new Vector3(0,0,0); void Start() { } // Update is called once per frame void Update() { while(current_position != position){ if(current_position != 7){ next_pos = new Vector3(positions[(current_position+1),0],1.25f,positions[(current_position+1),1]); current_position += 1; } else{ next_pos = new Vector3(positions[0,0],1.25f,positions[0,1]); current_position = 0; } transform.localPosition = Vector3.Slerp(transform.localPosition, next_pos, 10.0f * Time.deltaTime); } } }
Did Unity crash completely, without an error message? If so, it sounds like the while loop ran too long. It looks like you're setting position from the editor---are you certain that it's set to a reasonable number (0-7) in the inspector? Another try might be using a failsafe. Try adding a maximum iteration count to your code: Code (CSharp): void Update() { int max_iterations = 8; int iteration_count = 0; while(current_position != position && iteration_count < max_iterations){ if(current_position != 7){ next_pos = new Vector3(positions[(current_position+1),0],1.25f,positions[(current_position+1),1]); current_position += 1; } else{ next_pos = new Vector3(positions[0,0],1.25f,positions[0,1]); current_position = 0; } transform.localPosition = Vector3.Slerp(transform.localPosition, next_pos, 10.0f * Time.deltaTime); iteration_count += 1; } }
Setting a transform's position over and over in a loop doesn't make sense; no frames are passing between iterations of the loop, so only the last position you set will matter. Pretty sure the code as presented would never have worked. (You may have inadvertently changed some important detail in the process of "moving" it from another location?) Also, it's not clear what the "position" variable is used for or where it's being set, but if it ever got set to a value less than 0 or greater than 7, then your loop would run infinitely, because current_position would never become equal to position. That could explain Unity crashing.
Actually, if the implementation details haven't changed in the recent times, Unity overloaded Vector3 equality operators, something similar to static public bool operator ==(Vector3 a, Vector3 b) { return Mathf.Approximately(a.x, b.x) && Mathf.Approximately(a.y, b.y) && Mathf.Approximately(a.z, b.z); }, something I'm not a big fan of, because it lets noobs slip by with "good enough" code design that typically explodes when no one is looking. But that's just a side note. The code, as presented, is nebulous.
Starting with "actually" implies this is a correction, but I don't see how that relates to anything discussed so far in this thread. The variables "position" and "current_position" are ints.
Yes, you're right, I haven't paid too much attention to it. Didn't mean to correct you at all, it was merely a side note. More like a possible explanation for why it might have worked before. But it probably didn't so...