Unity 2D rhythm game prefab object spawn slower after build - unity3d

im totally newbie in unity, my brother helped me in coding and now we stuck on a problem. we're making a 2D mobile (android) rhythm game based on our folk songs. the problem is like in the title.
game demo: https://youtu.be/UJl461f8QZc
its just miliseconds late but its so uncomfortable, we only adjust the time just like the songs original beats per minute.
this is the code we write to define the time so we can write the time (ms) in the editor
/* void Update()
{
transform.Translate(0 , -speed * Time.deltaTime , 0);
timer += Time.deltaTime;
}*/
private void FixedUpdate()
{
rb.velocity = -transform.up * speed * 45f * Time.fixedDeltaTime;
timer += Time.deltaTime;
}
as you can see, the fixedDeltaTime doesn't work either
and this is the code to spawn the tiles :
public void Muncul()
{
int RG = Random.Range(0 , graphics.Length);
int SD = Random.Range(0 , spawner.Count);
GameObject t = Instantiate(tiles , spawner[SD].position , spawner[SD].rotation);
t.GetComponent<SpriteRenderer>().sprite = graphics[RG];
}
and to create how many tiles to spawn, we use code to record the song we played and then it detects the beats in song to spawn the tile per beat.
i appreciate any help, and sorry for my bad english

Related

Player moves in Update() but not FixedUpdate() in Unity

I made a car controller in unity to drive around. I've read FixedUpdate is better to use for physical objects instead of Update, but when I switch to Fixed update my car no longer drives. Does anyone know why this might be? Thank so much you for any help you an provide!
public float speed;
public float turnSpeed;
private RigidBody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
Move();
Turn();
}
void Move(){
// moves the car
if (Input.GetKey(KeyCode.W)){
rb.AddRelativeForce(new Vector3(Vector3.forward.x, 0, Vector3.forward.z) * speed);
}
else if (Input.GetKey(KeyCode.S)){
rb.AddRelativeForce(new Vector3(Vector3.forward.x, 0, Vector3.forward.z) * -speed);
}
// maintains forward velocity relative to car.
Vector3 localVelocity = transform.InverseTransformDirection(rb.velocity);
localVelocity.x = 0;
rb.velocity = transform.TransformDirection(localVelocity);
}
void Turn(){
// turns the car.
if (Input.GetKey(KeyCode.D)){
rb.AddRelativeTorque(Vector3.up * turnSpeed);
Debug.Log("TURNING CAR");
}
else if (Input.GetKey(KeyCode.A)){
rb.AddRelativeTorque(-Vector3.up * turnSpeed);
Debug.Log("TURNING CAR");
}
Here's the code. Pressing W or S adds a force, pressing A or D adds a torque. When I try turning in FixedUpdate the console will write "TURNING CAR" as it should which shows that it's making past the AddRelativeTorque line, but it still won't turn so I'm not really sure what's going on. Thanks again, I really appreciate any help.
Try increasing the force used.
Fixed update runs less frequently than update (using the default settings, in most scenarios). Since the code is running much less frequently, less force is being accumulated over the same time period.
Consider a game running at 100fps. The default fixed time step is 0.02s (50 frames per second). Since update is running at 100fps, you have twice as much force being applied from update than would be applied from fixed update.
If you make your force value independent of the time since the last update happened, you will not need to worry about this.
For Update use myForceValue * Time.deltaTime.
For FixedUpdate use myForceValue * Time.fixedDeltaTime.

How to find out who shot a projectile

So I am trying to make a multiplayer game with abilities sort of like Overwatch/Paladins. All in all, one ability should be a sort of projectile that moves across the ground and allows that player to teleport to its position at any time while it is alive. I can't find the solution to teleporting only the player that shot it since thus far in my tests, when one player activated their ability, all players would teleport. How can I solve this?
My code:
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
GetComponent<playerController>().heldAbility = "gateCrash";
if (GetComponent<playerController>().heldAbility == "gateCrash")
holding = true;
else
holding = false;
if (holding && Input.GetMouseButtonDown(0))
PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "GateCrashModel"), spawnPos, transform.rotation, 0);
}
This code is attached to the projectile:
public float speed = 10;
PhotonView pv;
private void Awake()
{
pv = transform.GetComponent<PhotonView>();
}
private void FixedUpdate()
{
transform.Translate(transform.forward * speed * Time.fixedDeltaTime);
}
I guess that I should make have something as instatntiation parameter but idk what.
A simple approach would be to add a shotOwner property to each of your projectiles. Every time a projectile is fired, update shotOwner to point to the player object that fired the shot. (This will also let you implement "Player_X killed Player_Y" functionality, among other things.)

