0
\$\begingroup\$

I'm creating a skating game, my Idea now for moving was when I move and then dont press any button anymore to move, it should still roll a bit and decrease momentum after a short while to fully stop. My problem now is when I dont press any buttons anymore after moving, it stops automaticly.

I came up with the idea of giving the rigidbody a force to the direction of the player, what is kinda working like I want to have it but its not realy connected with the momentum, it stops a little bit in the air or while I am skating then slowly going to stop. I want to use the momentum of my move Float "speed" to get keep the momentum and slowly decrease till it stops.

This is my code I came up with. I COMMENTED THE CODE ARE WHAT I CAME UP WITH LOOK ON "void Rolling" FUNCTION.


//MOVEMENT FLOATS
public float speed; //starting Speed float
public float maxspeed; // Maximum Speed float (changeable)
private float minspeed = 0;
public float dcrsSpeedMltpr; //decreaseSpeedMultiplier (changeable)
public float runSpeed;
public float turnSmoothing; //Smoothness of rotating (changeable)
public float jumpSpeed; //Speed of the Jump (changeable)

public GameObject rayCastGround; //Raycasting if Player is onGround or not
private Vector3 movement; //Sets the movement
private Vector3 targetDirection;
private Rigidbody playerRigidBody;
private Animator anim;
public LayerMask Groundmask; //Groundmask for Jump Bool

public bool isGrounded;
public bool isSkating = false;

void Awake()
{
    playerRigidBody = GetComponent<Rigidbody> ();
    anim = GetComponent<Animator>();
    speed = 0f;
}

void FixedUpdate()
{
    float lh = Input.GetAxisRaw ("Horizontal");
    float lv = Input.GetAxisRaw ("Vertical");

    Jump ();
    Move (lh, lv);
    Rolling ();

    if(lh > 0 || lv > 0 || lh < 0 || lv < 0 )
    {
        if(isGrounded)
        {
            anim.Play("SkatingAnim");
        }
    }
    else
    {
        if(isGrounded && speed == 0f)
        {
            anim.Play ("SkatingIdleAnim");
        }
    }
}


void Move (float lh, float lv)
{
    movement.Set (lh, 0f, lv);
    //movement = Camera.main.transform.TransformDirection(movement);

    //RUNSPEED
    if (Input.GetKey (KeyCode.LeftShift) && isGrounded)
    {
        movement = movement.normalized * runSpeed * Time.deltaTime;
    } 
    //SET THE SPEED OF MAX AND MIN SPEED!
    else
    {
        if(Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.D) || Input.GetKey (KeyCode.W) || Input.GetKey (KeyCode.S))
        {
            speed = speed + dcrsSpeedMltpr * Time.deltaTime;
            if(speed >= maxspeed)
            {
                speed = maxspeed;
            }

            isSkating = true;
            if(isSkating)
            {
                anim.SetBool ("isOnSkating", true);
            }
        }
        else
        {
            isSkating = false;
            if(!isSkating)
            {                   
                anim.SetBool("isOnSkating", false);
            }

            speed = speed - dcrsSpeedMltpr * Time.deltaTime;
            if(speed <= minspeed)
            {
                speed = minspeed;
            }
        }
        anim.SetFloat("Skatespeed", speed);
    }

    movement = movement.normalized * speed * Time.deltaTime;

    playerRigidBody.MovePosition (transform.position + movement);

    if (lh != 0f || lv != 0f) 
    {
        Rotating(lh, lv);
    }
}
//HERE IS THE CODE I CAME UP WITH ADDFORCE ON RIGIDBODY IT SHOULD ROLL A BIT AFTER STOPPING MOVING TILL THE MOMENTUM IS ZERO.
void Rolling()
{
    //MOVES A LITTLE BIT FURTHER WHEN PLAYER STOPS SKATING
    AnimatorStateInfo info = anim.GetCurrentAnimatorStateInfo(0);
    if(!isSkating) //<- WHEN THE PLAYER STOPS SKATING IT GOES TO ROLL ANIMATION AND SHOULD THEN DECREASE ITS MOMENTUM TILL ZERO
    {
        playerRigidBody.AddForce(transform.forward * speed * 1.75f);
        Debug.Log(speed);
    }
}


