39 lines
1.1 KiB
C#
39 lines
1.1 KiB
C#
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
namespace HappyForest.Prototypen
|
||
|
|
{
|
||
|
|
public class PilleGameObjectMover : MonoBehaviour
|
||
|
|
{
|
||
|
|
public Vector3 velocity = Vector3.right;
|
||
|
|
private float speed = 5f;
|
||
|
|
private float raycastDistance = 0.8f;
|
||
|
|
|
||
|
|
void FixedUpdate()
|
||
|
|
{
|
||
|
|
if (velocity.magnitude == 0)
|
||
|
|
velocity = Vector3.right;
|
||
|
|
|
||
|
|
// Raycast nur gegen Wände checken
|
||
|
|
RaycastHit hit;
|
||
|
|
if (Physics.Raycast(transform.position, velocity.normalized, out hit, raycastDistance))
|
||
|
|
{
|
||
|
|
if (hit.collider.CompareTag("Wall"))
|
||
|
|
{
|
||
|
|
velocity = Vector3.Reflect(velocity, hit.normal);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Bewegung
|
||
|
|
transform.position += velocity.normalized * speed * Time.fixedDeltaTime;
|
||
|
|
}
|
||
|
|
|
||
|
|
void OnCollisionEnter(Collision collision)
|
||
|
|
{
|
||
|
|
if (collision.gameObject.CompareTag("Wall"))
|
||
|
|
{
|
||
|
|
velocity = Vector3.Reflect(velocity, collision.relativeVelocity.normalized);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|