How do I slow down player movement after OnTriggerEnter
Hi, I have two script attached to my player. My first script is a AddForce script that pushes my player after OnTriggerEnter2D is called, I attached it to a tagged object . My second script is my player movement script that allows my to control my player by mouse click, the script also has a function where IEnumerator OnTriggerEnter2D is called after collision that freezes my player after 0.1 seconds. It works but the only problem is, is that after it collider with a object my player is pushed but it looks like its being teleported. How do I slow down the movement of my player after it collides with my object so that it does't looks like its being teleported.
Here is my player movement script:
using UnityEngine;
using System.Collections;
public class Playermovement : MonoBehaviour {
public float speed = 15f;
private Vector3 target;
void Start () {
target = transform.position;
}
void Update () {
if (Input.GetMouseButtonDown (0)) {
target = Camera.main.ScreenToWorldPoint (Input.mousePosition);
target.z = transform.position.z;
}
transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
}
IEnumerator OnTriggerEnter2D(Collider2D other) {
if (other.tag == "Bouncy object")
playerMovementRef.enabled = true;
yield return new WaitForSeconds(0.1f);
playerMovementRef.enabled = false;
yield return new WaitForSeconds(0.1f);
playerMovementRef.enabled = false;
}
}
And here is my AddForce script attached to my object:
using UnityEngine;
using System.Collections;
public class Bounce : MonoBehaviour {
void OnTriggerEnter2D(Collider2D other) {
if (other.tag == "Bouncy object")
GetComponent<Rigidbody2D> ().AddForce (transform.right * 280, ForceMode2D.Impulse);
}
void OnTriggerExit2D(Collider2D other)
{
if (other.tag == "Bouncy object")
GetComponent<Rigidbody2D> ().constraints = RigidbodyConstraints2D.FreezeAll;
}
}