简体   繁体   中英

How do I add Momentum to my Character in Unity C#?

I am very new to Unity 5, and I have a basic knowledge of the interface. I have never coded anything big in particular. I can't do anything without tutorials. But I've found a tutorial a while ago that seems to fit my needs.

Basically what it was, was that I wanted to add momentum to the hitbox (cube) that the player would have. When you press the W key, you would build up speed and then reach terminal velocity. How would I accomplish this?

I've tried to do something to make it work, and I think I was pretty close (as I felt as though it was a working block) but I couldn't figure out where to put it or what the errors were saying so I came to this site.

Here is the Code: https://pastebin.com/dmSuJR7m

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterControls : MonoBehaviour {

public float speed = 3.0F;

// Use this for initialization
void Start () {
    Cursor.lockState = CursorLockMode.Locked;
}



// Update is called once per frame

void Update () {

    if (Input.GetKeyDown("w")) for > (deltaTime * 4) {
    public float speed = 6.0F
    }

    float translation = Input.GetAxis("Vertical") * speed;
        float strafe = Input.GetAxis("Horizontal") * speed;
        translation *= Time.deltaTime;
        strafe *= Time.deltaTime;

        transform.Translate(strafe, 0, translation);

        if (Input.GetKeyDown("escape"))
            Cursor.lockState = CursorLockMode.None;
    }
}

I don't need an immediate answer but you should consider answering when you see this.

Try this code :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterControls : MonoBehaviour {

    public float base_speed = 3.0f;
    public float max_speed = 6.0f;
    private float current_speed;

    // Use this for initialization
    void Start () {
        current_speed = base_speed;
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update () {

        if (Input.GetKey(KeyCode.W) && current_speed < max_speed) {
            current_speed += 1 * Time.deltaTime;
        } else if (!Input.GetKey(KeyCode.W) && current_speed > base_speed) {
            current_speed -= 1 * Time.deltaTime;
        }

        float translation = Input.GetAxis("Vertical") * current_speed;
        float strafe = Input.GetAxis("Horizontal") * current_speed;
        translation *= Time.deltaTime;
        strafe *= Time.deltaTime;

        transform.Translate(strafe, 0, translation);

        if (Input.GetKeyDown("escape"))
            Cursor.lockState = CursorLockMode.None;
    }
}

This code will take the base_speed (the start amount, which in this case, I also use for the normal amount) and set it to the current_speed which is used to manipulate your current speed without losing the base speed (keep a reference to what the starting amount was, if we didnt have this, we would have no idea when to stop accelerating and would have to hard code it which is never good).

When you hold down W without being fully accelerated, you will start accelerating, if you're not pressing W and is accelerated, start decelerating until you get to the base speed.

Your code wouldn't even compile because of

if (Input.GetKeyDown("w")) for > (deltaTime * 4)

a proper for loop looks like this:

for (int i = 0; i < 10; i++) {  }

and you cannot use it inside of an if statement.

Read this and this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM