I was trying to make my game in unity but I got stuck while making "game over" part. please help!
https://we.tl/t-uzRSX676pW knife prefab is a trigger. when knife touches player, the game should stop but it is not stopping. i am using 2 C# scripts
movement.cs
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class movement : MonoBehaviour { private Animator anim; private SpriteRenderer sr; public float speed = 3f;
private void Start()
{
Time.timeScale = 1f;
}
void Awake()
{
anim = GetComponent<Animator>();
sr = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update()
{
move();
}
void move()
{
float h = Input.GetAxisRaw("Horizontal");
Vector3 temp = transform.position;
if (h > 0)
{
temp.x += speed * Time.deltaTime;
sr.flipX = false;
anim.SetBool("walk", true);
}
else if (h < 0)
{
temp.x += -(speed * Time.deltaTime);
sr.flipX = true;
anim.SetBool("walk", true);
}
else if (h == 0)
{ anim.SetBool("walk", false); }
transform.position = temp;
void OnTriggerEnter2D(Collider2D target)
{
if(target.tag=="knife")
{
Time.timeScale = 0f;
}
}
}
}
spawner.cs
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class spawner : MonoBehaviour {
private float min_x = -11.42f, max_x = 9.8f;
public GameObject knife;
void Start()
{
StartCoroutine(StartSpawning ());
IEnumerator StartSpawning ()
{
yield return new WaitForSeconds(Random.Range(0.5f, 1f));
GameObject k = Instantiate(knife);
float x = Random.Range(min_x, max_x);
k.transform.position = new Vector2(x, transform.position.y);
StartCoroutine(StartSpawning());
}
}
// Update is called once per frame
void Update()
{
}
}
Comment
Your answer
Follow this Question
Related Questions
2d Grid Based Movement, Won't Work 0 Answers
How to align 2d object to terrain? 2 Answers
Objects not colliding 1 Answer
C# delaying command 0 Answers
2D AI movement 0 Answers