Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Transferring Force on Collision

Discussion in 'Scripting' started by turbosheep, Jun 7, 2015.

  1. turbosheep

    turbosheep

    Joined:
    Jul 19, 2013
    Posts:
    25
    Alright, so, I'm trying to create pinball flippers.
    I'm applying torque force on rigidbodies attached to the flippers, and everything works alright. Thing is, no matter with how much force I rotate them, the ball just kind of stays on the flipper, no matter how fast they 'flip'.

    I know there's the addforce() function, but shouldn't fast colliding rigidbodies alright realistically act forces on each other? Also, the ball has the right amount of bounciness, I tried applying bounciness to flipper, etc...
    I just want to know how to realistically apply the flipper torque force to the ball. Thanks in advance!
     
  2. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Have you looked at the balls weight? or the flippers weight? The physics do work like you want, or it wouldnt be a good physics engine!
     
  3. turbosheep

    turbosheep

    Joined:
    Jul 19, 2013
    Posts:
    25
    I tried to reduce the mass, but the ball just kind of gets 'placed' outside of the flippers trajectory, then basically has no added force at all; it just rolls straight down again.
     
  4. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    How are you rotating the rigid bodies? are you using gravity? how is the table orientated?
     
  5. turbosheep

    turbosheep

    Joined:
    Jul 19, 2013
    Posts:
    25
    I tried both a lerp method and an addtorque() method. Here's all of the code... Currently using the torque method.
    I'm using gravity, but in Z (-10). The table is actually flat, so I don't have to angle everything.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Flipper : MonoBehaviour
    5. {
    6.     Quaternion startRotation;
    7.     public float flipperSpeed = 0.1f;
    8.     Rigidbody rb;
    9.     bool addTorque;
    10.  
    11.     void Start()
    12.     {
    13.         startRotation = transform.rotation;
    14.         rb = GetComponent<Rigidbody>();
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         if (Input.GetKeyDown(KeyCode.E)) addTorque = true; //StartCoroutine(RotateFlipper());
    20.     }
    21.  
    22.     void FixedUpdate()
    23.     {
    24.         if (addTorque) AddFlipperTorque();
    25.     }
    26.  
    27.     void AddFlipperTorque()
    28.     {
    29.         Debug.Log("add torque");
    30.         Quaternion target = startRotation * Quaternion.Euler(0, 45, 0);
    31.         if (transform.rotation.eulerAngles.y < target.eulerAngles.y) rb.AddTorque(Vector3.up*5000f, ForceMode.Impulse);
    32.         else
    33.         {
    34.             rb.angularVelocity = Vector3.zero;
    35.             addTorque = false;
    36.             StartCoroutine(ResetFlipper());
    37.         }
    38.     }
    39.  
    40.  
    41.     IEnumerator RotateFlipper()
    42.     {
    43.         Quaternion target = startRotation * Quaternion.Euler(0, 30, 0);
    44.         float t = 0;
    45.         float conversionFactor = 1/flipperSpeed;
    46.  
    47.         while (t < 1)
    48.         {
    49.             t += Time.deltaTime * conversionFactor;
    50.             transform.rotation = Quaternion.Lerp(startRotation, target, t);
    51.             yield return null;
    52.         }
    53.  
    54.         StartCoroutine(ResetFlipper());
    55.     }
    56.  
    57.     IEnumerator ResetFlipper()
    58.     {
    59.         float t = 0;
    60.         float conversionFactor = 1/flipperSpeed;
    61.        
    62.         while (t < 1)
    63.         {
    64.             t += Time.deltaTime * conversionFactor;
    65.             transform.rotation = Quaternion.Lerp(transform.rotation, startRotation, t);
    66.             yield return null;
    67.         }
    68.        
    69.        
    70.     }
    71.  
    72. //END
    73. }
     
  6. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Thats weird so the ball just hits the flipper and stops? Does it force the flipper to move? The only thing I could think of was either, that gravity was causing it - but you said your table is aligned fine, or the ball was two heavy. The physics materials on both the ball and flipper have a lot of bouncieness right?

    Are you using a joint for the flipper?
     
  7. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    also, try switching gravity back to normal and angle the table a bit just as a test.
     
  8. turbosheep

    turbosheep

    Joined:
    Jul 19, 2013
    Posts:
    25
    It's not like the ball doesn't move at all, but the the force is small and always the same, no matter how much force I use for the addtorque() and where it hits the flipper. I can increase bounciness, but that also applies if the ball hits the flipper without it moving. Tried the gravity thing, didn't work. Thanks for the effort, though.
     
  9. greg_vrl

    greg_vrl

    Joined:
    Aug 10, 2015
    Posts:
    2
    I think you want to use RigidBody.MoveRotation() instead of setting the rotation on the transform directly. if you do so, it'll move translate the body, but I don't believe there's any velocity data..so the other object doesn't react as if it was hit by a moving body.

    try:
    rb.MoveRotation(Quaternion.Lerp(startRotation, target, t));

    instead of:

    transform.rotation=Quaternion.Lerp(startRotation, target, t);