Search Unity

Stick to ground script

Discussion in 'Scripting' started by TheRexenor, Oct 8, 2015.

  1. TheRexenor

    TheRexenor

    Joined:
    Jun 12, 2015
    Posts:
    23
    Hi there!

    I'm trying to make a space race idea and I need the vessel to stick to the track while moving. Is there a s script that can do this???
     

    Attached Files:

  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You can always raycast downwards and set its position to wherever the hit.point is. Getting the rotation right will be a little tougher, but using hit.normal and such can be used to solve that.
     
  3. TheRexenor

    TheRexenor

    Joined:
    Jun 12, 2015
    Posts:
    23
    I am using the script below and the vessel keeps on jumping all over the place!


    Code (JavaScript):
    1. function Update () {
    2.     var hit : RaycastHit;
    3.     if (Physics.Raycast (transform.position, -Vector3.up, hit)) {
    4.         var distanceToGround = hit.distance;
    5.         //use below code if your pivot point is in the middle
    6.         //transform.position.y = hit.distance - transform.GetComponent.<Collider>().bounds.extents;
    7.         //use below code if your pivot point is at the bottom
    8.         transform.position.y = hit.distance;
    9.     }
    10. }
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You should probably use the vessel's down and not the global down?
     
  5. TheRexenor

    TheRexenor

    Joined:
    Jun 12, 2015
    Posts:
    23
    I change up to down and it seems to work better but still it has problems...