Howdy again,
I’m back…this time I’m working on the movement code for a Platformer. So far everything is working with one exception…the Character doesn’t go back to a grounded state and set VerticalSpeed to 0 once they go through 0.0. This particular bit of offending code is just a test case, my next step is to add a custom Collision Block that will get attached to the quads being used to build my level. I went this route because I get way better control over the movement of the player. In testing things out using the key command configuration tool in Unity…I have 8 devices plugged into my computer (all joystick type peripherals…because I flight sim a lot). I’m actually really happy with the overall movement code so far, and the jump code…which i’ll nerf with a timer (ol’ boy rolls around with a jet pack). The key thing is, making sure I can get a good collision and grounded code set working. So, with that, here’s the code!
V/R
Brad
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
//Player initialization...
public int PlayerHealth = 64;
//These variables set the rate at which the player Moves...
public int MovementSpeed = 3;
public int JumpSpeed = 6;
public float Gravity = 0.5f;
//These variables hold the speed as set in the Key Press code...
float VerticalSpeed;
float HorizontalSpeed;
//This Vector2 is required to hold the Transform information so it can be modified...
Vector2 PlayerPosition;
//This Boolean is meant to be used in the Weapon code for determining which direction to shoot the players bullets...
//bool PlayerFacingRight = true;
//This Boolean is used for determining if the player is in the air or not...
bool PlayerGrounded = true;
// Use this for initialization
void Start ()
{
//Make PlayerPosition hold the Players Transform...
PlayerPosition = transform.position;
}
// Update is called once per frame
void Update ()
{
//Player movement code...
if (Input.GetKey ("d"))
{
//PlayerFacingRight = true;
//Adjust the scaling so the Player faces in the right direction...
transform.localScale = new Vector2(1, 1);
//Set the Horizontal speed in the right direction...
HorizontalSpeed = MovementSpeed;
}
if (Input.GetKey ("a"))
{
//PlayerFacingRight = false;
//Adjust the scaling so the Player faces in the right direction...
transform.localScale = new Vector2(-1, 1);
//Set the Horizontal speed in the right direction...
HorizontalSpeed = -MovementSpeed;
}
//If both keys are being pressed or neither key is being pressed, and the Player is Grounded, set HorizontalSpeed to 0...
if ((Input.GetKey ("a") && Input.GetKey ("d")) || (!Input.GetKey ("a") && !Input.GetKey ("d")))
{
if (PlayerGrounded == true)
{
HorizontalSpeed = 0;
}
}
//Player Jump Code...
if (Input.GetKey("w"))
{
VerticalSpeed = JumpSpeed;
PlayerGrounded = false;
}
if (PlayerGrounded == false)
{
VerticalSpeed -= Gravity;
}
else if ((PlayerPosition.y + VerticalSpeed) >= 0.0f)
{
VerticalSpeed = 0;
PlayerGrounded = true;
}
//Modify the Players X and Y Positions...
PlayerPosition.x += HorizontalSpeed * Time.deltaTime;
PlayerPosition.y += VerticalSpeed * Time.deltaTime;
//Pass the Modified Player Position to the Transform...
transform.position = PlayerPosition;
}
}
Quick Update to the code…I have some Animation stuff setup, but everytime I uncomment it completely freezes the movement code and sticks the Player in place…what have I done wrong?
V/R
Brad
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
//Player initialization...
public int PlayerHealth = 64;
//These variables set the rate at which the player Moves...
public int MovementSpeed = 3;
public int JumpSpeed = 6;
public float Gravity = 0.5f;
//These variables hold the speed as set in the Key Press code...
float VerticalSpeed;
float HorizontalSpeed;
//This Vector2 is required to hold the Transform information so it can be modified...
Vector2 PlayerPosition;
//This Boolean is meant to be used in the Weapon code for determining which direction to shoot the players bullets...
//bool PlayerFacingRight = true;
//This Boolean is used for determining if the player is in the air or not...
bool PlayerGrounded = true;
// Use this for initialization
void Start ()
{
//Make PlayerPosition hold the Players Transform...
PlayerPosition = transform.position;
}
// Update is called once per frame
void Update ()
{
//Player movement code...
if (Input.GetKey ("d"))
{
//PlayerFacingRight = true;
//Adjust the scaling so the Player faces in the right direction...
transform.localScale = new Vector2(1, 1);
//Set the Horizontal speed in the right direction...
HorizontalSpeed = MovementSpeed;
//Play the Player Run Animation...
//gameObject.GetComponent<Animation>().Play("PlayerRun");
}
if (Input.GetKey ("a"))
{
//PlayerFacingRight = false;
//Adjust the scaling so the Player faces in the right direction...
transform.localScale = new Vector2(-1, 1);
//Set the Horizontal speed in the right direction...
HorizontalSpeed = -MovementSpeed;
//Play the Player Run Animation...
//gameObject.GetComponent<Animation>().Play("PlayerRun");
}
//If both keys are being pressed or neither key is being pressed, and the Player is Grounded, set HorizontalSpeed to 0...
if ((Input.GetKey ("a") && Input.GetKey ("d")) || (!Input.GetKey ("a") && !Input.GetKey ("d")))
{
if (PlayerGrounded == true)
{
HorizontalSpeed = 0;
//Play the Player Idle Animation...
//gameObject.GetComponent<Animation>().Play("PlayerIdle");
}
}
//Player Jump Code...
if (Input.GetKey("w"))
{
VerticalSpeed = JumpSpeed;
PlayerGrounded = false;
}
if (PlayerGrounded == false)
{
VerticalSpeed -= Gravity;
}
else if ((PlayerPosition.y + VerticalSpeed) >= 0.0f)
{
VerticalSpeed = 0;
PlayerGrounded = true;
}
//Modify the Players X and Y Positions...
PlayerPosition.x += HorizontalSpeed * Time.deltaTime;
PlayerPosition.y += VerticalSpeed * Time.deltaTime;
//Pass the Modified Player Position to the Transform...
transform.position = PlayerPosition;
}
}
Another quick update. Got the animation working…Turns out the way I was doing it was all wrong. Just needed to use the SetBool function that’s attached to the Animator after it’s been assigned to an object. Go figure! Works like a charm now! So…Now I need help on the collision. I’m using BoxCollider2D’s. One is on an Empty Game Object called Collision Box, the other is on the Player itself. Right now one goes right through the other…I’m using the OnCollisionEnter2D function for collision detection…but I’m suspecting that since I’m not using physics (from the reading I’ve done I think this is the case) it isn’t going to work right…can someone smarter than I confirm or deny that please?
Here’s the updated Code:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
//Player initialization...
public int PlayerHealth = 64;
//These variables set the rate at which the player Moves...
public int MovementSpeed = 3;
public int JumpSpeed = 6;
public float Gravity = 0.5f;
//This is required for Animating the Player...
Animator PlayerAnimator;
//These variables hold the speed as set in the Key Press code...
float VerticalSpeed;
float HorizontalSpeed;
//This Vector2 is required to hold the Transform information so it can be modified...
Vector2 PlayerPosition;
//This Boolean is meant to be used in the Weapon code for determining which direction to shoot the players bullets...
//bool PlayerFacingRight = true;
//This Boolean is used for determining if the player is in the air or not...
bool PlayerGrounded = true;
// Use this for initialization
void Start ()
{
//Set PlayerPosition to the Players Transform...
PlayerPosition = transform.position;
//Attach the Animator to PlayerAnimator
PlayerAnimator = GetComponent<Animator> ();
//Initialize the Animation State...
PlayerAnimator.SetBool ("IDLE", true);
}
// Update is called once per frame
void Update ()
{
//Player movement code...
if (Input.GetKey ("d"))
{
//PlayerFacingRight = true;
//Adjust the scaling so the Player faces in the right direction...
transform.localScale = new Vector2(1, 1);
//Set the Horizontal speed in the right direction...
HorizontalSpeed = MovementSpeed;
//Transition from the IDLE State...
PlayerAnimator.SetBool("IDLE", false);
}
if (Input.GetKey ("a"))
{
//PlayerFacingRight = false;
//Adjust the scaling so the Player faces in the right direction...
transform.localScale = new Vector2(-1, 1);
//Set the Horizontal speed in the right direction...
HorizontalSpeed = -MovementSpeed;
//Transition from the IDLE State...
PlayerAnimator.SetBool("IDLE", false);
}
//If both keys are being pressed or neither key is being pressed, and the Player is Grounded, set HorizontalSpeed to 0...
if ((Input.GetKey ("a") && Input.GetKey ("d")) || (!Input.GetKey ("a") && !Input.GetKey ("d")))
{
if (PlayerGrounded == true)
{
HorizontalSpeed = 0;
//Transition Back to the Idle State.
PlayerAnimator.SetBool("IDLE", true);
}
}
//Player Jump Code...
if (Input.GetKey("w"))
{
VerticalSpeed = JumpSpeed;
PlayerGrounded = false;
}
if (PlayerGrounded == false)
{
VerticalSpeed -= Gravity;
}
//Modify the Players X and Y Positions...
PlayerPosition.x += HorizontalSpeed * Time.deltaTime;
PlayerPosition.y += VerticalSpeed * Time.deltaTime;
//Pass the Modified Player Position to the Transform...
transform.position = PlayerPosition;
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.name == "CollisionBox")
{
PlayerGrounded = true;
VerticalSpeed = 0;
PlayerAnimator.SetBool ("IDLE", true);
}
}
}
Fixed it. Required me to add a RigidBody 2D. I also adjusted the OnCollisionEnter2D code to use ‘tag’ instead of ‘name’.
Guess I didn’t need to bother the forums for this 
Edit…Spoke to soon. The character will actually penetrate into the floor, and then if I stagger the boxes the character remains at the same altitude…could use some help there now I guess. Oh well, learnin’ for sure!
Updated Code:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
//Player initialization...
public int PlayerHealth = 64;
//These variables set the rate at which the player Moves...
public int MovementSpeed = 3;
public int JumpSpeed = 6;
public float Gravity = 0.5f;
//This is required for Animating the Player...
Animator PlayerAnimator;
//These variables hold the speed as set in the Key Press code...
float VerticalSpeed;
float HorizontalSpeed;
//This Vector2 is required to hold the Transform information so it can be modified...
Vector2 PlayerPosition;
//This Boolean is meant to be used in the Weapon code for determining which direction to shoot the players bullets...
//bool PlayerFacingRight = true;
//This Boolean is used for determining if the player is in the air or not...
bool PlayerGrounded = true;
// Use this for initialization
void Start ()
{
//Set PlayerPosition to the Players Transform...
PlayerPosition = transform.position;
//Attach the Animator to PlayerAnimator
PlayerAnimator = GetComponent<Animator> ();
//Initialize the Animation State...
PlayerAnimator.SetBool ("IDLE", true);
}
// Update is called once per frame
void Update ()
{
//Player movement code...
if (Input.GetKey ("d"))
{
//PlayerFacingRight = true;
//Adjust the scaling so the Player faces in the right direction...
transform.localScale = new Vector2(1, 1);
//Set the Horizontal speed in the right direction...
HorizontalSpeed = MovementSpeed;
//Transition from the IDLE State...
PlayerAnimator.SetBool("IDLE", false);
}
if (Input.GetKey ("a"))
{
//PlayerFacingRight = false;
//Adjust the scaling so the Player faces in the right direction...
transform.localScale = new Vector2(-1, 1);
//Set the Horizontal speed in the right direction...
HorizontalSpeed = -MovementSpeed;
//Transition from the IDLE State...
PlayerAnimator.SetBool("IDLE", false);
}
//If both keys are being pressed or neither key is being pressed, and the Player is Grounded, set HorizontalSpeed to 0...
if ((Input.GetKey ("a") && Input.GetKey ("d")) || (!Input.GetKey ("a") && !Input.GetKey ("d")))
{
if (PlayerGrounded == true)
{
HorizontalSpeed = 0;
//Transition Back to the Idle State.
PlayerAnimator.SetBool("IDLE", true);
}
}
//Player Jump Code...
if (Input.GetKey("w"))
{
VerticalSpeed = JumpSpeed;
PlayerGrounded = false;
}
if (PlayerGrounded == false)
{
VerticalSpeed -= Gravity;
}
//Modify the Players X and Y Positions...
PlayerPosition.x += HorizontalSpeed * Time.deltaTime;
PlayerPosition.y += VerticalSpeed * Time.deltaTime;
//Pass the Modified Player Position to the Transform...
transform.position = PlayerPosition;
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "CollisionBox")
{
PlayerGrounded = true;
VerticalSpeed = 0;
PlayerAnimator.SetBool ("IDLE", true);
}
}
}
Solved this one too…except for the penetration issues.
I had to have:
//Check to see if the player has stopped collding with Tagged objects and do the following...
void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.tag == "CollisionBox")
{
PlayerGrounded = false;
}
//Put Enemy and Enemy bullet collision code here...
}
So now. Can anyone help me solve the interpenetration of objects issue?