- Home /
How to set a realistic wall physic
Hello! I am developing a game where the user has to move a ball through a maze by tilting the smartphone. When I tested the game, I noticed that the ball has a strange behavior when hitting the wall. This is the code attached to my sphere:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent (typeof (Rigidbody))]
public class PlayerController : MonoBehaviour {
float speed = 100.0f;
public Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
Vector3 acc = Input.acceleration;
camera.transform.rotation.eulerAngles.y, 0) * acc;
rb.AddForce(acc.x * speed, 0.0f, acc.y * speed);
}
void OnCollisionEnter (Collision collision) {
if (collision.gameObject.CompareTag("Wall")) {
rb.velocity = rb.velocity/2;
rb.AddForce (Vector3.Reflect (rb.velocity, collision.contacts [0].normal) * rb.velocity.magnitude, ForceMode.Impulse);
}
}
}
I tried many different solutions, but I would like to avoid to use the IsKinematic option enabled on my wall, since the sphere, colliding with the wall, stops to move in an unrealistic way.
The ball starts spinning faster and suddendly it jumps away from the maze
No matter what the actual problem is, but I'd make the level static and tiltt the camera adding appropriate forces to the ball.
Less error prone, more performant.
So, you're suggesting to add a sort of physic to wall/ball reaction through a script attached to the camera?
Your answer
Follow this Question
Related Questions
Fungus C# ExecuteBlock() won't execute when triggered by collision or trigger enter…! 0 Answers
Collider collides, triggers OnCollisionEnter, then bounces, before destroying itself 2 Answers
Calculating Sphere vs. Plane Friction 1 Answer
Sphere Friction Not Working Correctly When Bouncing Hard 0 Answers
How To Improve my Rigid Body Sphere Character Controller 2 Answers