- Home /
Scroll rect Autoscroll script almost working
Ok so in my project I'm creating the items menu and I made a scroll rect for it. After researching and finding out that autoscroll wasn't available already in Unity. I found a bunch of different scripts, but only one is almost working. It scrolls like I need it to, but It doesn't scroll all the way to the end of my item buttons/content. Can someone help me figure out what's wrong? Thanks in advance!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
[RequireComponent( typeof( ScrollRect ) )]
public class ScrollToSelected : MonoBehaviour {
public float scrollSpeed = 10f;
ScrollRect m_ScrollRect;
RectTransform m_RectTransform;
RectTransform m_ContentRectTransform;
RectTransform m_SelectedRectTransform;
void Awake() {
m_ScrollRect = GetComponent<ScrollRect>();
m_RectTransform = GetComponent<RectTransform>();
m_ContentRectTransform = m_ScrollRect.content;
}
void Update() {
UpdateScrollToSelected();
}
void UpdateScrollToSelected() {
// grab the current selected from the eventsystem
GameObject selected = EventSystem.current.currentSelectedGameObject;
if ( selected == null ) {
return;
}
if ( selected.transform.parent != m_ContentRectTransform.transform ) {
return;
}
m_SelectedRectTransform = selected.GetComponent<RectTransform>();
// math stuff
Vector3 selectedDifference = m_RectTransform.localPosition - m_SelectedRectTransform.localPosition;
float contentHeightDifference = ( m_ContentRectTransform.rect.height - m_RectTransform.rect.height );
float selectedPosition = ( m_ContentRectTransform.rect.height - selectedDifference.y );
float currentScrollRectPosition = m_ScrollRect.normalizedPosition.y * contentHeightDifference;
float above = currentScrollRectPosition - ( m_SelectedRectTransform.rect.height / 2 ) + m_RectTransform.rect.height;
float below = currentScrollRectPosition + ( m_SelectedRectTransform.rect.height / 2 );
// check if selected is out of bounds
if ( selectedPosition > above ) {
float step = selectedPosition - above;
float newY = currentScrollRectPosition + step;
float newNormalizedY = newY / contentHeightDifference;
m_ScrollRect.normalizedPosition = Vector2.Lerp( m_ScrollRect.normalizedPosition, new Vector2( 0, newNormalizedY ), scrollSpeed * Time.deltaTime );
} else if ( selectedPosition < below ) {
float step = selectedPosition - below;
float newY = currentScrollRectPosition + step;
float newNormalizedY = newY / contentHeightDifference;
m_ScrollRect.normalizedPosition = Vector2.Lerp( m_ScrollRect.normalizedPosition, new Vector2( 0, newNormalizedY ), scrollSpeed * Time.deltaTime );
}
}
}
Answer by billybob1978 · Mar 18, 2019 at 02:49 AM
What do you mean by autoscroll?
I have a chat and I make it autoscroll to the bottom every time I hit enter by using this.
scrollrect.velocity = new Vector2(0f, 1000f);
Not sure if that helps!
Answer by bicomicguy · Mar 18, 2019 at 03:54 AM
Like when selecting a button from the viewport and the ui automatically pushes the scrollrect to follow the buttons that are out of bounds/view of the scroll rect. Thanks for the reply, I'll check and see if it works.
Unfortunately it didn't work, it made the scroll weird.
Your answer
![](https://koobas.hobune.stream/wayback/20220612193813im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Moving a ScrollRect child via script to focus on some point. 0 Answers
Scrollbar touch sensitivity 0 Answers
How do I Make TextMeshPro InputField scroll through code? 0 Answers
[Solved] need help regarding autolayout in scrollrect 1 Answer
ScrollRect not working on mobile at certain resolution 0 Answers