Search Unity

Slerping issues

Discussion in 'Scripting' started by infiniteWin, Feb 20, 2020.

  1. infiniteWin

    infiniteWin

    Joined:
    Nov 28, 2019
    Posts:
    6
    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):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class player_control_sript : MonoBehaviour
    6. {
    7.     public int position = 0;
    8.     private int current_position = 0;
    9.     private float[,] positions = {{16,16},{16,10},{16,6},{16,2},{16,-2},{16,-6},{16,-10},{16,-16}};
    10.     private Vector3 next_pos = new Vector3(0,0,0);
    11.     void Start()
    12.     {
    13.        
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         while(current_position != position){
    20.             if(current_position != 7){
    21.                 next_pos = new Vector3(positions[(current_position+1),0],1.25f,positions[(current_position+1),1]);
    22.                 current_position += 1;
    23.             }
    24.             else{
    25.                 next_pos = new Vector3(positions[0,0],1.25f,positions[0,1]);
    26.                 current_position = 0;
    27.             }
    28.             transform.localPosition = Vector3.Slerp(transform.localPosition, next_pos, 10.0f * Time.deltaTime);
    29.            
    30.         }
    31.     }
    32. }
    33.  
     
  2. Josiah_Kunz

    Josiah_Kunz

    Joined:
    Jun 8, 2017
    Posts:
    10
    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):
    1. void Update()
    2.     {
    3.         int max_iterations = 8;
    4.         int iteration_count = 0;
    5.         while(current_position != position && iteration_count < max_iterations){
    6.             if(current_position != 7){
    7.                 next_pos = new Vector3(positions[(current_position+1),0],1.25f,positions[(current_position+1),1]);
    8.                 current_position += 1;
    9.             }
    10.             else{
    11.                 next_pos = new Vector3(positions[0,0],1.25f,positions[0,1]);
    12.                 current_position = 0;
    13.             }
    14.             transform.localPosition = Vector3.Slerp(transform.localPosition, next_pos, 10.0f * Time.deltaTime);
    15.            iteration_count += 1;
    16.         }
    17.     }
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    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.
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,113
    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.
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    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.
     
  6. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,113
    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...