If scrollIntoView working not good enough / scrollIntoView not working / scrollIntoView alternative - js-scrollintoview

I have faced with the problem that scrollIntoView some times wokrs good, some times no (in most cases not good).So I tried to find an alternative and some kind of it was this article.
But in my program i needed also a horizontal scrolling to element and vertical scrolling showing element in the top of parent window.
So I modifed code from article.
/* This is the main function where which pass two ref parameters of Parent element & Child element */
function scrollIntoView(parent, child) {
const parentBounding = parent.getBoundingClientRect(),
clientBounding = child.getBoundingClientRect();
const parentTop = parentBounding.top,
clientTop = clientBounding.top,
parentLeft = parentBounding.left,
clientLeft = clientBounding.left - 400; /* 400 is a shift so that each difference is not nailed to the upper left edge. You can delete it */
if (parentTop >= clientTop) {
scrollTo(parent, -(parentTop - clientTop), 300, 'vertical');
}
else {
scrollTo(parent, clientTop - parentTop, 300, 'vertical');
}
if (parentLeft >= clientLeft) {
scrollTo(parent, -(parentLeft - clientLeft), 300, 'horizontal');
}
else {
scrollTo(parent, clientLeft - parentLeft, 300, 'horizontal');
}
}
function scrollTo(element, to, duration, type) {
let start = (type == 'vertical') ? element.scrollTop : element.scrollLeft,
currentTime = 0,
increment = 20;
let animateScroll = function() {
currentTime += increment;
let val = easeInOutQuad(currentTime, start, to, duration);
if (type == 'vertical') {
element.scrollTop = val;
}
else {
element.scrollLeft = val;
}
if (currentTime < duration) {
setTimeout(animateScroll, increment);
}
}
animateScroll();
}
/* Function for smooth scroll animation with the time duration */
function easeInOutQuad(time, startPos, endPos, duration) {
time /= duration / 2;
if (time < 1) return (endPos / 2) * time * time + startPos;
time--;
return (-endPos / 2) * (time * (time - 2) - 1) + startPos;
}

This feature is experimental (At the time of my comment)
Both scrollIntoView() its scrollIntoViewOption.

Related

How to limit player's velocity only when it is accelerating?

