How to set lapTimer in multiplayer games in unity3d? - 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

Related

Unity Hand tracking issue on the quest with UI raycasting

I'm working on a Unity project, trying to test the UI interaction on the Quest II with hand tracking and ray casting.
I've set up a very basic scene with all the default assets (OVRCameraRig, UIHelper) and added just a button to test the UI interaction.
This is what my scene looks like:
The issue I have is, when I run the scene, the Ray is rotated 90 degrees, and it's attached to the wrist for some reason. I made a video here to show what I mean :
https://www.youtube.com/watch?v=5f12yfpugB8
It's still interacting with the UI though.
Then after watching some online tutorials, I commented out these lines in the HandInputSelector.cs, which was attached to the UIHelper:
void SetActiveController(OVRInput.Controller c)
{
/*
Transform t;
if(c == OVRInput.Controller.LTouch)
{
t = m_CameraRig.leftHandAnchor;
}
else
{
t = m_CameraRig.rightHandAnchor;
}
m_InputModule.rayTransform = t;
*/
}
and instead added a 2nd script to the UI helper, with these lines only:
public OVRHand hand;
public OVRInputModule inputModule;
private void Start()
{
inputModule.rayTransform = hand.PointerPose;
}
Now the ray is at least attached to the correct position, but it still doesn't rotate properly with the hand movement. I made another video of it here :
Now the ray is at least attached to the correct position, but it still doesn't rotate properly with the hand movement. I made another video of it here :
https://youtu.be/q3d0eG2LwY0
My Unity version is 2021.3.1f1
Can someone please tell me what I'm doing wrong?

Unity type racer

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;
}

Updating the Computer Player action turn by turn in Unity

Currently i am developing the board game. There are four players, one is the actual human player and other remaining three is computer player or simply a bot.
When it is the turn of human player to throw the dice. He can throw and update the score and other thing.
But when it is the computer player turn i want everything to be updated such as throwing the dice updating score slowly so that player can see it.
What i am doing now is normal and after a human player turn, the computer player turn is updated in millisecond.
How can i do it so that it will be updated slowly and player can see it.
This is a very general question and without some code it's difficult to answer but there are two main options you can look into.
1) Convert your AI method into a coroutine and then add some "WaitForSeconds" in between.
IEnumerator YourMethod(){
// Do stuff
yield return new WaitForSeconds(time);
// Do stuff
yield return new WaitForSeconds(time);
// Do stuff
yield return new WaitForSeconds(time);
2) Break your Method into several methods and chain calls to one another at the end with Invoke
void YourMethod(){
// Do stufff
Invoke("YourMethod2", time);
void YourMethod2(){
// Do stuff
Invoke("YourMethod3", time);

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