- Home /
Question by
LucasPThiago · Oct 15, 2020 at 11:33 PM ·
colliderhitradius
Unity - Object hiting soon the other object
I made a replica project to the game Knife Hit. And I'm having trouble with the following question, is my knife hitting the target before it hits it, or the target is detecting the hit before the knife even arrives. What causes the knife to fly around the target, where it should be stuck on the target. As shown in the image below. Consequently, the spawn of the knives that stay on the target, which is to hinder the gameplay, happen the same, are born and are flying around the target.
The code i use is this:
void Update()
{
if (shoot)
{
lastPosition = transform.position;
transform.position += Vector3.up * speed * Time.deltaTime;
RaycastHit2D hit = Physics2D.Linecast(lastPosition, transform.position);
if(hit.collider != null)
{
shoot = false;
if (hit.transform.tag == "Knife")
{
Level.Instance.HitWrong();
rigidbody.bodyType = RigidbodyType2D.Dynamic;
rigidbody.AddTorque(10, ForceMode2D.Impulse);
}
else
{
transform.position = new Vector3(transform.position.x, hit.point.y, transform.position.y);
transform.parent = hit.transform;
collider.enabled = true;
Level.Instance.HitSucced(rigidbody);
}
}
}
}
knife-hitin-soon.png
(283.1 kB)
Comment
Your answer
Follow this Question
Related Questions
Hit collider problem 1 Answer
CapsuleCollider : How to get the info which part is hit? 2 Answers
OverlapSphere picking colliders that are not inside 0 Answers
Compare hit.collider.name 1 Answer
if hit something 3 Answers