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

Rotate object on fling

Discussion in 'Scripting' started by piggybank1974, Jan 26, 2017.

  1. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    This is the third time I've tried to post this here.

    I'm trying to fling an object(voxel ball I created) in the correct direction, and have it rotate in the direction it is spinning, when it hits colliders (see idea2.png screenshot), it bounces off.

    I've posted a little running gif using "screen to gif", that shows what the object is doing, I think I have the flinging part working fine, but I have no clue as to how to do the rotation(23+ years since this type of thing and i did do it anyways)

    I'm pulling my hair out with this, and I've not go much let anyways, Here is my code so far fling and everything.

    Any advice on this matter would greatly be appreciated.

    Code (CSharp):
    1. public class SwipeBallComponent : MonoBehaviour
    2. {
    3. private Vector3 mStartPos;
    4. private Vector3 mEndPos;
    5.  
    6. private Rigidbody mRigidbody;
    7. private float factor = 300.0F;
    8. private Vector3 mData;
    9. private Vector3 mInfinityVector;
    10.  
    11. void Start()
    12. {
    13.   mRigidbody = this.GetComponent<Rigidbody>();
    14.   mInfinityVector = new Vector3(Mathf.Infinity, Mathf.Infinity, Mathf.Infinity);
    15.   mData =  mInfinityVector;
    16. }
    17.  
    18. void Update()
    19. {
    20.   if (Input.GetMouseButtonDown(0) == true)
    21.   {
    22.    mStartPos = Input.mousePosition;
    23.    mStartPos = Camera.main.ScreenToWorldPoint(mStartPos);
    24.    //Debug.Log("StartPos: " + mStartPos.ToString());
    25.   }
    26.  
    27.   if (Input.GetMouseButtonUp(0) == true)
    28.   {
    29.    mEndPos = Input.mousePosition;
    30.    mEndPos = Camera.main.ScreenToWorldPoint(mEndPos);
    31.  
    32.    //Debug.Log("EndPos: " + mEndPos.ToString());
    33.    Vector3 heading = mEndPos - mStartPos;
    34.    float distance = heading.magnitude;
    35.    velecity = distance;
    36.    Vector3 direction = heading / distance;
    37.  
    38.    mData = direction * 5F * factor;
    39.   }
    40. }
    41.  
    42. void FixedUpdate()
    43. {
    44.    if (mData.Equals(mInfinityVector) == false)
    45.    {
    46.     mRigidbody.AddRelativeForce(mData);
    47.     mRigidbody.AddTorque(new Vector3(mData.x, 0F, -mData.y));
    48.     mData = mInfinityVector;
    49.  
    50.    }
    51. }
    52.  
    53. public void OnCollisionExit(Collision collision)
    54. {
    55.   if (collision.collider != null)
    56.   {
    57.    //Rigidbody mRigidbody = collision.collider.gameObject.GetComponent<Rigidbody>();
    58.  
    59.    Vector3 mVel = -mRigidbody.velocity;
    60.    mRigidbody.AddRelativeTorque(new Vector3(-mVel.x, 0F, mVel.y));
    61.   }
    62. }
    63. }
    64.  
    Thanks
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    One way is to put the voxel graphics inside a sphere collider and just let the physics drive the sphere's rotation. Unless you make the PhysicMaterial very slippery, it should feel much like a rolling ball bouncing on a wall.

    Remember, the inertia of the roll will be transferred and change the angle of bounce so that it won't be a clean bounce each time once you do this.
     
  3. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    @Kurt-Dekker

    Thank you for taking the time to reply, The voxel ball is currently in a sphere collider and has a material that has full bounciness e.g set to 1, I've just set the bounciness down to 0.5, and it feels no different.

    Could I use the Quaternion.LookRotation to rotate the ball to at least look in the correct direction?

    I still want it to spin in the correct direction though.

    maybe you can put together a little demo to show my what you mean, if that's not to much trouble.

    I've been pulling my hair out on this for several days.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    The spinning is going to be more a function of the drag. If you have the drag set to nothing on the physicMaterial, then it will slide like ice on ice. If you have it set higher, it will act more like a billiard ball on a table.
     
  5. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    Kurt is on the right track, but you're looking for friction, not drag. Friction will cause the ball to rotate. Adjust it on the PhysicMaterial.
     
    Kurt-Dekker likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Yes, apologies for sloppy terminology. Thanks for noting that @cranky !!

    The Rigidbody has drag that causes the object to slow.

    The PhysicMaterial has static and dynamic friction that affects how "slippery" the collision contact is.

    To make the object rotate as it "rolls" along a surface, the PhysicModel's friction properties will be what you want.
     
  7. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    Also be aware of the difference between static and dynamic (kinetic) friction.

    Static friction determines how much force you need to push a still object and move it, while kinetic friction is the force pushing against a moving object.

    I think I defined that correctly.
     
    Kurt-Dekker likes this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Close... static friction is when the two surfaces are not moving relative to each other, i.e., like a car tire rolling along the ground, or as the case you said, an object that is at rest.

    Dynamic friction is when the surfaces are sliding over each other.

    Obviously in real life there is also a band of hysteresis as the relative speed is near zero and surface deformation comes into play, causing the actual relative speed to oscillate between zero and nonzero.

    This is what you hear with tire chatter when you brake or accelerate really hard. (And when it is not the car's ABS or other traction control software operating!)
     
  9. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    @Kurt-Dekker
    @cranky

    Sorry for the late reply, I've added you both in this post because you have been so kind to reply, I've still not go it working, but here is what I've done, so you might be able to guide me more.

    First I now have two physics materials, (Bounce is forthe Ball, and Collider_Bounce is for the wall edge colliders if you want to call them that.

    Bounce
    Dynamic Friction: 200
    Static Friction: 0
    Bounciness: 1
    Friction Combine: Average
    Bounciness Combine: Average

    Collider_Bounce
    Dynamic Friction: 175
    Static Friction: 0
    Bounciness: 0.75
    Friction Combine: Average
    Bounciness Combine: Average

    What happens is the Ball does bounce ok but after a couple of collisions does not rotate anymore, although it ONLY rotates on a single angle from Left to right not forward to backwards.

    I've not set any angular drag or drag on the balls rigidbody.

    Thanks again.