Question by
themegabluman · May 28, 2016 at 05:02 AM ·
2d2d-platformer2d-gameplay
Changing player variables through button presses.
Hello everyone! I'm having any issues were I cannot switch in between several different variables for my player, i'm also using the standard 2D Unity character assets. I am only able to change these variables through the Unity Editor itself by ticking and un-ticking the Air Control options in Unity.
These are the PlatformerCharacter2D Script and the PlatformerCharacter2DUserControl, I'm sorry about the wall of code down below, any help would be much appreciated :
using System;
using UnityEngine;
namespace UnityStandardAssets._2D
{
public class PlatformerCharacter2D : MonoBehaviour
{
//Adult Movement Variables
public float maxSpeedAdult = 10f; // The fastest the player can travel in the x axis with Adult.
public float jumpForceAdult = 400f; // Amount of force added when the player jumps with Adult.
//Teen Movement Variables
public float maxSpeedTeen = 5;
public float jumpForceTeen = 400;
//Baby Movement Variables
public float maxSpeedBaby = 2;
public float jumpForceBaby = 50;
//[Range(0, 1)] [SerializeField] private float m_CrouchSpeed = .36f; // Amount of maxSpeed applied to crouching movement. 1 = 100%
[SerializeField] private bool m_AirControl = false;
[SerializeField] private bool m_AirControlForTeen = false;
[SerializeField] private bool m_AirControlForBaby = false;
// Whether or not a player can steer while jumping;
[SerializeField] private LayerMask m_WhatIsGround; // A mask determining what is ground to the character
private Transform m_GroundCheck; // A position marking where to check if the player is grounded.
const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded
private bool m_Grounded; // Whether or not the player is grounded.
private bool m_GroundedForTeen;
private bool m_GroundedForBaby;
private Transform m_CeilingCheck; // A position marking where to check for ceilings
const float k_CeilingRadius = .01f; // Radius of the overlap circle to determine if the player can stand up
private Animator m_Anim; // Reference to the player's animator component.
private Rigidbody2D m_Rigidbody2D;
private bool m_FacingRight = true; // For determining which way the player is currently facing.
private void Awake()
{
// Setting up references.
m_GroundCheck = transform.Find("GroundCheck");
m_CeilingCheck = transform.Find("CeilingCheck");
m_Anim = GetComponent<Animator>();
m_Rigidbody2D = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
m_Grounded = false;
// The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
// This can be done using layers instead but Sample Assets will not overwrite your project settings.
Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
m_Grounded = true;
}
m_Anim.SetBool("Ground", m_Grounded);
// Set the vertical animation
m_Anim.SetFloat("vSpeed", m_Rigidbody2D.velocity.y);
}
public void MoveAdult(float moveAdult, bool jumpAdult, bool adultTransform)
{
//only control the player if grounded or airControl is turned on
if (m_Grounded || m_AirControl)
{
// The Speed animator parameter is set to the absolute value of the horizontal input.
m_Anim.SetFloat("Speed", Mathf.Abs(moveAdult));
// Move the character (Adult)
m_Rigidbody2D.velocity = new Vector2(moveAdult* maxSpeedAdult, m_Rigidbody2D.velocity.y);
// If the input is moving the player right and the player is facing left...
if (moveAdult > 0 && !m_FacingRight)
{
// ... flip the player.
Flip();
}
// Otherwise if the input is moving the player left and the player is facing right...
else if (moveAdult < 0 && m_FacingRight)
{
// ... flip the player.
Flip();
}
}
// If the player should jump... (For the Adult)
if (m_Grounded && jumpAdult && m_Anim.GetBool("Ground"))
{
// Add a vertical force to the player.
m_Grounded = false;
m_Anim.SetBool("Ground", false);
m_Rigidbody2D.AddForce(new Vector2(0f, jumpForceAdult));
}
}
public void MoveTeen(float moveTeen, bool jumpTeen, bool teenTransform)
{
//only control the player if grounded or airControl is turned on
if (m_GroundedForTeen || m_AirControlForTeen)
{
// The Speed animator parameter is set to the absolute value of the horizontal input.
m_Anim.SetFloat("Speed", Mathf.Abs(moveTeen));
// Move the character (Adult)
m_Rigidbody2D.velocity = new Vector2(moveTeen * maxSpeedTeen, m_Rigidbody2D.velocity.y);
// If the input is moving the player right and the player is facing left...
if (moveTeen > 0 && !m_FacingRight)
{
// ... flip the player.
Flip();
}
// Otherwise if the input is moving the player left and the player is facing right...
else if (moveTeen < 0 && m_FacingRight)
{
// ... flip the player.
Flip();
}
}
// If the player should jump... (For the Teen)
if (m_GroundedForTeen && jumpTeen && m_Anim.GetBool("Ground"))
{
// Add a vertical force to the player.
m_GroundedForTeen = false;
m_Anim.SetBool("Ground", false);
m_Rigidbody2D.AddForce(new Vector2(0f, jumpForceTeen));
}
}
public void MoveBaby(float moveBaby, bool jumpBaby, bool babyTransform)
{
//only control the player if grounded or airControl is turned on
if (m_GroundedForBaby || m_AirControlForBaby)
{
// The Speed animator parameter is set to the absolute value of the horizontal input.
m_Anim.SetFloat("Speed", Mathf.Abs(moveBaby));
// Move the character (Adult)
m_Rigidbody2D.velocity = new Vector2(moveBaby * maxSpeedBaby, m_Rigidbody2D.velocity.y);
// If the input is moving the player right and the player is facing left...
if (moveBaby > 0 && !m_FacingRight)
{
// ... flip the player.
Flip();
}
// Otherwise if the input is moving the player left and the player is facing right...
else if (moveBaby < 0 && m_FacingRight)
{
// ... flip the player.
Flip();
}
}
// If the player should jump... (For the Teen)
if (m_GroundedForBaby && jumpBaby && m_Anim.GetBool("Ground"))
{
// Add a vertical force to the player.
m_GroundedForBaby = false;
m_Anim.SetBool("Ground", false);
m_Rigidbody2D.AddForce(new Vector2(0f, jumpForceBaby));
}
}
private void Flip()
{
// Switch the way the player is labelled as facing.
m_FacingRight = !m_FacingRight;
// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
}
And this is the other script:
using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
namespace UnityStandardAssets._2D
{
[RequireComponent(typeof (PlatformerCharacter2D))]
public class Platformer2DUserControl : MonoBehaviour
{
private PlatformerCharacter2D m_Character;
private bool m_jumpAdult;
private bool m_jumpTeen;
private bool m_jumpBaby;
private void Awake()
{
m_Character = GetComponent<PlatformerCharacter2D>();
}
private void Update()
{
if (!m_jumpAdult)
{
// Read the jump input in Update so button presses aren't missed.
m_jumpAdult = CrossPlatformInputManager.GetButtonDown("Jump");
}
if (!m_jumpTeen)
{
// Read the jump input in Update so button presses aren't missed.
m_jumpTeen = CrossPlatformInputManager.GetButtonDown("Jump");
}
if (!m_jumpBaby)
{
// Read the jump input in Update so button presses aren't missed.
m_jumpBaby = CrossPlatformInputManager.GetButtonDown("Jump");
}
}
private void FixedUpdate()
{
// Read the inputs.
bool adultTransform = Input.GetButtonDown("Adult Transform");
bool teenTransform = Input.GetButtonDown("Teen Transform");
bool babyTransform = Input.GetButtonDown("Baby Transform");
float h = CrossPlatformInputManager.GetAxis("Horizontal");
// Pass all parameters to the character control script.
m_Character.MoveAdult(h, m_jumpAdult, adultTransform);
m_Character.MoveTeen(h ,m_jumpTeen, teenTransform);
m_Character.MoveBaby(h, m_jumpBaby, babyTransform);
m_jumpAdult = false;
m_jumpTeen = false;
m_jumpBaby = false;
}
}
}
c470a9703aa61fd5e4fc9d82ec769015.png
(2.2 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612081650im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distance Joint 2D with Kinematic Player 2 Answers
2D Weird Jumping 1 Answer
2d platformer physics 0 Answers
My character won't stop crouching, they dont stop crouchin 2 Answers
Sound doesn't play on Trigger??? 2 Answers