- Home /
Question by
LeonardoBite · Mar 23, 2021 at 09:00 PM ·
2dphysics2d-platformercolor changedouble jump
How to make correct double jump with collor changing?
Hello everyone who will see it:) I am very new in C# programming and unity (classic for this type of questions). In my game i am trying to make my character doing stuff when it have one color and doing other stuff when have another color. In this particular situation I am trying to make it double jumb when it have blue color. I was trying to make all physics in fixedUpdate and inputs in update but still not the result what i want. It is working chaotically. What am i doing wrong or maybe i must change approach for this kind of mechics? I will be really appreciate every help :)
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerMovement : MonoBehaviour
{
SpriteRenderer sr;
public Scene loadedlevel;
private Rigidbody2D _rb;
[SerializeField] float AirJumpForce = 3f;
[Header("For Movement")]
[SerializeField] float speed ;
private float XDirectionInput;
[Header("For Jumping")]
[SerializeField] float JumpForce;
[SerializeField] LayerMask groundLayer;
[SerializeField] Transform groundCheck;
[SerializeField] Vector2 groundCheckSize;
public bool isGrounded;
private bool hasJumped;
private bool hasDoubleJumped;
private bool jumping;
private bool doubleJump;
[Header("Colors")]
private Color redColor;
private Color blueColor;
private bool isBlue;
void Start()
{
blueColor = new Color(0f, 0f, 1f, 1f);
redColor = new Color(1f, 0f, 0f, 1f);
loadedlevel = SceneManager.GetActiveScene();
sr = GetComponent<SpriteRenderer>();
_rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
isGrounded = Physics2D.OverlapArea(
new Vector2(transform.position.x + 0.64f, transform.position.y - 0.64f),
new Vector2(transform.position.x - 0.64f, transform.position.y - 0.64f),
groundLayer);
XDirectionInput = Input.GetAxis("Horizontal");
XDirectionInput *= speed;
Vector3 characterScale = transform.localScale;
if (isGrounded)
{
jumping = false;
hasJumped = false;
doubleJump = false;
hasDoubleJumped = false;
}
if (Input.GetKeyDown(KeyCode.Space))
{
if (isGrounded)
{
jumping = true;
}
else if(!isGrounded && !hasJumped && isBlue)
{
jumping = true;
}
else if(jumping && !doubleJump )
{
doubleJump = true;
}
}
if (jumping && !hasJumped)
{
_rb.velocity = new Vector2(_rb.velocity.x, JumpForce);
hasJumped = true;
}
else if (doubleJump && !hasDoubleJumped )
{
hasDoubleJumped = true;
}
}
private void FixedUpdate()
{
isBlue = sr.color.Equals(blueColor);
_rb.velocity = new Vector2(XDirectionInput, _rb.velocity.y);
Flip(XDirectionInput);
}
private void Flip(float movement)
{
Vector3 characterScale = transform.localScale;
if (movement < 0)
{
characterScale.x = -1;
wallJumpDirection = 1;
}
if (movement > 0)
{
characterScale.x = 1;
wallJumpDirection = -1;
}
transform.localScale = characterScale;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
colorChange(collision);
}
void colorChange(Collision2D collision)
{
if (collision.transform.tag == "redPlatform")
{
sr.color = collision.gameObject.GetComponent<SpriteRenderer>().color;
}
else if (collision.transform.tag == "bluePlatform")
{
sr.color = collision.gameObject.GetComponent<SpriteRenderer>().color;
}
else if (collision.transform.tag == "whiteChanger")
{
sr.color = collision.gameObject.GetComponent<SpriteRenderer>().color;
}
}
void OnDrawGizmos()
{
Gizmos.color = new Color(0, 1, 0, 0.5f);
Gizmos.DrawCube(new Vector2(transform.position.x , transform.position.y - 0.66f),
new Vector2(1.28f, 0.05f ));
}
Comment