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. Dismiss Notice

Need help/suggestions with player following 360 loop in track

Discussion in 'Scripting' started by Boo-Let, Dec 27, 2020.

  1. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    So title says it. I have been messing with this the last few days and have tried every suggestion other answers have provided to get that "smooth" transition across a surface normal. The problem I am have is. Since the "track" isn't perfectly smooth, and as the player reaches another polygon it causes a "twitch" between each poly all the way around the loop. I am not using a rigidbody nor a character controller. Simply an object with a primitive collider.

    Any suggestions on getting it smoothed out over polys? I mean obviously there has to be a way as I've seen many games do it. I just cannot manage a good solution after like 40+ variations. Now, I am not good with vector math so I am sure there is something I could learn that might help out, that I have missed.

    Thanks.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Here's a few ideas to try:

    1. use two raycasts, but don't read the normals. Instead use the points where the raycasts hit and derive your own normal (point1 minus point2 but rotated 90 degrees locally. Think of it like straight edge going around the faceted circle.

    2. read the intended angle (which snaps from discrete values), but only gradually adjust the angle of your craft towards that... this has problems the faster you go obviously, to get the tuning overall right.

    3. read 10 raycasts from nose to tail of your ship and use the average of all the normals

    Each of these will absolutely work, but they will each have different "feels" to the smoothness.
     
    PraetorBlue likes this.
  3. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    I use three points right now. 2 in front and 1 in rear. Each about 2 units in front and behind. the fronts are separated about 4 units apart. Giving 2 units per side. So to find the rotation needed along that I would need the sum of all 3 and find the average? or would I need to cross reference rear center point to front left, and rear center to front right. Then subtract those two? Like I said i'm not good with vector math. Do you know a good documentation link I should check out?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Actually if you're already doing three and getting their contact points in space, you can get the normal with the cross product of the two sides.

    SO... if A B are at the nose and C is at the tail, draw a "V" between them, the point of the V being at the stern of your vehicle.

    The left side of the V is A-C and the right side of the V is B-C... with me so far?

    Cross product is then:

    Code (csharp):
    1. var N = Vector3.Cross( (A-C), (B-C));
    We'll call this N for Normal. This is a spike sticking up from your craft, perpendicular to the plane where ABC lie.

    That N is probably quite near to the current
    tranform.up
    , different by whatever you have tilted since last frame.

    Therefore you need to produce a rotation that will get you from your current rotation (with transform.up) to the new rotation you want (with N being up).

    Code (csharp):
    1. var tilt = Quaternion.FromToRotation( transform.up, N); // here's how much we have to change
    2.  
    3. transform.rotation = tilt * transform.rotation;  // apply our new tilt to our existing rotation
    See? Vector math is easy! :)

    To see how this will look, take 2 hardback books and hold them in an open-L shape:

    Code (csharp):
    1. \_
    Then take your ship (perhaps your cel phone) and gently push it along the bottom book towards the ramp of the other book, and watch how its rotation changes as the nose of the phone slides up the diagonal book.

    That's what the above will give you.
     
    Boo-Let likes this.
  5. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Cool deal. Makes good sense. I never used vector math like that so I wasn’t sure how to approach it. I’ll give it a shot when I get home in next couple of days and let you know how it turns out. Thanks.
     
    Kurt-Dekker likes this.