0
I have a question for people who have ever played the knife hit mobile game. I’ve been creating a similar game in unity 2D for some time now in terms of mechanics, and I’m having trouble getting a better representation of the physicality when driving a knife into a wood trunk. To further outline the problem I will describe how it more or less looks in the original knife hit game. When driving the knife into the wood trunk, there is a small animation caused by physics in which the wood trunk moves slightly. It seems to me that it may be about the mass and gravity of the objects in question. So does anyone have information what values both the wood trunk and the knife have in the Rigidbody 2D component and with what gravity the knife should move. And I have a second problem namely I don’t know why the knife in my game on the phone when pressed into the screen shoots the knife with a millisecond delay which spoil the gameplay very much I will only add that in the Unity program on the computer the game works fine and there is no delay many people wrote to me that the problem may be related to the code and that the application has a lot of work to do when the user clicks, that is why the knife shoots with a delay, it seems to me that this is not the issue but nevertheless below I add my code from the knife and from the game in general
knife script
using UnityEngine;
public class KnifeScript : MonoBehaviour
{
[SerializeField]
private Vector2 throwForce;
//knife shouldn't be controlled by the player when it's inactive
//(i.e. it already hit the log / another knife)
private bool isActive = true;
//for controlling physics
private Rigidbody2D rb;
//the collider attached to Knife
private PolygonCollider2D knifeCollider;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
knifeCollider = GetComponent<PolygonCollider2D>();
}
[SerializeField] private AudioSource swist;
[SerializeField] private AudioSource Gdzwiek;
void Update()
{
//this method of detecting input also works for touch
if (Input.GetMouseButtonDown(0) && isActive)
{
rb.AddForce(throwForce, ForceMode2D.Impulse);
GameController.Instance.GameUI.DecrementDisplayedKnifeCount();
rb.gravityScale = 1;
//once the knife isn't stationary, we can apply gravity (it will not automatically fall down)
//Decrement number of available knives
swist.Play();
}
}
[SerializeField] private AudioSource Gloss;
private void OnCollisionEnter2D(Collision2D collision)
{
//we don't even want to detect collisions when the knife isn't active
if (!isActive)
return;
//if the knife happens to be active (1st collision), deactivate it
isActive = false;
//collision with a log
if (collision.collider.tag == "Log")
{
GetComponent<ParticleSystem>().Play();
Gdzwiek.Play();
//stop the knife
rb.velocity = new Vector2(0, 0);
//this will automatically inherit rotation of the new parent (log)
rb.bodyType = RigidbodyType2D.Kinematic;
transform.SetParent(collision.collider.transform);
//move the collider away from the blade which is stuck in the log
knifeCollider.offset = new Vector2(knifeCollider.offset.x, -0.1f);
//Spawn another knife
GameController.Instance.OnSuccessfulKnifeHit();
}
//collision with another knife
else if (collision.collider.tag == "Knife")
{
//start rapidly moving downwards
rb.velocity = new Vector2(rb.velocity.x, -2);
//Game Over
GameController.Instance.StartGameOverSequence(false);
Gloss.Play();
}
}
}
game script
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
[RequireComponent(typeof(GameUI))]
public class GameController : MonoBehaviour
{
public static GameController Instance { get; private set; }
[SerializeField]
private int knifeCount;
[Header("Knife Spawning")]
[SerializeField]
private Vector2 knifeSpawnPosition;
[SerializeField]
private GameObject knifeObject;
public GameUI GameUI { get; private set; }
private void Awake()
{
Instance = this;
GameUI = GetComponent<GameUI>();
}
private void Start()
{
GameUI.SetInitialDisplayedKnifeCount(knifeCount);
SpawnKnife();
}
public void OnSuccessfulKnifeHit()
{
if (knifeCount > 0)
{
SpawnKnife();
}
else
{
StartGameOverSequence(true);
}
}
private void SpawnKnife()
{
knifeCount--;
Instantiate(knifeObject, knifeSpawnPosition, Quaternion.identity);
}
public void StartGameOverSequence(bool win)
{
StartCoroutine("GameOverSequenceCoroutine", win);
}
public GameObject log;
[SerializeField] private AudioSource winEffect;
private IEnumerator GameOverSequenceCoroutine(bool win)
{
if (win)
{
winEffect.Play();
Destroy(log.gameObject);
GetComponent<ParticleSystem>().Play();
yield return new WaitForSecondsRealtime(0.3f);
FindObjectOfType<LevelLoader>().LoadNextLevel();
FindObjectOfType<SaveScene>().Load_Saved_Scene();
}
else
{
yield return new WaitForSecondsRealtime(0.5f);
GameUI.ShowRestartButton();
}
}
public void RestartGame()
{
SceneManager.LoadScene("1.kregiel.1");
}
public void MenuGame()
{
SceneManager.LoadScene("0.MENUstartowe");
}
public void ContinueGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex, LoadSceneMode.Single);
}
}
That's a lot of questions bunched up into one, my friend. Is this solved only when all questions are answered? I would create a post for each question if I were you. It'll motivate people to post answers.
– Ady_M