Search Unity

I've been trying to script a more realistic movement, but I've done something wrong.

Discussion in 'Scripting' started by Tutuki_, Dec 16, 2017.

  1. Tutuki_

    Tutuki_

    Joined:
    Dec 16, 2017
    Posts:
    3
    I want to make a 2d game with 3d graphics. My idea of realistic movement is that the character shouldn't start at max speed, but slowly accelerate towards it, but as soon as I let go of the right key, it quickly stops.

    That part was not that great, but was ok so far, however I got really stuck when I tried to add the jump.


    As my jump goes I want my character to go up slower than it falls, although its not "real-world" realistic, I think I works wonders for video games. I try doing that by changing the gravity via script on the jump.
    The problem is that my character don't jump at all, its like its stuck to the ground.

    Here is my code, can you guys review for me please? <3 <# <3



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     public float speed;
    8.     public float fallMultiplier = 2.5f;
    9.     public float lowJumpMultiplier = 2f;  
    10.  
    11.     private Rigidbody rb;
    12.  
    13.     void Awake()
    14.     {
    15.         rb = GetComponent<Rigidbody>();
    16.     }
    17.  
    18.     void FixedUpdate()
    19.     {
    20.         float moveHorizontal = Input.GetAxis("Horizontal");
    21.       //  float moveVertical = Input.GetAxis("Vertical");
    22.  
    23.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, 0.0f);
    24.  
    25.         rb.AddForce(movement * speed * Time.deltaTime, ForceMode.Impulse);
    26.  
    27.         if (rb.velocity.y <= 0)
    28.         {
    29.             rb.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
    30.         } else if (rb.velocity.y > 0 && !Input.GetButton ("Jump")){
    31.             rb.velocity += Vector3.up * Physics.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;
    32.         }
    33.  
    34.     }
    35.  
    36.  
    37.  
    38. }
    39.  
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    This guy worked out a way to make it seem a little more old school
     
  3. Tutuki_

    Tutuki_

    Joined:
    Dec 16, 2017
    Posts:
    3

    I know, that was my inspiration. But differently from his, my script doesn't seen to work.
     
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    You have code for what to do if the jump button isn't pressed, but not for when it is pressed. If I remember right, he used two scripts for that. The jump was on the other one. It doesn't have to be, you should move it onto this one.
     
    Last edited: Dec 16, 2017