I have a 2.5D space shooter game in progress that uses Unity's built-in physics. Everything happens in 2D space but all the models are 3D.
The player (a space ship) can rotate using a controller axis and can accelerate when a button is held down (e.g. xbox controller's A button).
There is a limit on how fast the player can move (maxEngineSpeed) and I clamp the magnitude of the RigidBody's velocity in FixedUpdate as follows:
if (rb.velocity.magnitude > maxEngineSpeed)
{
rb.velocity = Vector2.ClampMagnitude(rb.velocity, maxEngineSpeed);
}
Now the problem is that this prevents the veclocity from ever reaching a value higher than maxEngineSpeed .
I want a behaviour that only limits the velocity when the player is accelerating. If the player somehow gains more speed from a coillision or from a bullet hit, then the velocity should not be limited. We can think it like the space ship not having enough power in its engines to go any faster. It's like linear drag but only when accelerating (when not accelerating, the ship doesn't decelerate at all). I have power-ups that grant the player more maximum speed, so it's important.
How would this be implemented? I've tried to only limit the velocity when the player is accelerating, but then it clamps it immediately to specified value and it looks unnatural. Would a couroutine work that would slowly reduce the magnitude when accelerating? But then it would have to take account the direction of the player and current velocity?
EDIT: Clarification: in practise what I would like to have is to ask a RigidBody "if I apply this force to you while you're moving faster than maxEngineSpeed, would it increase your speed? If it would, don't apply the force, if it would decrease your speed, then apply it".
EDIT: changed the maxSpeed variable name to maxEngineSpeed for more clarity.
Remove the clamping in FixedUpdate. Instead, add a check where you add Velocity (where you detect Xbox Controllers 'A' is pressed).
Something like:
if(Input.GetButton("Xbox-A"))
{
if(rb.velocity.magnitude < scaledMaxSpeed)
{
rb.addForce(...);
}
}
So if you are faster than your max-speed, the ship cannot accelerate more (by own power).
Pull down & Drag
There are so many ways to achieve what you want. Below I show two possible methods with a working demo to allow you to get a bit of a feel for how the perform and differ. Also link at bottom to another demo.
Pull down
You can pull down the velocity by defining a max over speed and a over speed drag coefficient
The pull down method
Define settings
float pullDown = 0.1f; // dimensionless > 0, < 1
float maxOverSpeed = 5.0f;
float maxSpeed = 4.0f
float acceleration = 0.1f;
Per frame
if (accelerate && speed < maxSpeed) { speed += acceleration }
// clamp max over speed
speed = speed > maxOverSpeed ? maxOverSpeed : speed;
float speedAdjust = speed - maxSpeed;
// pull speed down if needed
speed -= speedAdjust > 0.0f ? speedAdjust * pullDown : 0.0f;
// set the velocity magnitude to the new speed
Personally I don't like this method as it is a coasting model, ship gets to speed an holds it, there is no deceleration, but it does give finer control over velocity.
Drag
My preferred method is to use a simple drag coefficient. Slight modification to add extra draw when over speed
However this makes is difficult to know what the max speed will be given some acceleration. There is a formula that will give you a drag coefficient to match a max speed for acceleration, or acceleration to match a max speed for a drag coefficient, but off the top of my head I can not remember it as its been years since I found I needed to use it.
I wing it and define an approximation, test it, and refine till I get what feels right. In reality if ask what is the max speed of the player? All i know is not too fast and not too slow. :P
The drag method
Define
float acceleration = 0.1f;
float drag = 1.0f - 0.021f;
float overSpeedDrag = 1.0f - 0.026f;
float maxSpeed = 4;
Per frame
// apply drag depending on speed
speed *= speed > maxSpeed ? overSpeedDrag : drag;
if (accelerate) { speed += acceleration }
// set the velocity magnitude to the new current speed
Example
These methods as code do not give much of a feel for the actual results so the following snippet implements both methods so you can see and feel how they work.
The code is at the top (in JavaScript) the two different methods are flagged PULL_DOWN, DRAG in the function update() {
Ship speeds are in pixels per second (Pps)
Both ships have same acceleration constant, however ship B (drag method) does not accelerate at a constant rate.
Ship A will coast, ship B will always come to a stop.
Click bump to kick the ship's velocity.
const accelFunction = {
get vel() { return new Vec2(0, 0) },
speed: 0,
acceleration: 0.1,
maxSpeed: 4,
// drag constants
drag: 1 - 0.0241,
overSpeedDrag: 1 - 0.0291,
// pulldown constants;
overSpeed: 5,
pullDown: 0.1,
update() {
if (this.method === DRAG) { // Drag method
this.speed *= this.speed > this.maxSpeed ? this.overSpeedDrag: this.drag;
if (this.accelerate) { this.speed += this.acceleration }
} else { // Pull down method
if (this.accelerate && this.speed < this.maxSpeed) { this.speed += this.acceleration }
this.speed = this.speed > this.maxOverSpeed ? this.maxOverSpeed : this.speed;
var speedAdjust = this.speed - this.maxSpeed;
this.speed -= speedAdjust > 0 ? speedAdjust * this.pullDown : 0;
}
// move ship
this.vel.length = this.speed;
this.pos.add(this.vel);
},
}
/* rest of code unrelated to anwser */
requestAnimationFrame(start);
const ctx = canvas.getContext("2d");
const PULL_DOWN = 0;
const DRAG = 1;
var shipA, shipB;
var bgPos;
function Ship(method, control, controlBump) { // creates a Player ship
control.addEventListener("mousedown",() => API.go());
control.addEventListener("mouseup",() => API.coast());
control.addEventListener("mouseout",() => API.coast());
controlBump.addEventListener("click",() => API.bump());
const API = {
...accelFunction,
pos: new Vec2(100, 50 + method * 50),
method, // 0 drag method, 1 pulldown
draw() {
ctx.setTransform(1,0,0,1,this.pos.x - bgPos.x, this.pos.y)
ctx.strokeStyle = "#FFF";
ctx.lineWidth = 2;
ctx.beginPath();
ctx.lineTo(20, 0);
ctx.lineTo(-20, -20);
ctx.lineTo(-20, 20);
ctx.closePath();
ctx.stroke();
ctx.fillText(this.method ? "B" : "A", -11, 3);
ctx.fillText((this.speed * 60 | 0) + "Pps", 80, 3);
if (this.accelerate) {
ctx.strokeStyle = "#FF0";
ctx.beginPath();
ctx.lineTo(-20, -10);
ctx.lineTo(-30 - Math.rand(0,10), 0);
ctx.lineTo(-20, 10);
ctx.stroke();
}
},
focus: false,
reset() {
this.focus = false;
this.vel.zero();
this.pos.init(100, 50 + this.method * 50);
this.speed = 0;
this.accelerate = false;
},
go() {
this.accelerate = true;
this.focus = true;
if (this.method === 1) { shipA.reset() }
else { shipB.reset() }
},
coast() {
this.accelerate = false;
},
bump() {
this.speed += 1;
},
};
return API;
}
function start() {
init();
requestAnimationFrame(mainLoop);
}
function mainLoop() {
ctx.setTransform(1,0,0,1,0,0);
ctx.clearRect(0,0,500,170);
shipA.update();
shipB.update();
bgPos.x = shipA.focus ? shipA.pos.x - 50 : shipB.pos.x - 50 ;
drawBG(bgPos);
shipA.draw();
shipB.draw();
requestAnimationFrame(mainLoop);
}
function drawBG(bgPos) {
ctx.fillStyle = "#FFF";
ctx.beginPath();
const bgX = -bgPos.x + 100000;
for (const p of background) {
x = (p.x + bgX) % 504 - 2;
ctx.rect(x, p.y, 2, 2);
}
ctx.fill();
}
const BG_COUNT = 200;
const background = [];
function init() {
ctx.font = "16px arial";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
bgPos = new Vec2();
shipA = Ship(PULL_DOWN, goA, bumpA);
shipB = Ship(DRAG, goB, bumpB);
var i = BG_COUNT;
while (i--) {
background.push(new Vec2(Math.rand(0, 10000), Math.rand(-1, 170)));
}
}
/* Math LIB Vec2 and math extensions */
Math.rand = (m, M) => Math.random() * (M - m) + m;
function Vec2(x = 0, y = (temp = x, x === 0 ? (x = 0 , 0) : (x = x.x, temp.y))) { this.x = x; this.y = y }
Vec2.prototype = {
init(x, y = (temp = x, x = x.x, temp.y)) { this.x = x; this.y = y; return this },
zero() { this.x = this.y = 0; return this },
add(v, res = this) { res.x = this.x + v.x; res.y = this.y + v.y; return res },
scale(val, res = this) { res.x = this.x * val; res.y = this.y * val; return res },
get length() { return this.lengthSqr ** 0.5 },
set length(l) {
const len = this.lengthSqr;
len > 0 ? this.scale(l / len ** 0.5) : (this.x = l);
},
get lengthSqr() { return this.x * this.x + this.y * this.y },
};
canvas {background: #347;}
div {
position: absolute;
top: 150px;
left: 20px;
}
span { color: white; font-family: arial }
<canvas id="canvas" width="500" height="170"></canvas>
<div>
<button id="goA">Go A</button>
<button id="bumpA">Bump A</button>
<button id="goB">Go B</button>
<button id="bumpB">Bump B</button>
<span> Methods: A = Pull down B = Drag </span>
</div>
No limit
There are many variations on these methods, and the are many example on SO (I have written many answers in the subject. eg See demo snippet (bottom of answer) for example of drag method modification) .
Which method you use is very dependent on how you want the interaction to feel, there is no right or wrong method as game physics will is very different than real physics.
Knowing that acceleration (a) is the change in velocity (Δv) over the change in time (Δt), I'll check that.
Maybe with something like (pseudo):
float lastVelocity = 0;
bool isAccelerating = false;
Update()
{
float currentVelocity = rb.velocity;
if(currentVelocity > lastVelocity)
{
isAccelerating = true;
lastVelocity = currentVelocity;
}
else
{
isAccelerating = false;
}
}
Now you know when your "ship" is speedingUp, the only way to decrease the speed is caused by external forces (like gravity, or friction), depending of your setup, I'll deactivate those forces, or change the physicalMaterial that is causing the friction.

Move Object around a platform of tiles in Unity

I want to make a spinning spike to move around a platform made out of tiles like in the following
I have written every possible state on where to move when platform tiles block the spike's way. It would look something like this
for (int i = 0; i < platformTileMap.Length; i++)
{
if (platformTileMap[i].HasTile(cellPosition + new Vector3Int(0, -1, 0))) // BOTTOM
{
moveX = moveDir;
}
else if (platformTileMap[i].HasTile(cellPosition + new Vector3Int(0, 1, 0))) // TOP
{
moveX = -moveDir;
}
else if (platformTileMap[i].HasTile(cellPosition + new Vector3Int(-1, -1, 0))) //BOT LEFT
{
if (moveDir == 1)
{
moveY = -1;
}
else moveX = moveDir;
}
else if (platformTileMap[i].HasTile(cellPosition + new Vector3Int(1, 1, 0))) //TOP RIGHT
{
if (moveDir == 1)
{
moveY = 1;
}
else moveX = -moveDir;
}
else if (platformTileMap[i].HasTile(cellPosition + new Vector3Int(1, -1, 0))) // BOT RIGHT
{
if (moveDir == 1)
{
moveX = moveDir;
}
else
{
moveY = -1;
}
}
else if (platformTileMap[i].HasTile(cellPosition + new Vector3Int(-1, 1, 0))) // TOP LEFT
{
if (moveDir == -1)
{
moveY = 1;
}
else
{
moveX = -moveDir;
}
}
I feel like there has to be a more efficient way to solve this. Do I really have to write every possibility in if statements? Can I achieve this with pathfinding?
How about this
Use raycast
Place Empty GameObject and rotate spike when it arrives them
You could create an array of points to move to.
public Vector3[] movePoints;
You can set movePoints in inspector or in code (such as Start function) user choice
In the update loop lerp to the next point in sequence, when arrived pull the next point, unless we are at the end of the array then pull the first point in the array, rinse and repeat forever.
https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html
Set this up properly once, when you make more blades and different configurations or want to change speed it will be ez pz.
For anyone interested. Here is how i solved it:
GridLayout platformGridLayout;
Grid platformGrid;
Tilemap[] platformTileMap;
[SerializeField] float rotationSpeed;
[SerializeField] float moveSpeed;
[SerializeField] private LayerMask platformLayerMask;
Vector3Int startPos;
Vector3Int currentCellPosition;
Vector3 raycastPlatformDir;
Vector3 raycastMoveDir;
float platformRaycastDist;
// Start is called before the first frame update
void Start()
{
movePoints = new List<Vector3Int>();
platformGridLayout = transform.parent.GetComponentInParent<GridLayout>();
platformGrid = transform.parent.GetComponentInParent<Grid>();
startPos = platformGridLayout.WorldToCell(transform.position);
Debug.Log("Cell Startposition: " + startPos);
PlatformToMoveOn();
GetStartRaycastDir();
platformRaycastDist = platformGridLayout.cellSize.x;
Debug.Log("CellCenterToWorld of Startposition: " + platformGrid.GetCellCenterWorld(startPos));
Debug.Log(platformGrid.GetCellCenterLocal(currentCellPosition + Vector3Int.FloorToInt(raycastPlatformDir) + Vector3Int.FloorToInt(raycastMoveDir)));
}
private void PlatformToMoveOn()
{
platformTileMap = new Tilemap[2];
platformTileMap[0] = GameObject.Find("Platform").GetComponent<Tilemap>();
platformTileMap[1] = GameObject.Find("MovingPlatform").GetComponent<Tilemap>();
}
private void GetStartRaycastDir()
{
for (int i = 0; i < platformTileMap.Length; i++)
{
if (platformTileMap[i].HasTile(startPos + new Vector3Int(0, -1, 0))) // BOTTOM
{
raycastPlatformDir = Vector3.down;
}
else if (platformTileMap[i].HasTile(startPos + new Vector3Int(0, 1, 0))) // TOP
{
raycastPlatformDir = Vector3.up;
}
else if (platformTileMap[i].HasTile(startPos + new Vector3Int(1, 0, 0))) // RIGHT
{
raycastPlatformDir = Vector3.right;
}
else if (platformTileMap[i].HasTile(startPos + new Vector3Int(-1, 0, 0))) // LEFT
{
raycastPlatformDir = Vector3.left;
}
}
raycastMoveDir = Quaternion.Euler(0, 0, 90) * raycastPlatformDir * Mathf.Sign(moveSpeed);
//raycastMoveDir = new Vector3(raycastPlatformDir.y, raycastPlatformDir.x) * Mathf.Sign(moveSpeed);
}
// Update is called once per frame
void Update()
{
MoveSpike();
}
private void MoveSpike()
{
currentCellPosition = platformGridLayout.WorldToCell(transform.position); // + raycastPlatformDir * platformGridLayout.cellSize.y / 2;
// Debug.Log(cellPosition);
//Debug.Log(raycastMoveDir);
transform.Rotate(0, 0, 300 * rotationSpeed * Time.deltaTime);
RaycastHit2D raycastMove = Physics2D.Raycast(platformGrid.GetCellCenterLocal(currentCellPosition),raycastMoveDir,0.01f,platformLayerMask);
RaycastHit2D raycastPlatform = Physics2D.Raycast(platformGrid.GetCellCenterLocal(currentCellPosition), raycastPlatformDir, platformRaycastDist, platformLayerMask);
Debug.DrawRay(transform.position, raycastMoveDir * 0.01f, Color.red);
Debug.DrawRay(transform.position, raycastPlatformDir * platformRaycastDist, Color.green);
if (currentCellPosition != startPos) { // Check on Platform corners
Debug.Log("Checking");
if (raycastMove.collider != null)
{
// reassign raycastsdirections
RotateRaycastDirections(1);
Debug.Log("Spike Collision");
}
else if (raycastPlatform.collider == null)
{
RotateRaycastDirections(-1);
Debug.Log("Spike on Platform");
}
startPos = currentCellPosition;
}
/*transform.position = Vector3.MoveTowards(transform.position,
platformGrid.GetCellCenterLocal(currentCellPosition + Vector3Int.FloorToInt(raycastPlatformDir) + Vector3Int.FloorToInt(raycastMoveDir)), moveSpeed * Time.deltaTime); */
transform.Translate(raycastMoveDir.x * Mathf.Abs(moveSpeed) * Time.deltaTime, raycastMoveDir.y * Mathf.Abs(moveSpeed) * Time.deltaTime, 0, Space.World);
}
private void RotateRaycastDirections(int angle)
{
raycastPlatformDir = Quaternion.Euler(0, 0, 90) * raycastPlatformDir * angle * Mathf.Sign(moveSpeed);
raycastMoveDir = Quaternion.Euler(0, 0, 90) * raycastMoveDir * angle * Mathf.Sign(moveSpeed);
// raycastPlatformDir = new Vector3(raycastPlatformDir.y, raycastPlatformDir.x) * angle;
//raycastMoveDir = new Vector3(raycastMoveDir.y, raycastMoveDir.x) * -angle;
}
Edit:
This doesnt work on moving Platforms though. Any ideas how i could fix that? I tried changing the raycast position to the tilemapscentercell position but it doesnt work.

Unity3D : How to hide touchscreen keyboard when i select inputfile and inputfield still focus

I've had this problem for a long time. I want to hide touchscreen keyboard when I select inputfile and inputfield still focus. I don't need touchscreen keyboard but I need carretpostion and focus on inputfield(application like calculator).
Thank you everyone for the answer. I solved the problem by custom inputfield. I disable touchscreen keyboard and get carretpostion by OnPointerUp .
code :
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class InputFieldWithOutKeyboard : InputField
{
protected override void Start()
{
keyboardType = (TouchScreenKeyboardType)(-1);
}
public override void OnPointerDown(UnityEngine.EventSystems.PointerEventData eventData)
{
base.OnPointerDown(eventData);
}
public override void OnPointerUp(PointerEventData eventData)
{
Vector2 mPos;
RectTransformUtility.ScreenPointToLocalPointInRectangle(textComponent.rectTransform, eventData.position, eventData.pressEventCamera, out mPos);
Vector2 cPos = GetLocalCaretPosition();
int pos = GetCharacterIndexFromPosition(mPos);
Debug.Log("pos = " + pos);
GameObject.FindWithTag("canvas").GetComponent<Calculator>().carretPostion = pos;
GameObject.FindWithTag("canvas").GetComponent<Calculator>().carretVector = mPos;
base.OnPointerUp(eventData);
}
public Vector2 GetLocalCaretPosition()
{
// if (isFocused)
// {
TextGenerator gen = m_TextComponent.cachedTextGenerator;
UICharInfo charInfo = gen.characters[caretPosition];
float x = (charInfo.cursorPos.x + charInfo.charWidth) / m_TextComponent.pixelsPerUnit;
float y = (charInfo.cursorPos.y) / m_TextComponent.pixelsPerUnit;
Debug.Log("x=" + x + "y=" + y);
return new Vector2(x, y);
// }
// else
// return new Vector2(0f, 0f);
}
private int GetCharacterIndexFromPosition(Vector2 pos)
{
TextGenerator gen = m_TextComponent.cachedTextGenerator;
if (gen.lineCount == 0)
return 0;
int line = GetUnclampedCharacterLineFromPosition(pos, gen);
if (line < 0)
return 0;
if (line >= gen.lineCount)
return gen.characterCountVisible;
int startCharIndex = gen.lines[line].startCharIdx;
int endCharIndex = GetLineEndPosition(gen, line);
for (int i = startCharIndex; i < endCharIndex; i++)
{
if (i >= gen.characterCountVisible)
break;
UICharInfo charInfo = gen.characters[i];
Vector2 charPos = charInfo.cursorPos / m_TextComponent.pixelsPerUnit;
float distToCharStart = pos.x - charPos.x;
float distToCharEnd = charPos.x + (charInfo.charWidth / m_TextComponent.pixelsPerUnit) - pos.x;
if (distToCharStart < distToCharEnd)
return i;
}
return endCharIndex;
}
private int GetUnclampedCharacterLineFromPosition(Vector2 pos, TextGenerator generator)
{
// transform y to local scale
float y = pos.y * m_TextComponent.pixelsPerUnit;
float lastBottomY = 0.0f;
for (int i = 0; i < generator.lineCount; ++i)
{
float topY = generator.lines[i].topY;
float bottomY = topY - generator.lines[i].height;
// pos is somewhere in the leading above this line
if (y > topY)
{
// determine which line we're closer to
float leading = topY - lastBottomY;
if (y > topY - 0.5f * leading)
return i - 1;
else
return i;
}
if (y > bottomY)
return i;
lastBottomY = bottomY;
}
// Position is after last line.
return generator.lineCount;
}
private static int GetLineEndPosition(TextGenerator gen, int line)
{
line = Mathf.Max(line, 0);
if (line + 1 < gen.lines.Count)
return gen.lines[line + 1].startCharIdx - 1;
return gen.characterCountVisible;
}
}
You can use something like this
TouchScreenKeyboard keyboard;
void Update()
{
if (keyboard != null)
{
if (Input.deviceOrientation == DeviceOrientation.FaceDown)
keyboard.active = false;
if (Input.deviceOrientation == DeviceOrientation.FaceUp)
keyboard.active = true;
}
}
it will retrieve the TouchScreenKeyboard and after that, you can active or deactive it as you want.

How to move the character in a certain way?

There is a way. The character starts at the midpoint. Then it should move to the right, if HorizontalAxis > 0 and to the left, if < 0. I have did something like this -
Way:
public Transform[] PatrolPoints;
public Transform Way;
public int CurrentPoint;
public int NeedPoint;
if (transform.position == PatrolPoints[CurrentPoint].position && CurrentPoint < PatrolPoints.Length-1)
{
//PatrolPoints[CurrentPoint];
NeedPoint = CurrentPoint + 1;
}
else if(transform.position == PatrolPoints[NeedPoint].position && CurrentPoint != PatrolPoints.Length - 1)
{
CurrentPoint++;
}
else if(transform.position == PatrolPoints[PatrolPoints.Length - 1].position)
{
NeedPoint--;
}
if (CurrentPoint != PatrolPoints.Length - 1 && moveAxis > 0)
{
transform.position = Vector3.MoveTowards(transform.position, PatrolPoints[NeedPoint].position, moveAxis * moveRate * Time.deltaTime);
//An error occurs here
}
else if(CurrentPoint > 0 && moveAxis < 0)
{
transform.position = Vector3.MoveTowards(transform.position, PatrolPoints[NeedPoint - 1].position, -moveAxis * moveRate * Time.deltaTime);
}
Length of the array - 3 elements.
From the middle point to the right, the character normally goes both left and right, but as soon as I get to the last point, an error occurs -
Array index is out of range
During the error variables are:
NeedPoint: -1;
CurrnetPoint: 2;
And from the middle to the left point the character does not go at all.
this is the solution for:
The character starts at the midpoint. Then it should move to the
right, if HorizontalAxis > 0 and to the left, if < 0.
i assumed that "move to the right" means increasing the index in array and "move to the left" means decreasing the index in array
public Transform[] PatrolPoints;
public int NextPointOnLeft = 0; // because player starts at 1
public int NextPointOnRight = 2; // because player starts at 1
if (HorizontalAxis < 0)
{
if (transform.position == PatrolPoints[NextPointOnLeft].position)
{
if(NextPointOnLeft > 0)
{
--NextPointOnLeft;
}
// EDIT: NextPointOnRight = NextPointOnLeft + 1;
}
// EDIT:
NextPointOnRight = NextPointOnLeft + 1;
transform.position = Vector3.MoveTowards(transform.position, PatrolPoints[NextPointOnLeft].position, moveRate * Time.deltaTime);
}
if (HorizontalAxis > 0)
{
if (transform.position == PatrolPoints[NextPointOnRight].position)
{
if(NextPointOnRight < PatrolPoints.Length-1)
{
++NextPointOnRight;
}
// EDIT: NextPointOnLeft = NextPointOnRight - 1;
}
// EDIT:
NextPointOnLeft = NextPointOnRight - 1;
transform.position = Vector3.MoveTowards(transform.position, PatrolPoints[NextPointOnRight].position, moveRate * Time.deltaTime);
}
if you need a different solution, please specify what you need

How to solve IndexOutOfRangeException?

I have some error related to IndexOutOfRangeException. The number of errors is over 200 and the error comes up when I play the game.
Error shown like this:
IndexOutOfRangeException: Array index is out of range. GameController.OnGUI () (at Assets/MicroRacers Assets/Scripts/GameController.js:67)
Here is the code of my game controller.
var CheckPoints:Transform[];
var LapsToWin:int = 5;
var PointsPerPlace:Array = new Array(8,4,2,1);
var CountDownToStart:float = 5;
var Items:Transform[];
var ItemCopy:Object;
var SpawnTime:Vector2 = Vector2(5,10);
private var SpawnTimeCount:float = 0;
var MaximumItems:int = 5;
private var ItemCount:int = 0;
private var FinishPlace:int = 0;
private var Players:int = 1;
var RaceEndDelay:float = 5;
//~ function Awake()
//~ {
//~ Application.targetFrameRate = 30;
//~ }
function Start()
{
SpawnTimeCount = Random.Range(SpawnTime.x, SpawnTime.y);
}
function Update ()
{
if ( SpawnTimeCount > 0 )
{
SpawnTimeCount -= Time.deltaTime;
}
else if ( ItemCount < MaximumItems )
{
SpawnTimeCount = Random.Range(SpawnTime.x, SpawnTime.y);
ItemCount++;
ItemCopy = Instantiate(Items[Mathf.Floor(Random.Range(0, Items.length - 1))], CheckPoints[Mathf.Floor(Random.Range(0, CheckPoints.length - 1))].position + Vector3.up * 0.3, Quaternion.identity);
ItemCopy.transform.Translate(Vector3.right * Random.Range(-3,3), Space.Self);
}
if ( Players == 0 )
{
RaceEndDelay -= Time.deltaTime;
if ( RaceEndDelay <= 0 )
{
Application.LoadLevel("end");
}
}
}
//This script displays the HUD, with current weapon, ammo left, Health, Shield, and score
var GUIskin:GUISkin; //The skin gui we'll use
var CountdownTextures:Texture2D[];
private var CountdownTextureIndex:int = 0;
function OnGUI()
{
GUI.skin = GUIskin; //The skin gui we'll use
if ( CountDownToStart > Time.timeSinceLevelLoad )
{
GUI.DrawTexture(Rect( (Screen.width - CountdownTextures[Mathf.Round(Time.timeSinceLevelLoad)].width) * 0.5, (Screen.height - CountdownTextures[Mathf.Round(Time.timeSinceLevelLoad)].height) * 0.5, CountdownTextures[Mathf.Round(Time.timeSinceLevelLoad)].width, CountdownTextures[Mathf.Round(Time.timeSinceLevelLoad)].height), CountdownTextures[Mathf.Round(Time.timeSinceLevelLoad)]); //Draw the HUD texture
}
}
Mathf.Round(Time.timeSinceLevelLoad) is just bigger CountdownTextures array size.
Maybe try adding this:
Debug.Log("Index: "+Mathf.Round(Time.timeSinceLevelLoad)+", Array size: "+ CountdownTextures.Length);
And you should see that index is bigger than size.
You can also add kind of safety check inside if, something like this:
if(Mathf.Round(Time.timeSinceLevelLoad) < CountdownTextures.Length && <your second statement>)