What kind of math could I use, to reposition my hands, to my gun in Unity?

Hi so I am making a recoil script for my First Person Shooter. I have the gun working pretty well. Here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunRecoil : MonoBehaviour
{
public Vector3 beforeRecoilRotation, rightBeforeRecoilRotation, leftBeforeRecoilRotation;
//I have a separate script controlling maxRecoilX and recoil
public float maxRecoilX = -0, recoilSpeed = 10, recoil = 0;
[SerializeField]
GameObject leftControl, rightControl, weapon;
public bool once = false;
// Start is called before the first frame update
void Recoiling()
{
//if recoil is present
if (recoil > 0)
{
//make amount of recoil
var maxRecoil = Quaternion.Euler(maxRecoilX, 0, 0);
//move gun's rotation, according to recoil
weapon.transform.localRotation = Quaternion.Slerp(weapon.transform.localRotation, maxRecoil, Time.deltaTime * recoilSpeed);
//subtract recoil by time.
recoil -= Time.deltaTime;
}
else
{
//make sure recoil is now zero
recoil = 0;
//make min recoil, based on starting position
Quaternion minRecoil = Quaternion.Euler(beforeRecoilRotation);
Quaternion minRecoilHandRight = Quaternion.Euler(0, 0, 0);
weapon.transform.localRotation = Quaternion.Slerp(weapon.transform.localRotation, minRecoil, Time.deltaTime * recoilSpeed / 2);
// rightControl.transform.localRotation = Quaternion.Slerp(rightControl.transform.localRotation, minRecoilHandRight, Time.deltaTime * recoilSpeed / 2);
maxRecoilX = 0;
}
}
// Update is called once per frame
void Update()
{
//constantly run Recoiling
Recoiling();
}
}
I also have hands for my gun. I want them to follow the rotation of the gun, to the rotation of my gun.
First I tried just applied the recoil to my hand. The results were close-ish, but not close enough. I used something like
var maxRecoilRight = Quaternion.Euler(maxRecoilX *5f, 0, 0);
rightControl.transform.localRotation = Quaternion.Slerp(weapon.transform.localRotation, maxRecoilRight, Time.deltaTime * recoilSpeed);
Here are the results:
recoil One
I also tried using Fast IK: https://assetstore.unity.com/packages/tools/animation/fast-ik-139972#reviews .
It didn't work, since I am using the parts of the arm in an animation
I also cannot parent the hands to the gun, since I am going to have to replace animations, and it might break my generic rig.
What kind of math could I use, to reposition my hands, to my gun in Unity? Please leave any suggestions for me. Thank you and have a good day/evening.
You could turn off the animation of the hands while the recoil is ongoing, and use an IK solution for that time. Then when the recoil is finished you can turn it back on.

Unity VideoPlayer: Missing Rewind-Method

