ScrollRect Snapping

Hi guys,

I’ve been following a tutorial to get to grips with UI and I’ve come across a problem I can’t seem to figure out.

public class ScrollRectSnap : MonoBehaviour {

	public RectTransform panel; // Holds the scroll panel
	public RectTransform[] bttn;
	public RectTransform center; // Center to compare distance for each button

	private float[] distance; // All buttons' distance to the center
	private bool dragging = false; //Will be true while being dragged
	private int bttnDistance; //Holds distance between buttons
	private int minButtonNum; //Holds the number of button, with smallest distance to center

	void Start(){

		int bttnLength = bttn.Length;
		distance = new float[bttnLength]; 

		//Get distance between buttons
		bttnDistance = (int)Mathf.Abs(bttn[1].GetComponent<RectTransform>().anchoredPosition.x - bttn[0].GetComponent<RectTransform>().anchoredPosition.x);

	}

	void Update(){

		for (int i = 0; i < bttn.Length; i++) {
			//Calculates the distance from the center for each button in the scene, see debug of this script object
			distance _= Mathf.Abs(center.transform.position.x - bttn*.transform.position.x);*_

* }*

* float minDistance = Mathf.Min (distance); // Gets the min distance of objects in distance array*

* for (int a = 0; a < bttn.Length; a++) {*

* if (minDistance == distance[a])*
* {*
* minButtonNum = a; // Holds the value of the button with the smallest distance to the center*
* }*
* }*

* if (!dragging) {*

_ LerpToBttn(minButtonNum * -bttnDistance);_
* }*
* }*

* public void LerpToBttn(int position){*

_ float newX = Mathf.Lerp (panel.anchoredPosition.x, position, Time.deltaTime * 2f);_
* Vector2 newPosition = new Vector2 (newX, panel.anchoredPosition.y);*

* panel.anchoredPosition = newPosition;*

* }*

* public void StartDrag(){*

* dragging = true;*
* }*

* public void EndDrag(){*

* dragging = false;*
* }*
}
LerpToBttn function gives the correct output when I use print() but `panel.anchoredPosition = newPosition; does not seem to be setting the anchoredPosition at all, resulting in the panel not snapping into place. After much tweaking and messing around I still can’t seem to figure out why.
Any help will be much appreciated,
Many thanks,
Takz

The issue was using Time.deltaTime for the Mathf.Lerp. I used Time.fixedDeltaTime instead and that seemed to fix it. In case for any reason someone needs this in the future.