For whatever reason my character is moving faster at higher framerates even though I execute Time.deltaTime. Sorry for it being so long though.
To explain this further, upon building my program, it will move a lot fast if you set the settings to “Fast” rather than “Fantastic”. The higher the graphical setting the slow it moves, “Fantastic” will make it move as it’s supposed to move, leaving me to the conclusion that it’s due to higher framerates.
I tried looking for ways to make it visible upon clicking, but I couldn’t find any kind of code for that. Could someone write a reply explaining how to insert that? Thanks
using UnityEngine;
using System.Collections;
public class AvatarMovement : MonoBehaviour {
public Texture leftRun01; //First texture in left running animation.
public Texture leftRun02; //Second texture in left running animaation.
public Texture leftRun03; //Third texture in left running animation.
public Texture leftRun04; //Fourth texture in left running animation.
public Texture rightRun01; //First texture in right running animation.
public Texture rightRun02; //Second texture in right running animation.
public Texture rightRun03; //Third texture in right running animation.
public Texture rightRun04; //Fourth texture in right running animation.
public float AnimationInterval; //Value in seconds in between each texture when running animations.
public float verticalRaycastLength; //Value of a default vertical raycast.
public float horizontalRaycastLength; //Value of a default horizontal raycast.
public float JumpSpeed; //Value of the height of the players jump.
public float WalkingSpeed; //Walking speed of the player.
public bool AssistWalk = true; //Allow walking assist? Refer to WalkRight and WalkLeft methods.
private bool runRightAnimation = false; //Run right animation?
private bool runLeftAnimation = true; //Run left animation?
private bool isJumping = false; //Is the player currently in the air?
private bool AllowJumpMovement; //Can the player move in the air?
public bool Walk = false; //Forces movement.
private Vector3 StartPosition; //Coordinates of where the player spawned.
void Start()
{
StartPosition = transform.position; //Saves coordinates of where the player spawned.
Debug.Log (StartPosition);
} //Run when gameobject with script is instantiated.
void Update()
{
ForceWalkTrigger forceWalkTrigger = GameObject.Find("ForceWalkTrigger").GetComponent<ForceWalkTrigger>();
if (Input.GetKeyDown(KeyCode.R)) //Respawns the player.
{
if (Application.isEditor) //Only run if we're using the editor.
{
transform.position = StartPosition; //Teleports player to starting position.
}
}
if (Input.GetKey (KeyCode.Escape))
{
Application.Quit(); //Quits application.
}
if (Input.GetKey("d"))
{
runRightAnimation = true; //Code that tells the script it's.
runLeftAnimation = false; //supposed to run right animation.
RaycastHit Target;
if (AllowJumpMovement || Walk) //Allows the player to move while not grounded.
{
WalkRight();
}
if (Physics.Raycast(transform.position, -Vector3.right, out Target, horizontalRaycastLength))
{
if (Target.collider.gameObject.name == "Crate") //Changes walking speed if pushing a crate.
{
WalkingSpeed = 0.3f;
}
else if(Target.collider.gameObject.name == "Collider") //Resets speed if not pushing a crate.
{
WalkingSpeed = 0.5f;
}
if (AssistWalk) //Assists in walking up cliffs.
{
rigidbody.AddForce(Vector3.up * 3000 * JumpSpeed * Time.deltaTime); //This has been implemented to make it less "bumpy" and a lot more smooth.
}
}
else
{
WalkingSpeed = 0.5f; //Resets walking speed.
}
}
else if (Input.GetKey("a"))
{
runRightAnimation = false; //Code that tells the script it's
runLeftAnimation = true; //supposed to run the left animation.
RaycastHit Target;
if (AllowJumpMovement || Walk) //Allows the player to move while not grounded.
{
WalkLeft();
}
if (Physics.Raycast(transform.position, Vector3.right, out Target, horizontalRaycastLength))
{
if (Target.collider.gameObject.name == "Crate") //Changes walking speed if pushing a crate.
{
WalkingSpeed = 0.3f;
}
else if(Target.collider.gameObject.name == "Collider") //Resets speed if not pushing a crate.
{
WalkingSpeed = 0.5f;
}
if (AssistWalk) //Assists in walking up cliffs.
{
rigidbody.AddForce(Vector3.up * 3000 * JumpSpeed * Time.deltaTime); //This has been implemented to make it less "bumpy" and a lot more smooth.
}
}
else
{
WalkingSpeed = 0.5f; //Resets walking speed.
}
}
if (Input.GetKeyDown("w")) //Makes the character jump.
{
Jump ();
}
else if (Input.GetKeyDown("space")) //Also makes the character jumps. Else if statement to make sure you can't doublejump.
{
Jump();
}
if (runLeftAnimation) //True if we're facing left.
{
if (!DontRunLeft) //Added to make sure we don't run the animation several times at once.
{
StartCoroutine(LeftAnimation()); //Starts the running animation. Refer to LeftAnimation method.
}
}
else //Ran if we're not facing left.
{
if (!DontRunRight) //Added to make sure we don't run the animation several times at once.
{
StartCoroutine(RightAnimation()); //Starts the running animation. Refer to RightAnimation method.
}
}
} //Run every frame.
void WalkRight()
{
if (AssistWalk)
{
transform.position -= new Vector3(WalkingSpeed / 10, WalkingSpeed / -10, 0 * Time.deltaTime); //Disallows the player to "glitch" while walking up hills.
}
else
{
transform.position -= new Vector3(WalkingSpeed / 10, 0, 0 * Time.deltaTime);
}
} //Code for walking right.
void WalkLeft()
{
if (AssistWalk)
{
transform.position += new Vector3(WalkingSpeed / 10, WalkingSpeed / 10, 0 * Time.deltaTime); //Disallows the player to "glitch" while walking up hills.
}
else
{
transform.position += new Vector3(WalkingSpeed / 10, 0, 0 * Time.deltaTime);
}
} //Code for walking left.
private RaycastHit Target;
void Jump()
{
if (!isJumping)
{
if (Physics.Raycast(transform.position, -Vector3.up, out Target, verticalRaycastLength))
{
rigidbody.AddForce(Vector3.up * 100000 * JumpSpeed * Time.deltaTime);
}
}
} //Code for jumping.
void OnCollisionStay()
{
isJumping = false;
if (Physics.Raycast(transform.position, -Vector3.up, verticalRaycastLength))
{
AllowJumpMovement = true;
}
else
{
AllowJumpMovement = false;
}
} //Run every frame collider is touching another collider.
void OnCollisionExit()
{
isJumping = true;
} //Run when this collider stops touching another collider.
private bool DontRunLeft = false; //Variable to determine whether we can run the left animation loop or not. Used to make sure we don't run several loops at once.
IEnumerator LeftAnimation()
{
while (true) //This will allow us to break out of the animation, in case we suddenly change direction.
{
DontRunLeft = true; //This will block the script from running the same animation over and over again at the same time.
if (!runLeftAnimation) //If we suddenly changed direction, this will run code to snap out of the animation loop.
{
DontRunLeft = false; //We can now enter the animation loop again.
break; //Breaks out of the animation loop.
}
this.renderer.material.mainTexture = leftRun02; //Changes texture to the second texture in our loop.
yield return new WaitForSeconds(AnimationInterval); //Yields the entire loop for X amounts of seconds. Check inspector for details.
if (!runLeftAnimation) //If we suddenly changed direction, this will run code to snap out of the animation loop.
{
DontRunLeft = false; //We can now enter the animation loop again.
break; //Breaks out of the animation loop.
}
this.renderer.material.mainTexture = leftRun03; //Changes texture to the third texture in our loop.
yield return new WaitForSeconds(AnimationInterval); //Yields the entire loop for X amounts of seconds. Check inspector for details.
if (!runLeftAnimation) //If we suddenly changed direction, this will run code to snap out of the animation loop.
{
DontRunLeft = false; //We can now enter the animation loop again.
break; //Breaks out of the animation loop.
}
this.renderer.material.mainTexture = leftRun04; //Changes texture to the fourth texture in our loop.
yield return new WaitForSeconds(AnimationInterval); //Yields the entire loop for X amounts of seconds. Check inspector for details.
if (!runLeftAnimation) //If we suddenly changed direction, this will run code to snap out of the animation loop.
{
DontRunLeft = false; //We can now enter the animation loop again.
break; //Breaks out of the animation loop.
}
this.renderer.material.mainTexture = leftRun01; //Changes the texture back to the first one in our loop.
yield return new WaitForSeconds(AnimationInterval); //Yields the entire loop for X amounts of seconds. Check inspector for details.
DontRunLeft = false; //We can now enter the animation loop again.
break; //Breaks out of the animation loop.
}
} //Code for left running animation.
private bool DontRunRight = false; //Variable to determine whether we can run the right animation loop or not. Used to make sure we don't run several loops at once.
IEnumerator RightAnimation()
{
while (true) //This will allow us to break out of the animation, in case we suddenly change direction.
{
DontRunRight = true; //This will block the script from running the same animation over and over again at the same time.
if (!runRightAnimation) //If we suddenly changed direction, this will run code to snap out of the animation loop.
{
DontRunRight = false; //We can now enter the animation loop again.
break; //Breaks out of the animation loop.
}
this.renderer.material.mainTexture = rightRun02; //Changes texture to the second texture in our loop.
yield return new WaitForSeconds(AnimationInterval); //Yields the entire loop for X amounts of seconds. Check inspector for details.
if (!runRightAnimation) //If we suddenly changed direction, this will run code to snap out of the animation loop.
{
DontRunRight = false; //We can now enter the animation loop again.
break; //Breaks out of the animation loop.
}
this.renderer.material.mainTexture = rightRun03; //Changes texture to the third texture in our loop.
yield return new WaitForSeconds(AnimationInterval); //Yields the entire loop for X amounts of seconds. Check inspector for details.
if (!runRightAnimation) //If we suddenly changed direction, this will run code to snap out of the animation loop.
{
DontRunRight = false; //We can now enter the animation loop again.
break; //Breaks out of the animation loop.
}
this.renderer.material.mainTexture = rightRun04; //Changes texture to the fourth texture in our loop.
yield return new WaitForSeconds(AnimationInterval); //Yields the entire loop for X amounts of seconds. Check inspector for details.
if (!runRightAnimation)
{
DontRunRight = false; //We can now enter the animation loop again.
break; //Breaks out of the animation loop.
}
this.renderer.material.mainTexture = rightRun01; //Changes the texture back to the first one in our loop.
yield return new WaitForSeconds(AnimationInterval); //Yields the entire loop for X amounts of seconds. Check inspector for details.
DontRunRight = false; //We can now enter the animation loop again.
break; //Breaks out of the animation loop.
}
} //Code for right running animation.
}