- Home /
Problem is not reproducible or outdated
animation not playing on collision2d
Hello, I had this code working, and then unity crashed, and even though everything had been saved multiple times before that, it got rid of all my work, luckily I still had the code, so I rebuilt the project and now this code won't work. I tried everything and still can't figure it out, it has to do with the collision 2d function which is at the very end, but I will include the whole code for reference: Thank you soo much, any help is appreciated!
private var ray: Ray;
private var rayCastHit: RaycastHit;
var animator: Animator;
function Start () {
animator = GetComponent("Animator");
}
function Update(){
transform.position.y += .05;
if(Input.GetMouseButton(0)){
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, rayCastHit)){
if(rayCastHit.collider.gameObject.tag == "leftbottom"){
transform.position.x -= .05;
transform.position.y -= .07;
animator.SetBool("isright", false);
animator.SetBool("isleft", false);
}
if(rayCastHit.collider.gameObject.tag == "leftmiddle"){
transform.position.x -= .05;
transform.position.y += .02;
animator.SetBool("isright", false);
animator.SetBool("isleft", true);
}
if(rayCastHit.collider.gameObject.tag == "lefttop"){
transform.position.x -= .05;
transform.position.y += .03;
animator.SetBool("isright", false);
animator.SetBool("isleft", true);
}
if(rayCastHit.collider.gameObject.tag == "rightbottom"){
transform.position.x += .05;
transform.position.y -= .07;
animator.SetBool("isright", false);
animator.SetBool("isleft", false);
}
if(rayCastHit.collider.gameObject.tag == "rightmiddle"){
transform.position.x += .05;
transform.position.y += .02;
animator.SetBool("isleft", false);
animator.SetBool("isright", true);
}
if(rayCastHit.collider.gameObject.tag == "righttop"){
transform.position.x += .05;
transform.position.y += .03;
animator.SetBool("isleft", false);
animator.SetBool("isright", true);
}
}
}
}
function OnCollisionEnter2D(coll: Collision2D) {
if (coll.gameObject.tag == "asteroid"){
numOfHits += 1.0;
animator.SetFloat("hits", numOfHits);
}
}
Would you please describe exactly what result the code is supposed to produce and what result it currently produces?
Yes it is supposed to play a "hits" animation which is the damaged ship, and now , even though i see the float increase, the animation doesnt transition from the movement to the damaged animation.
This is most likely a problem with your Animator setup. If you could post a picture of it, that'd be really helpful in identifying your problem.
Check your animator controller for any missing thing. I got same problem with OnCollisionEnter2D(). If you mark rigidbody to is$$anonymous$$inematic sometimes function doesn't get called.
Honestly, the problem got solved by me deleting the transitions as they were from clip to clip, and recreating them the exact same. I have no clue what the problem was, probably just one of those Unity quirks, thanks a ton!
Follow this Question
Related Questions
Object passes through collider even when isTrigger is turned off. 2 Answers
OnTriggerEnter2D working, OnColliderEnter2D not working 1 Answer
2D Isometric Rigid Body Collision Resolution (collide and side) Not working 0 Answers
OnCollisionEnter2D doesn't work every time. 1 Answer
Object passes through collider even when isTrigger is turned off. 0 Answers