The idea is to take an 360-degree Video and map it to the sky box using the "Unity Interactive 360 Video Sample" from the asset store scene. The Camera moved through a mall at recording time.
We want the video's spectator to be able to "walk forwards", to "walk backwards", "to stand still" in the mall in VR in Unity at running time.
To do so we want to use the oculus joystick to move the player through the video. That is, scrolling the current video clip time forwards and backwards at runtime using the value from Input.GetAxis("Vertical").
Does work: Moving forwards works like a charm.
Does not work: Moving backwards does not work because playback speed cannot be negative.
This is the current script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
public class OculusInput_VideoControl : MonoBehaviour {
VideoPlayer videoPlayer = null;
// Use this for initialization
void Start () {
videoPlayer = (VideoPlayer)FindObjectOfType(typeof(VideoPlayer));
}
// Update is called once per frame
void Update () {
float joystickPosition_from_minusone_to_one = Input.GetAxis("Vertical");
videoPlayer.playbackSpeed = joystickPosition_from_minusone_to_one;
OVRDebugConsole.print(videoPlayer.playbackSpeed + " ### " + joystickPosition_from_minusone_to_one );
}
}
These are the systems specs:
How do we embed walking backwards?
if (joystickPosition_from_minusone_to_one < 0) videoPlayer.time -= (int) Mathf.Abs(joystickPosition_from_minusone_to_one);
is laggy!
if (joystickPosition_from_minusone_to_one < 0) videoPlayer.frame -= (int) Mathf.Abs(joystickPosition_from_minusone_to_one * 10);
is laggy too!
Negative playback speed is platform dependant.
If it is laggy, then this could be because of your platform.
You can and should however multiply the value you want to substract with Time.deltaTime.
It will probably improve your lag issue, but might not fix it completely.
Also, why Cast your Mathf.Abs to int? Is there a special reason for it?
Casting to int will round your float, which may be undesired behaviour.
Try the following code
if (joystickPosition_from_minusone_to_one < 0)
{
videoPlayer.time -= Mathf.Abs(joystickPosition_from_minusone_to_one) * Time.deltaTime;
}

Unity ball with constant speed

I'm making 2D mobile game and I have a ball, but my ball doesn't have a constant moving speed. What I need to do?
When I build game on my android device, ball have a various speed. In that case, my game is not playable.
I hope that someone can help me. Thanks.
This is on my start function:
void Start () {
GetComponent<Rigidbody2D> ()
.AddForce (new Vector2 (1f, 0.5f)* Time.deltaTime * force);
}
Is it a good practice if I used in code " Application.LoadLevel ("__Scena_0"); " to load existing scene when I lose " life" ? Problem happens when I lost " life " and try playing game in second chance.
My update function is about OnTriggerEnter2D when my ball hit trigger objects.
EDIT 23.12.2015. : problem solve with adding physics material (fiction 0) and adding to script:
using UnityEngine.SceneManagement;
...
SceneManager.LoadScene ("MainScene");
The problem is the calculation of the force vector:
new Vector2 (1f, 0.5f) * Time.deltaTime * force
You are using Time.deltaTime as a factor, which means that the applied force is not constant, but depending on the deltaTime, which is the duration of the last frame. This explains why it changes randomly.
I don't think Time.deltaTime is what you want here, try just removing the factor and adjusting the "force" factor accordingly. You should now have a constant force applied, independent from the platform you play on.
Create a new Physics Material, set friction zero and add it to your object. If your object has no friction, its speed cannot be decreased. Then, use AddForce() on Rigidbody2D to speed up.
Assuming that your ball and colliding walls have bouncy materials with no friction. Try
public float _ballSpeed = 2.5f;
Rigidbody2D _rb;
void Start()
{
_rb = GetComponent<Rigidbody2D>();
_rb.velocity = Vector2.one * _ballSpeed;
}
void Update()
{
}
From this you can control ball speed through _ballSpeed
//========== OR ==========//
If you want to retain speed even after ball destruction.
Retain _ballSpeed in any global static variable.
Suppose you have a class named Globals, declare a variable like,
public class Globals
{
public static float BALL_SPEED = 2.5f;
}
Now in Ball Script
Rigidbody2D _rb;
void Start()
{
_rb = GetComponent<Rigidbody2D>();
_rb.velocity = Vector2.one * Globals.BALL_SPEED;
}
void Update()
{
}