- Home /
Weird behavior when projectile hits target and becomes child.
Whenever my projectile hits something, I have a script that allows it to stick to the object it collided with. For some reason, the hit object always moves a little bit towards the direction the projectile came from.
Gif of what is happening here: WeirdBehavior Gif
Here is the method that I'm using in script that is on the projectile:
void TridentStick(Collision2D col)
{
myRigid.velocity = Vector2.zero;
Destroy(gameObject.GetComponent<Rigidbody2D>());
PROJECTILE_DEPTH = Random.Range(minDepth, maxDepth);
Debug.Log(PROJECTILE_DEPTH);
col.transform.Translate(PROJECTILE_DEPTH * -Vector2.right);
transform.parent = col.transform;
Destroy(gameObject.GetComponent<Collider2D>());
isHit = true;
}
Any Idea what could remedy this behavior?
col.transform.Translate(PROJECTILE_DEPTH * -Vector2.right);
move object I collided with by PROJECTILE_DETH at negative right direction.
try removing that line? I don't see why you have it.
Answer by josehzz112 · Jul 23, 2016 at 04:27 AM
Hello, that happens because you are moving the position of the col gameObject.
Here's my solution
void TridentStick(Collision2D col)
{
myRigid.velocity = Vector2.zero;
Destroy(gameObject.GetComponent<Rigidbody2D>());
PROJECTILE_DEPTH = Random.Range(minDepth, maxDepth);
Debug.Log(PROJECTILE_DEPTH);
//col.transform.Translate(PROJECTILE_DEPTH * -Vector2.right);
transform.parent = col.transform;
transform.localPosition = transform.localPosition + (PROJECTILE_DEPTH * Vector3.right);
Destroy(gameObject.GetComponent<Collider2D>());
isHit = true;
}
Wow. I can't believe I didn't notice that. You're solution worked perfectly. Thanks!!!
Your answer
Follow this Question
Related Questions
How can I make a collision sensitive teleporter for a 2D game #c 1 Answer
I am making a 2d game and I want to make it where to can pick up bricks with your mouse 3 Answers
Convert Input.mousePosition to RectTransform pivot position 2 Answers
Inverting Position on One Axis 0 Answers
Cannot change camera position (2D) 1 Answer