Unable to stop animation upon collision (2d, animation, rigidbody)
Can someone help me determine what is preventing my player from stopping when colliding with an object? My movement script which has the collision logic built into it notifies me when a collision takes place via debug.log, which tells me triggers are setup correctly. But I simply can't figure out how to stop the animation movement of the player (first off), and IF he will he will continue to move if I click a new location for him to move to.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class touchplayermovement : MonoBehaviour
{
public Animator anim;
public Vector3 target = new Vector3();
public Vector3 direction = new Vector3();
public Vector3 position = new Vector3();
public float speed = 2.0f;
public Rigidbody2D rb;
void Start()
{
anim = gameObject.GetComponent<Animator>();
rb = gameObject.GetComponent<Rigidbody2D>();
}
void Update()
{
position = gameObject.transform.position;
if (Input.GetMouseButtonDown(0))
{
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
target.z = 0;
}
if (target != Vector3.zero && (target - position).magnitude >= .06)
{
direction = (target - position).normalized;
gameObject.transform.position += direction * speed * Time.deltaTime;
anim.SetBool("iswalking", true);
anim.SetFloat("input_x", direction.x);
anim.SetFloat("input_y", direction.y);
// Debug.Log("Walking");
// Debug.Log(transform.position);
}
else
{
direction = Vector3.zero;
anim.SetBool("iswalking", false);
// Debug.Log("Stopped");
// Debug.Log(transform.position);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
Debug.Log("Collision detected");
direction = Vector3.zero;
anim.SetBool("iswalking", false);
rb.isKinematic = false;
}
}
Bumping as I'm now able to detect collision by way of getting my trigger setup and proof through debug.log. I'm however still trying to figure out why my character will not stop upon collision.
Your answer
Follow this Question
Related Questions
How to know when the player object falls between tiles in 2D game to take decesion of gameover 1 Answer
my colliders don't work properly 2 Answers
Collision not working 1 Answer
2D colliders doesnt work with Gravity Scale = 0 0 Answers
How to check if any/multiple 2D sprites neighbour/collide with eachother 0 Answers