32 lines
971 B
C#
32 lines
971 B
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class PlayerController : MonoBehaviour
|
|
{
|
|
public float moveSpeed = 1f;
|
|
public float bounceForce = 5f;
|
|
public float separationForce = 10f; // Force to separate overlapping objects
|
|
public PlayerInput playerInput;
|
|
void Update()
|
|
{
|
|
Vector3 targetPos = playerInput.worldSpace;
|
|
targetPos.z=0;
|
|
transform.position = Vector3.Lerp(transform.position, targetPos, moveSpeed * Time.deltaTime);
|
|
}
|
|
|
|
void OnCollisionEnter2D(Collision2D collision)
|
|
{
|
|
// Apply bounce force to the collided object
|
|
if(collision.gameObject.TryGetComponent<Rigidbody2D>(out Rigidbody2D rb))
|
|
{
|
|
if(rb.velocity.sqrMagnitude < 0.2f){
|
|
rb.AddForce((-collision.contacts[0].point + (Vector2)transform.position).normalized * bounceForce);
|
|
Debug.Log("Player force");
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|