void Rotating (float lh, float lv)
{
    targetDirection = new Vector3 (lh, 0f, lv);

    Quaternion targetRotation = Quaternion.LookRotation (targetDirection, Vector3.up);

    Quaternion newRotation = Quaternion.Lerp (GetComponent<Rigidbody> ().rotation, targetRotation, turnSmoothing * Time.deltaTime);

    playerRigidBody.MoveRotation(newRotation);      
}
\$\endgroup\$
  • \$\begingroup\$ What values are you using for maxspeed and dcrsSpeedMltpr? What output are you seeing in your log? The following two lines are going to be fighting for dominance: (in Move): speed = speed - dcrsSpeedMltpr * Time.deltaTime; and then (in Rolling): playerRigidBody.AddForce(transform.forward * speed * 1.75f);. Also in one method you're physically moving the object, while in the other you're applying a force. \$\endgroup\$ – Zhaph - Ben Duguid Jan 22 '18 at 9:57
  • \$\begingroup\$ The Maxspeed is there for the speed that it doesnt go further than the maxspeed, if I press WASD the speed goes from 0 to maxspeed, but when I dont press WASD the speed goes down to minspeed that is set to 0 but it takes a little time to reach 0 and also to reach maxspeed. The dcrsSpeedMltpr is just a little number that the speed doesnt go up that fast. And I know I was applying a force to the rb but I didnt come up with any other Idea, maybe someone could help me on that. \$\endgroup\$ – Daniel Jan 22 '18 at 12:33
  • \$\begingroup\$ You're right - the use of both MovePosition and and forces don't seem to be an issue - and your code seems to work for me (dropped onto a sphere with a Rigidbody) - have you added Drag or Gravity constraints to your rigid-body that are stopping it sooner than you expect? \$\endgroup\$ – Zhaph - Ben Duguid Jan 22 '18 at 13:04
1
\$\begingroup\$

You should consider adding a Physic Material to your skate and bring down the Dynamic and Static frictions.

enter image description here

Debugging is difficult and you have a lot of things going on in your script. Something could be interfering with something else. I suggest you strip it down and slowly add things back to it. Start with something like the below script that will move a cube left and right, pressing he up and down arrows. Once you have the deisred behavior, then add more of your script and see where it is breaking it.

public float force; 

private Rigidbody rg;

void Start()
{
    rg = GetComponent<Rigidbody>();
}

void FixedUpdate()
{
    if (Input.GetKey(KeyCode.UpArrow))
    {
        rg.AddRelativeForce(Vector3.right * force ,ForceMode.Force);
    }
    if (Input.GetKey(KeyCode.DownArrow))
    {
        rg.AddRelativeForce(Vector3.right * -force, ForceMode.Force);
    }
} 
\$\endgroup\$
  • \$\begingroup\$ I worked out the code u gave me. The Rolling Function works now like I wanted to, but when I move to the side by pressing A or D key it goes first right then when I let the key go it goes forward a bit whyever and I need help to rotate my player towards the camera rotation, nothing seems to work I looked up many codes and tried it. \$\endgroup\$ – Daniel Jan 23 '18 at 0:30
  • \$\begingroup\$ Sounds like your initial question was mostly answered. And now you have a new question. It is better to start a new question, add description, code, screenshots so we can try to help you again. I am not 100% sure, with looking at your code what exactly is going on. \$\endgroup\$ – Fabio S. Jan 23 '18 at 0:37
  • \$\begingroup\$ I already found out now how to move to the facing direction of the camera. But my player slides around like on ice, when I turn he still slides forward a bit then starts turning, its maybe cause of the force Applying to the rigibody. \$\endgroup\$ – Daniel Jan 23 '18 at 1:31
  • \$\begingroup\$ Because you said "like ice" it makes me think it's probably because of the physics material. Try this. Add a second collider to your skate, make sure this collider is slitly above the one with the physics material. This new collider should be without the physics material, and your player should be in contact with this new collider. See If that fixes it. \$\endgroup\$ – Fabio S. Jan 23 '18 at 1:40
  • \$\begingroup\$ It didnt fixed it, I guess it is cause of the force applying from AddRelativeForce, because when I move not that fast and stop it stops faster then when I am moving fast and then try to stop, we have to come up with something that stops the force faster I already changed on calculating my speed the dcrsMltplr to another float what is called upcrsMltplr. I use two different Multipliers, one when I am starting skating to gain speedForce from slow to fast and one when I dont skate Anymore to decrease the speedForce amount faster then it goes up. \$\endgroup\$ – Daniel Jan 23 '18 at 2:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.