Unity type racer - unity3d

I'm designing a typeracer game in unity where the player is in an athletics 100m sprint and the faster they type, the faster the character runs.
I'm just wondering should I be aiming to get an animation that completes between every correct letter entered or working out an algorithm that speeds up, slows down and pauses the animation depending on whether the letter is correct.
Has anyone had any experience with something like this before?
Also, being quite new to unity i'm just using the standard assets with Ethan as my model, is this the right thing to be doing here?

Original Thoughts
You could have it so that every correct character type speeds up the animation of the character and slowly ticks down per millisecond that passes (i.e slowing down if you aren't typing). Then when the user enters a wrong character the animation gets increasingly slower (1/10 of the previous time every time (?)).
Solution
In Unity, working with timing is a little difficult, my class and I had issues with it this year. The best solution we found is working within the FixedUpdate loop itself, as this is run on a more concise time frame than just Update.
Example
For my solution (and what we all ended up doing) was to update the time in FixedUpdate and use it in Update
void FixedUpdate() {
if (timer >= 12f) stopped = true;
if (!stopped) timerDT = updateTimer(Time.deltaTime);
}
If the timer variable is greater than 12 (seconds) then stop movement.
If not stopped, continue updating and adding to the timer, as well as giving the frame time back to timerDT
void Update() {
this.transform.translate(velocity * timerDT);
}
This will run and update the game object attached based on its velocity and the time frame given in FixedUpdate
For you, I would have the script save the animation controller as a variable in the script:
Controller animation = {animation controller};
Note: Don't remember what needs to go here, but I'm pretty sure it's the controller
Then you can change the animation like so:
void Update() {
update_animation(timerDT, anim_speed);
}
void FixedUpdate() {
timerDT = updateTimer(Time.deltaTime);
if (timerDT - oldDT > 0.1) {
oldDT = timerDT;
anim_speed = anim_speed / 0.1; // for decreasing speed
}
}
void update_animation(float deltatime, float speed) {
animation["run"].speed = anim_speed;
}

Related

Disable a joystick Unity 3D

edit
After messing with this for a while, what it seems like is that, something was changed in unity. I tried using a bool instead and it works much better. Although I currently have to hold down the gamepad button due to the logic, some progress was made and at least he's going straight out of rolling into running with no problem.
I'm trying to disable the joystick so that after my player does a roll, he will go into an idle animation for a short period and then go back to the blend tree. The blend tree contains idle walk and run animations. At the moment he will roll, transition with "Has Exit Time" into an idle animation (but is still able to slide around if you have the joystick held down) and then transition with "Has Exit Time" to the blend tree.
https://www.youtube.com/watch?v=RVWh-YAQElQ
it's from this tutorial, but I don't know. He never mentions this problem in the video and no one in the comments sections mentions says anything. But basically if you just transition from the roll animation to the blend tree and keep the joystick held down, the animation skips once the roll animation is over. Instead of him just stopping for a second and then allowing the player to move again.
public void OnEnable()
{
if (inputActions == null)
{
inputActions = new PlayerControls();
inputActions.PlayerMovement.Movement.performed += inputActions => movementInput = inputActions.ReadValue<Vector2>();
inputActions.PlayerMovement.Camera.performed += i => cameraInput = i.ReadValue<Vector2>();
if (!animator.GetCurrentAnimatorStateInfo(0).IsName("Idle"))
{
inputActions.PlayerMovement.Movement.performed += inputActions => movementInput = inputActions.ReadValue<Vector2>();
}
}
inputActions.Enable();
}
I've tried disabling the joystick with the code at the bottom, but it just ignores it for some reason.

How to get info about completion of one of my animations in Unity?

I have monster named Fungant in my 2D platform game.
It can hide as mushroom and rise to his normal form.
I try to handle it in code, but I don't know how to get information about finished animation (without it, animation of rising and hiding always was skipped).
I think the point there is to get info about complete of the one of two animations - Rise and Hide.
There is current code:
if (
fungantAnimator.GetCurrentAnimatorStateInfo(0).IsName("Fungant Rise")
)
{
fungantAnimator.SetBool("isRising", false);
isRisen = true;
fungantRigidbody.velocity = new Vector2(
walkSpeed * Mathf.Sign(
player.transform.position.x - transform.position.x),
fungantRigidbody.velocity.y
);
}
if (fungantAnimator.GetCurrentAnimatorStateInfo(0).IsName("Fungant Hide"))
{
fungantAnimator.SetBool("isHiding", false);
isRisen = false;
}
I try this two ways:
StateMachineBehaviour
I would like to get StateMachineBehaviour, but how to get it?
No idea how to process this further.
AnimationEvents
Tried to do with animation event but every tutorial have list of functions to choose (looks easy), but in my Unity I can write Function, Float, Int, String or select object (what I should do?).
I decided to write test() function with Debug only inside, and create AnimationEvents with written "test()" in function field, but nothing happens.
Same as above, no more ideas how to process this further.
I personally would use animation events for this. Just add an event to the animation and then the event plays a function after the animation transition is finished.
For more information on how to use animation events you can click here.
This post may also help you to add an event when the animation is finished.
I hope this answer helps you to solve this problem.

increasing player's speed when entering booster and a speed upgrade system

I'm building my first game in Unity using my limited knowledge, youtube tutorials and troubleshooting on google and even though i have a couple of idea's on how i could do this i can't seem to get it to work.
For one of the larger levels i need speedups that boost the players speed when driving over them. This is seen in a lot of racing games but to clarify i found this video where this type of boosters is used https://www.youtube.com/watch?v=GQ1FLPBw1FE.
I've tried making a script that would remove my current movementscript and replace it with a faster one, i've also tried making a script that would detect collision with the player(tag) and add force to it. Both didn't work i've also been thinking about using timescale but i don't want the rest of the world or scorecounters etc to speed up, just the player.
this is the basic script i am using for playermovement.
#pragma strict
function Start () {
}
var forwardSpeed:float = 35;
var turnSpeed:float = 1;
function FixedUpdate () {
var forwardMoveAmount=Input.GetAxis("Vertical")*forwardSpeed;
var turnAmount=Input.GetAxis("Horizontal")*turnSpeed;
transform.Rotate(0,turnAmount,0);
rigidbody.AddRelativeForce(0,0,-forwardMoveAmount);
}
and since the player's vehicle is customizable i also want to make an upgrade that permanently increases the players base movement speed by a certain amount. i would do this by putting in an if(upgrade is active)
increase its speed by x;
this would have to stack with the booster
if you guys have any idea's on how i could do either or both of these things please share and thanks a lot in advance!
Instead of replacing the movement script with a faster one, add in an extra variable to the players movement.
var forwardMoveAmount=Input.GetAxis("Vertical")*forwardSpeed * boostSpeed;
Where boostSpeed regularly equals 1, but when the player collides with a speed boost push the value up to something higher like 2, then after a short while it gets reset to 1. As for the player speed upgrade, follow the same logic, except permanently, so it would look something like this:
var forwardMoveAmount=Input.GetAxis("Vertical")*forwardSpeed * boostSpeed * upgradeSpeed;

Unity. Cant play animation in animator

i'm making a game in 2d and whenever i create an animation sprite, an animator controller is created automatically, i dont know why, i once tried it but i thought it wouldnt be necesary for animations i just want to make in 2d.. so after trying many times to play an animation without the animator (because unity says i have to set the animation legacy, which i dont know where), i gave it a try to play animations in the animator.. i searched through the script reference and wrote the code just like this:
#pragma strict
var velocity : float = 8;
function Update ()
{
var movement = Input.GetAxis("Horizontal") * velocity;
transform.Translate(Vector2.right * movement * Time.deltaTime);
if(Input.anyKey)
Animator.Play("move");
}
the error that unity says is : Assets/PlayerControl.js(12,26): BCE0020: An instance of type 'UnityEngine.Animator' is required to access non static member 'Play'.
so i tried to do this:
function Start ()
{
//var anim = GetComponent("Animator");
}
function Update ()
{
var movement = Input.GetAxis("Horizontal") * velocity;
transform.Translate(Vector2.right * movement * Time.deltaTime);
if(Input.anyKey)
anim.Play("move");
}
and another error occurs: Assets/PlayerControl.js(17,17): BCE0005: Unknown identifier: 'anim'.
i just want to play one simple animation that will just change the sprites. i know how to play animations without the animator.. so please tell me what to do with it.. how to stop unity auto creating animator controller or set the animation legacy, or how to fix this problem with the animator script.
Update:
i removed the code code just to make other stuff while i search solution for this but now it seems that animator is running this animation no matter what.. i made another state on it as idle, but then it goes to the animation i created.. i really dont understand this.
You can check Unity3D Animation mechanism with example http://docs.unity3d.com/Manual/Animator.html
Unity doesn't give us static method for animation play. See above.
http://docs.unity3d.com/ScriptReference/Animator.html
Try to play of animation
GetComponent("Animator").Play("move", -1, 0.0f);
Check the script reference of official site.
http://docs.unity3d.com/ScriptReference/Animator.Play.html

How to set lapTimer in multiplayer games in unity3d?

As I'm new to the coding and unity3d I'm finding tough to set the lap timer for my racing game which is the multiplayer game.
Well in terms of just code.
Time.time
Will give you the time (duh)
So you'd do something like the following
function Awake() {
startTime = Time.time;
}
function OnGUI () {
var currentLap = Time.time - startTime;
}
currentLap is the current time since the script started. So you could run the code in the "Awake()" method when the race starts. Then on screen display what the current lap time is.
Found a nice example from the Unity Developer Network that might be exactly what your looking for here