Rotate object on fling

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.

public class SwipeBallComponent : MonoBehaviour
{
private Vector3 mStartPos;
private Vector3 mEndPos;

private Rigidbody mRigidbody;
private float factor = 300.0F;
private Vector3 mData;
private Vector3 mInfinityVector;

void Start()
{
  mRigidbody = this.GetComponent<Rigidbody>();
  mInfinityVector = new Vector3(Mathf.Infinity, Mathf.Infinity, Mathf.Infinity);
  mData =  mInfinityVector;
}

void Update()
{
  if (Input.GetMouseButtonDown(0) == true)
  {
   mStartPos = Input.mousePosition;
   mStartPos = Camera.main.ScreenToWorldPoint(mStartPos);
   //Debug.Log("StartPos: " + mStartPos.ToString());
  }

  if (Input.GetMouseButtonUp(0) == true)
  {
   mEndPos = Input.mousePosition;
   mEndPos = Camera.main.ScreenToWorldPoint(mEndPos);

   //Debug.Log("EndPos: " + mEndPos.ToString());
   Vector3 heading = mEndPos - mStartPos;
   float distance = heading.magnitude;
   velecity = distance;
   Vector3 direction = heading / distance;

   mData = direction * 5F * factor;
  }
}

void FixedUpdate()
{
   if (mData.Equals(mInfinityVector) == false)
   {
    mRigidbody.AddRelativeForce(mData);
    mRigidbody.AddTorque(new Vector3(mData.x, 0F, -mData.y)); 
    mData = mInfinityVector;

   }
}

public void OnCollisionExit(Collision collision)
{
  if (collision.collider != null)
  {
   //Rigidbody mRigidbody = collision.collider.gameObject.GetComponent<Rigidbody>();

   Vector3 mVel = -mRigidbody.velocity;
   mRigidbody.AddRelativeTorque(new Vector3(-mVel.x, 0F, mVel.y));
  }
}
}

Thanks

2936456--217204--Ball.gif

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.

@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.

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.

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.

1 Like

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.

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.

1 Like

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!)

@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.