- Home /
Delay after setting mainTexture
Gday all,
I am having trouble with a quick project I am trying to finish off in the next few hours... It isnt game breaking but it is a bit annoying.
Webplayer: https://dl.dropboxusercontent.com/u/74159983/Frogpad/Frogpad.html
It is a basic boardgame where you play a frog, after you roll, the frog jumps to the next square. I am simulating a jump just by changing textures but for some reason there is a very noticeable (2-3 second) delay between changing the texture and executing the next part of the code. (Immediately evident if you look at webplayer)
The code I am using is:
currentPlayerScript
function Start () {
renderer.material.mainTexture = normalFrog;
}
function Update () {
}
function Jump(){
renderer.material.mainTexture = jumpingFrog;
}
function Land () {
renderer.material.mainTexture = normalFrog;
}
function Splash () {
renderer.material.mainTexture = splashFrog;
}
function SetPlayer(){
isHuman = true;
}
Key part of movement function
currentPlayerScript.Jump();
var distCovered = (Time.time - startTime) * moveSpeed;
var fracJourney = distCovered - distanceToMove;
while (!done){
distCovered = (Time.time - startTime) * moveSpeed;
fracJourney = distCovered - distanceToMove;
currentPlayer.position = Vector3.Lerp(startLocation, moveLocation, fracJourney);
yield;
if (fracJourney > 0.99){
done = true;
currentPlayerScript.currentSquare = newPos;
}
}
Full function in movement script
function MoveCharacter(i : int){
turnState = 2;
var newPos = currentPlayerScript.currentSquare + i;
if (newPos > boardSquares.Count - 1){
newPos = boardSquares.Count - 1;
}
if (newPos < 0){
newPos = 0;
}
var moveLocation = boardSquares[newPos].transform.position;
moveLocation += currentPlayerScript.safetyOffset;
var distanceToMove = (currentPlayer.position - moveLocation).magnitude;
var startLocation = currentPlayer.position;
var moveSpeed = 1;
var startTime = Time.time;
var done = false;
currentPlayerScript.Jump();
var distCovered = (Time.time - startTime) * moveSpeed;
var fracJourney = distCovered - distanceToMove;
while (!done){
distCovered = (Time.time - startTime) * moveSpeed;
fracJourney = distCovered - distanceToMove;
currentPlayer.position = Vector3.Lerp(startLocation, moveLocation, fracJourney);
yield;
if (fracJourney > 0.99){
done = true;
currentPlayerScript.currentSquare = newPos;
}
}
if (boardSquares[newPos].waterSquare){
currentPlayerScript.Splash();
moveMod = -1;
RunTurn();
}
else {
currentPlayerScript.Land();
moveMod = 1;
EndTurn();
}
}
I have no idea why it is doing this other than maybe a delay with changing the texture, but the actual texture change is instant... it is the delay afterwards which confuses me (I would have thought if there would be a delay, it would be as the texture was changing, not afterwards?)
Any help is greatly appreciated!
FTR you have confusing yield statements I don't understand. Did you copy this code from somewheres or did you write it from scratch?
for simple timers in Unity, usually just use Invoke() or InvokeRepeating(). there are 1000000000s examples onhere, also unityGE$$anonymous$$S.com for beginner articles.
i answer to your question, simply add a few Debug.Log("i am here")l statements throughout your code (in the loops especially) and you will find the problem in one $$anonymous$$ute
hope it helps
The yield commands are there in loops, otherwise it will cause a crash.
All the standard debug stuff I have tried - the thing I dont get is why there is a delay between changing the texture and moving...
Thanks for the comment though.
Answer by whydoidoit · May 09, 2013 at 06:08 AM
Your Lerp is wrong, it looks like it's to do with the calculation of fracJourney which should be:
fracJourney = distCovered/distanceToMove;
Thanks heaps! I have no idea how that happened as I copied from the unity docs... Damn gremlins!
Thankyou very much!!!
Your answer
Follow this Question
Related Questions
Assigning UV Map to model at runtime 0 Answers
Finding a list of properties in Shaders 1 Answer
Are extremely high load times for Procedural materials normal? 0 Answers
GUI movie texture - sound delay 1 Answer
Video texture problem? 2 Answers