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

A more 'slippery' controller

Discussion in 'Scripting' started by ostrich160, May 19, 2014.

  1. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    Hi guys, so currently I have this script attached to my player

    And its pretty cool, it works nicely, and yeh, good stuff.
    Before I carry on, I should mention Im using this in a 2d top down style.

    So anyway, the issue with it is, its too neat. Like I press w, and it goes straight forward until I stop pressing w and then it just halts. Turning speed is a bit too neat as well. I want it to be slidy and slippery, like really slippery, so for example I press w and then stop pressing it, and it keeps going until it slows down. And then when I turn is might turn a bit too much and just go round in circles, you know what I mean.
    So yeh, how would I do that?

    Cheers guys
     
  2. Deleted User

    Deleted User

    Guest

    This script is moving the unit/character by transform.Translate which means you will most likely have to use some form of lerping to get a smoothed out motion (or slippery motion). My suggestion would be to look at Mathf.lerp. Could try to use some thing along the lines of (Vector3.forward(moveDist*targetGas)) with targetGas = Mathf.lerp(targetGas, currentGas, time.deltatime). Allowing currentGas to replace the normal gas variable.

    Alas the if(gas!=0) {} stops the character from updating it's movement and also canceling any slippery/lerping motion you might have.
    Hopefully this will help you get started along to getting some basic slipper motion up and running.

    It might also be worth a look to try having motion by physics. You would then be able to utilize physics materials to easily create a sliding character.
     
    Last edited by a moderator: May 19, 2014
  3. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    Thanks mate, could you tell me where I put the mathf.lerp and all that other stuff, like in place of the transform or whatever. Sorry Im just new to coding, but Im learning which is always good
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Hahahaha.... it's so cute.


    OK, I took your idea.. not your code. but your code is very basic. I added Velocity to it, not just move and go stuff.

    When you do your moves and most everything, you will want to use Time.deltaTime. That helps smooth movements and stuff like that.

    Notice, I used a lerp as suggested. I also added in a lean to the vehicle to add some interest to it. Its not the greatest and there is no real physics movement in it, but its fun to see.

    Code (csharp):
    1.  
    2. private var power : float = 10;
    3. var maxSpeed : float = 20;
    4. var velocity : Vector3;
    5. var lean : float = 3;
    6.  
    7. function Update(){
    8.     var gas = Input.GetAxis("Vertical");
    9.     var steer = Input.GetAxis("Horizontal");
    10.    
    11.     velocity = Vector3.Lerp(velocity, transform.forward * gas * maxSpeed, power * Time.deltaTime);
    12.     var v = transform.InverseTransformDirection(velocity);
    13.     if(v.z < 0) steer = -steer;
    14.    
    15.     transform.position += velocity * Time.deltaTime;
    16.    
    17.     transform.LookAt(transform.position + transform.forward);
    18.     transform.Rotate(transform.up * 100 * steer * (velocity.magnitude / maxSpeed) * Time.deltaTime);
    19.    
    20.     transform.Rotate(Vector3.back * v.x * lean);
    21. }
    22.  
     
  5. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    Thanks mate, your a bloody lifesaver. This should be fun to play with when Im on my PC again, unfortunately Im only on my phone at the moment so I cant say how good it works, but even for the effort, thanks mate!

    *Grumbles*
     
  6. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    Alright, I just got a chance to play with the script. Its fantastic, especially that leaning, that was insanely cool!
    I do have two issues though, not complaints of course, just something you might be able to help with...

    1). Its still relitevely neat, I found a game that demonstrates the kind of insane movement I want. I dont know how hard this is to achieve, if its very hard I apologise for the big ask, but worth a shot

    http://store.steampowered.com/app/289760

    2). At speeds of over 35 it passes through corners with a bit of pushing. I tried that 'DontGoThroughThings' script, and it didnt really work. EDIT: I fixed this by using the age old solution of making the colliders fatter
     
    Last edited: May 20, 2014
  7. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Actually, rockets are very easy. They are basically rotation and thrust. In the case of those rockets, it is as simple as this:
    Code (csharp):
    1.  
    2. function UpdateRocket(){
    3.     var steer = Input.GetAxis("Horizontal");
    4.     velocity += transform.forward * power * Time.deltaTime;
    5.     if(velocity.magnitude > maxSpeed) velocity = velocity.normalized * maxSpeed;
    6.    
    7.     transform.position += velocity * Time.deltaTime;
    8.    
    9.     transform.Rotate(transform.up * 100 * steer * Time.deltaTime);
    10. }
    11.  
    In the start, while it's launching, you would'nt allow the player any control over it for like a second, then it's full speed ahead. The point is consistent thrust, not user controlled thrust.
     
  8. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    This is fantastic, issue is its kind of too hard to steer now. What I was thinking is really fast but sliding turns and that kind of thing. Also, you cant stop which is pretty crucial to my game.

    Thanks again by the way mate, this is all good stuff!
     
  9. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    "Slippery" but can stop? Tricky.

    You could make the thrust turn on and off like Asteroids maybe.

    Do you want the whole rotation thing, or would you rather directly thrust right when I push right, left when I push left without the trouble of turning around?
     
  10. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    Oh it needs to rotate unfortunately, thats kind of needed for the game to work.

    By slippy but can stop, as I said the steam link is quite good for that, you see when it turns it kind of skids around a bit, which makes the turning unpredictable, which is kind of what I want
     
  11. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    The demo from steam did not stop. Or it never showed this. It looked to be just 100% power all the time.


    That being said, stopping and such is easy. its as simple as adding in the same gas from the car part to it.
    Code (csharp):
    1.  
    2.     function UpdateRocket(){
    3.         var steer = Input.GetAxis("Horizontal");
    4.         var pow = Input.GetAxis("Vertical") * power;
    5.         //pow = Mathf.Clamp(pow, 0, power);
    6.         velocity += transform.forward * pow * Time.deltaTime;
    7.         if(velocity.magnitude > maxSpeed) velocity = velocity.normalized * maxSpeed;
    8.        
    9.         transform.position += velocity * Time.deltaTime;
    10.        
    11.         transform.Rotate(transform.up * 100 * steer * Time.deltaTime);
    12.     }
    13.  
     
  12. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    To be fair to you, you have delivered everything I asked for, but it just doesnt seem to work as I expected it to. Ive probably been wording this very badly. This should make it a bit easier. So far that original script you worked for me is the best. I just want steering to be a bit more frantic, slippy was a bad word to use, I guess skidding is much better in this context, rather than rockets Im thinking more poorly built racing cars on mud or something, you know what I mean?

    Also, a way to adjust turn speed on the script would be fantastic as well.

    So sorry for all this bother, however it has taught me a lot more about the unity physics scripting.
     
  13. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679