the infinite moving ball(player) goes up by hitting walls and reflects at 45 degrees. but mostly the ball goes along to the wall and thus the velocity changes. i dont want to change velocity and dont want to stuck to wall.
to prevent this issue:
I have attached to physic material to ball with 0 friction and 1 bounciness.
i have also attached the wall 0 friction and 0 bounciness.
but it s not the solution. can anybody help me?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerMovement : MonoBehaviour
{
public GameObject gameOverPage;
private Rigidbody rb;
Vector3 lastVelocity;
public Vector3 initialVelocity;
public float minVelocity=1f;
void Awake()
{
rb = GetComponent<Rigidbody>();
}
void Start()
{
rb.velocity = initialVelocity;
rb.AddForce(initialVelocity);
}
void Update()
{
lastVelocity = rb.velocity;
if (Input.GetMouseButtonDown(0))
{
Bounce(Vector3.right);
}
Debug.Log(rb.velocity);
}
void OnCollisionEnter(Collision coll)
{
if (coll.gameObject.tag == "Wall")
{
Bounce(coll.contacts[0].normal);
}
if (coll.gameObject.tag == "Obstacle")
{
rb.velocity = Vector3.zero;
gameOverPage.SetActive(true);
}
}
void Bounce(Vector3 CollNormal)
{
var speed = lastVelocity.magnitude;
var direction = Vector3.Reflect(lastVelocity.normalized, CollNormal);
rb.velocity = direction * Mathf.Max(speed, minVelocity);
getNormal = new Vector3(-rb.velocity.x, 0, 0);
}
public void Replay()
{
SceneManager.LoadScene("SampleScene");
}
}