Search Unity

Can someone help me with rigidbodies running on loops like a sonic game?

Discussion in 'Scripting' started by supersm64fan, Jul 7, 2015.

  1. supersm64fan

    supersm64fan

    Joined:
    Jun 10, 2015
    Posts:
    13
    I have no idea how im supposed to have sonic loop physics in a third person 3D game. I have no experience on C#, java script and boo. What i wanted to do is have the character tilt with the ramp/loop-de-loop without falling off of the loop unless he goes slowly up the ramp/loop. I also have rotation disabled on the rigidbody so the character doesn't fall over.But, when the character falls off ramps/slopes/loops, he returns to the original orientation(which in this case for my character is X=0, Y=90,Z=0).
     
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Could you child the player to the loop when his velocity is above a certain amount & de-child it if they slow down so that gravity then takes effect? You could also try setting the players rotation so whenever they aren't touching anything they are the right way up.

    Not sure if they will work how you want but that is certainly where I would start in order to see how close it gets to it.
     
  3. supersm64fan

    supersm64fan

    Joined:
    Jun 10, 2015
    Posts:
    13
    Child then unchild the player? I don't know how to do that. Are you saying to attach the player to the loop?
     
  4. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
  5. supersm64fan

    supersm64fan

    Joined:
    Jun 10, 2015
    Posts:
    13
    .-. Apparently the character flies off into an endless void.
     
  6. supersm64fan

    supersm64fan

    Joined:
    Jun 10, 2015
    Posts:
    13
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var hit: RaycastHit;
    4. var Velocity: float;
    5.  
    6. function Start () {
    7.  
    8. }
    9.  
    10. function Update () {
    11.  
    12. if (Physics.Raycast(transform.position, -transform.up,hit,3))
    13. {
    14. transform.rotation = Quaternion.FromToRotation(transform.up,hit.normal)*transform.rotation;
    15.  
    16.   }
    17.  
    18.   }
    I found this script but its buggy and it doesn't work everytime due to the movement script i use. It sometimes makes the character stay tilted.
     
  7. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Yep, the player becomes a part of the loop - it's how I got my player to stick to floating logs so they drifted across stream but could still walk around in the log. I had to ditch the rigid body & use a character controller tho as I had issues with the player scaling weirdly when he was on it & due to deadlines for handing the assignment in I couldn't spare more time to work on it.
     
  8. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570

    Is it when he makes it all the way around the loop & then reaches the ground again? If so I'd start by looking at the angle the loop is on when he hits the ground, you may need to level the ramp out first. Check to see if you can see what is different about the times it works & the times it doesn't, I.e does it always work on certain ramps & always fails in others? If so, what's the difference in those ramps at the point the player is wrong? Work back along from that spot until you can see something happening.
     
  9. Slaghton

    Slaghton

    Joined:
    Sep 9, 2013
    Posts:
    139
    Perhaps you do something like, when you enter the loop a ray hitting the loop sets normal gravity to 0 and makes your character stick to the normals or faces of the loop and when the speed gets below a certain point or the ray isnt touching the loop anymore it turns default gravity back on and turns off the force pushing you into the faces of the loop?

    I seen something like this used to simulate gravity on a spheroid world like that mario wii game.
     
    Celadaris likes this.
  10. supersm64fan

    supersm64fan

    Joined:
    Jun 10, 2015
    Posts:
    13
    Yes, when he finishes the loop he stays tilted. And its near the end of the loop where it happens
     
  11. supersm64fan

    supersm64fan

    Joined:
    Jun 10, 2015
    Posts:
    13
    The script i had shown is very buggy,choppy and not smooth at all. I really want to know how im supposed to make the player a part of a loop, but i don't know how.
     
    Last edited: Jul 8, 2015
  12. supersm64fan

    supersm64fan

    Joined:
    Jun 10, 2015
    Posts:
    13
    now i have a smooth rotating script, but it only goes about 50 degrees before falling. Here is the script.
    Code (JavaScript):
    1.  var adjustSpeed : float = 12;
    2. public var fromRotation : Quaternion;
    3. private var toRotation : Quaternion;
    4. private var targetNormal : Vector3;
    5. private var hit : RaycastHit;
    6. public var weight : float = 9;
    7.  
    8. function Start() {
    9.      targetNormal = transform.up;
    10. }
    11. function Update() {
    12.      if(Physics.Raycast(transform.position, -Vector3.up, hit)) {
    13.      
    14.            
    15.              targetNormal = hit.normal;
    16.              fromRotation = transform.rotation;
    17.              toRotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
    18.              weight = 0;
    19.            
    20.        
    21.          if(weight <= 1) {
    22.              weight += Time.deltaTime * adjustSpeed;
    23.              transform.rotation = Quaternion.Slerp(fromRotation, toRotation, weight);
    24.            
    25.          }
    26.      }
    27. }
     
  13. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Is there gravity? You could try turning it off when he is on the ramp
     
  14. supersm64fan

    supersm64fan

    Joined:
    Jun 10, 2015
    Posts:
    13
    how am i supposed to do that if i have 0 idea how to script in any program language.
     
  15. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    "Man, I really want to write a program, but I really don't want to have to learn how to write a program." Is there some language barrier here that's creating this impression? I sincerely hope that's the problem.

    Unity has a lot of fantastic GUI shortcuts, but it still requires a ton of programming. You need to hire a programmer or go grab a C# or Javascript book before worrying about Unity and making a game- nobody's going to build your game for you for free.
     
  16. supersm64fan

    supersm64fan

    Joined:
    Jun 10, 2015
    Posts:
    13
    well no duh. i knew that I would have to find at least a guide or something.
     
  17. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    If that's true a simple search would have shown you

    http://unity3d.com/learn

    Start with the tutorials. Put your game on hold (until you know how to code effectively & efficiently it will never be as good as you want it to be), do the tutorials then go back over them & introduce new elements to a) test if your understanding of how to code stuff works & b) learn how small changes can totally change how fun the game is/was etc

    People write scripts for what they need. Copying them & trying to use them without knowing how to modify or link them will cause more problems than it solves
     
    apparition likes this.
  18. apparition

    apparition

    Joined:
    Jan 11, 2012
    Posts:
    120
    It's very unlikely that you would find a guide for something so specific. I would suggest learning the basics of Unity and C#, maybe starting with a simpler game first, until you can work it out yourself.
     
  19. supersm64fan

    supersm64fan

    Joined:
    Jun 10, 2015
    Posts:
    13
    Never mind. I found something called the locomotion system which had what i was looking for.
     
    yanuaris likes this.
  20. yanuaris

    yanuaris

    Joined:
    Oct 16, 2015
    Posts:
    61
    How do you use the locomotion system so that it'll stick to the 360 walls?