Search Unity

Skating on Ice

Discussion in 'Scripting' started by MitchStan, Mar 24, 2007.

  1. MitchStan

    MitchStan

    Joined:
    Feb 26, 2007
    Posts:
    568
    I've been looking around the forum and the wiki site and have experimented with some scripting examples to mimic a player skating on ice, but so far my efforts have produced more of a slipping and sliding effect., especially when rotating to turn.

    Can anyone point me to a script that they know of, or throw me a suggestion or code fragmnt that I can play with that will help me achieve some sort of skatng on ice feel?

    I'm not a total noob to programming, but noob enough to be humbled by what you guys can do so easily.

    Thanks for any help, guys.

    Mitch
     
  2. MitchStan

    MitchStan

    Joined:
    Feb 26, 2007
    Posts:
    568
    Here is some code I'm playing with - I've set the physic material of the playing surface to ice and the physic material of the GO as metal.

    The targetPosition is set in the inspector - I use LookRotation and lerp to get the GO turning and the AddRelativeForce in the forward direction to get the GO moving.

    Any thoughts about how I can use the friction setting to reduce the sideways slip?

    // Pick a xz position in the inspector:
    var targetPosition = Vector3.zero;

    var smooth = .5;
    var force = 2;


    function FixedUpdate() {

    var targetRotation = targetPosition - rigidbody.position;

    var rotation = Quaternion.LookRotation(targetRotation);

    rigidbody.rotation = Quaternion.Lerp (rigidbody.rotation, rotation,Time.deltaTime * smooth);

    rigidbody.AddRelativeForce(Vector3.forward*force);

    }
     
  3. jeffcraighead

    jeffcraighead

    Joined:
    Nov 15, 2006
    Posts:
    740
    I've done friction two different ways. One way is to calculate drag on each axis based on some coef*velocityOnAxis and any other property (you'll have to poll for the surface type you're on since you're bypassing the physics material, though if you're always on ice this really doesn't matter) and add the force manually using AddRelativeForce. The second way is to modify the rigidbody.drag property depending on the velocities along the various axes. The first way will probably work better/more realistically but require slightly more code.

    Jeff
     
  4. MitchStan

    MitchStan

    Joined:
    Feb 26, 2007
    Posts:
    568
    Thanks for the suggestions. It seems if I make a new physic material (skate blade) and play around with friction 2 values, that seems to work nicely. I define friction 2 in the x direction and then tweak the static friction 2 and dynamic 2 friction values.

    As usual, Unity makes everything easier than you think. Amazing.