What's the best method for pinball-type bumper physics (3D)?
I'm new to Unity, and having trouble getting a bumper to "kick" my ball away when touched. Things I've tried: --I created a bumper that scales in size when touched by the ball, but this doesn't seem to add any force to --I got as far as adding a new force to the RB of the ball, but I'm unsure how to determine the correct vector direction:
//within Bump.cs, attached to the Bumper GameObject:
float bumperForce = 10;
public GameObject player;
public float thrust = 5;
void OnCollisionEnter(Collision collision) {
Debug.Log ("collision");
if (collision.gameObject.CompareTag ("Player")) {
//this makes the bumper bigger, but doesn't add the force to the ball.
transform.localScale += new Vector3 (0.5f, 0, 0.5f);
rbod = collision.gameObject.GetComponent<Rigidbody> ();
//What should this look like?:
rbod.AddForce (new Vector3(1,1,10) * thrust);
}
}
Answer by AZIIZoom · Feb 11, 2018 at 07:33 PM
Hello, I know that it have been 2 years since you asked this question :p but if it can help someone this is the script I used for the bumper :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bumper : MonoBehaviour
{
public int bumperForce = 800;
private GameObject player;
void Start ()
{
player = GameObject.FindGameObjectWithTag ("Player");
}
public void OnTriggerEnter (Collider collider)
{
if (collider.gameObject == player) {
player.GetComponent<Rigidbody> ().AddExplosionForce (bumperForce, transform.position, 1);
}
}
}
Thank you! I should have come back and updated my question with an answer so that our internet is more filled with solutions--not just questions!
Your answer
Follow this Question
Related Questions
Door hinge joints 0 Answers
How to make rigidbody not effect movement while still using it for collisions? 0 Answers
Backspin of Sphere with Rigidbody 0 Answers
Set predicate dice value when rolling 0 Answers
Falling Platform 1 Answer