Iqew
May 17, 2017, 9:52am
1
I’d like some help with my movement system. Currently, I’m making a game with inspired by games like Give Up and super meat boy. With that said, i want a movement system similar to those games. Basically I want it so that when I let go of a button, the player immediately stops or starts falling if it is mid-air or maybe do a double jump. At the moment this is all of my movement code.
using UnityEngine;
public class PlayerController : MonoBehaviour {
public GameObject Player;
public float speed;
public float JumpHeight;
//public float FallSpeed;
private Rigidbody rb;
private Vector3 Velocity;
private bool movX;
private bool grounded;
private bool isFalling;
void Start () {
rb = Player.GetComponent<Rigidbody>();
Velocity = Player.GetComponent<Rigidbody>().velocity;
movX = false;
}
void FixedUpdate () {
if(Velocity.y <= -0.1){
grounded = false;
isFalling = true;
}
if(Velocity.y >= 0.1){
grounded = false;
isFalling = false;
}
if(Velocity.y == 0){
grounded = true;
}
//MoveRight
if(Input.GetKey("d") || Input.GetKey(KeyCode.RightArrow)){
rb.isKinematic = false;
rb.AddForce(10 * speed * Time.deltaTime, 0, 0);
//Velocity.x = 10 * speed * Time.deltaTime;
movX = true;
}
//MoveLeft
else if(Input.GetKey("a") || Input.GetKey(KeyCode.LeftArrow)){
rb.isKinematic = true;
rb.AddForce(-10 * speed * Time.deltaTime, 0, 0);
//Velocity.x = -10 * speed * Time.deltaTime;
movX = true;
}
else{
rb.isKinematic = true;
}
if(Input.GetKeyDown("w") && grounded == true || Input.GetKeyDown(KeyCode.UpArrow) && grounded == true){
rb.isKinematic = false;
rb.AddForce(0, 10 * JumpHeight * Time.deltaTime, 0);
}
}
}
I know a lot of the code is wrong but i put it up here anyways so you guys can have some reference on the problem.
Hopefully someone can help me.
Donay
May 17, 2017, 11:04am
2
Start only runs once.
Think you need to make the following chages
void Start()
{
rb = Player.GetComponent<Rigidbody>();
}
void FixedUpdate() {
Velocity = rb.velocity;
movX = false;
To stop the player you can do somthing like
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
but make sure it only runs once when they user stops pressing a key or the player will stop moving.
You might also enjoy this article , where I demonstrate how to make a run & jump system (including fine jump control and double-jump)
Hi! I am new in unity and I have a question: Why in the if with key “d” isKinematic = false and in the if with the “a” key
isKinematic = true?
Yeah, that’s pretty suspicious. I suspect that that’s not really code you should be trying to learn from. Check out my article.
Hi, i wrote several scripts and attached them to player gameobject to handle player movement like supermeatboy.
i also applied some changes to the rigidbody2D of the player gameobject[quote=“Iqew, post:1, topic: 664951, username:Iqew”]
I’d like some help with my movement system. Currently, I’m making a game with inspired by games like Give Up and super meat boy. With that said, i want a movement system similar to those games. Basically I want it so that when I let go of a button, the player immediately stops or starts falling if it is mid-air or maybe do a double jump. At the moment this is all of my movement code.
using UnityEngine;
public class PlayerController : MonoBehaviour {
public GameObject Player;
public float speed;
public float JumpHeight;
//public float FallSpeed;
private Rigidbody rb;
private Vector3 Velocity;
private bool movX;
private bool grounded;
private bool isFalling;
void Start () {
rb = Player.GetComponent<Rigidbody>();
Velocity = Player.GetComponent<Rigidbody>().velocity;
movX = false;
}
void FixedUpdate () {
if(Velocity.y <= -0.1){
grounded = false;
isFalling = true;
}
if(Velocity.y >= 0.1){
grounded = false;
isFalling = false;
}
if(Velocity.y == 0){
grounded = true;
}
//MoveRight
if(Input.GetKey("d") || Input.GetKey(KeyCode.RightArrow)){
rb.isKinematic = false;
rb.AddForce(10 * speed * Time.deltaTime, 0, 0);
//Velocity.x = 10 * speed * Time.deltaTime;
movX = true;
}
//MoveLeft
else if(Input.GetKey("a") || Input.GetKey(KeyCode.LeftArrow)){
rb.isKinematic = true;
rb.AddForce(-10 * speed * Time.deltaTime, 0, 0);
//Velocity.x = -10 * speed * Time.deltaTime;
movX = true;
}
else{
rb.isKinematic = true;
}
if(Input.GetKeyDown("w") && grounded == true || Input.GetKeyDown(KeyCode.UpArrow) && grounded == true){
rb.isKinematic = false;
rb.AddForce(0, 10 * JumpHeight * Time.deltaTime, 0);
}
}
}
I know a lot of the code is wrong but i put it up here anyways so you guys can have some reference on the problem.
Hopefully someone can help me.
[/quote]
9017416–1243417–Supermeatboy movement mechanics.rar (3.21 KB)
Donay:
Start only runs once.
Think you need to make the following chages
void Start()
{
rb = Player.GetComponent<Rigidbody>();
}
void FixedUpdate() {
Velocity = rb.velocity;
movX = false;
To stop the player you can do somthing like
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
but make sure it only runs once when they user stops pressing a key or the player will stop moving.
My Bolds.
Watch yer spelling when writing code and stuff, it’s super sensitive.