Jump to content
Unity Insider Forum

Ändern von transform.localScale


Retoren

Recommended Posts

Hallo Zusammen

Ich bin noch neu hier und versuche mit Unity, aus Spass, einen kleinen Platformer zu erstellen.

Ich gehe viele verschiedene Tutorials durch um an mein Ziel zu kommen. Diverse Bücher gekauft um langsam alles zu verstehen.

 

Nun bin ich an einen Punkt gekommen den ich nicht verstehe, besser gesagt es funktionierte einmal und nach ein paar Änderungen bzw. weiter scripten funktioniert plötzlich mein Flip() nicht mehr.

 

Hier mal mein PlayerController:

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

public class PlayerController : MonoBehaviour
{

    private Rigidbody2D rigi2d;
    private BoxCollider2D boxCollider2D;
    [SerializeField] private LayerMask platformLayerMask;

    //walk
    public float Speed = 20.0f;
    [HideInInspector]
    public bool canMove = true;
    private float movementX;
    private float movementY;
    [HideInInspector]
    public bool lookingRight =true;

    //jump
    public float JumpForce = 20.0f;
    [HideInInspector]
    public bool isJumping;
    private float jumpTimeCounter;
    private float jumpTime;
    private int extrajump;
    public int extraJumpMax;
    public float fall = -5f;
    [HideInInspector]
    public bool falling = false;

    //dash
    public float dashSpeed = 25f;
    IEnumerator dashCoroutine;
    IEnumerator dashCoroutineRight;
    [HideInInspector]
    public bool isDashing;
    [HideInInspector]
    public bool canDash = true;
    private float direction = 1;
    private float normalGravity;
    public float dashDuration;
    public float dashCoolDown;

    //Cam
    public cameraFollow mainCamera;

    //wallSlid/walljump
    [HideInInspector]
    public float checkRadius;
    private bool isTouchingFront;
    public Transform checkWall;
    private bool wallSlide;
    public float wallSlideSpeed;

    //animation
    public Animator animator;
    
    

    void Start()
    {

        rigi2d = GetComponent<Rigidbody2D>();
        boxCollider2D = GetComponent<BoxCollider2D>();
        mainCamera= GameObject.FindGameObjectWithTag("MainCamera").GetComponent<cameraFollow>();
        normalGravity = rigi2d.gravityScale;
    }
    
    void Update()
    {
        //Move///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        if (canMove == true)
        {
            if (movementX != 0)
            {
                direction = movementX;
            }
            movementX = Input.GetAxisRaw("Horizontal");
            movementY = Input.GetAxisRaw("Vertical");
            animator.SetFloat("Speed", Mathf.Abs(movementX));
            if ((movementX > 0 && !lookingRight) || (movementX < 0 && lookingRight))
            {
                Flip();
            }

            if (movementY > 0)
                {
                    mainCamera.offset = new Vector3(0, 5, -10);
                }else if (movementY < 0)
                {
                    mainCamera.offset = new Vector3(0, -3, -10);
                }else if (movementY == 0)
                {
                    mainCamera.offset = new Vector3(0, 2, -10);
                }
                
            
        }
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        //Jump////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        if (isGrounded() || isTouchingFront == true)
        {
            extrajump = 0;
        }

        /////////////////////

        if (Input.GetButtonDown("Jump"))
        {
            if (isGrounded() || isTouchingFront == true)
            {
                isJumping = true;
                rigi2d.velocity = Vector2.up * JumpForce;
                jumpTimeCounter = jumpTime;
            }
            else 
            {
                if (Input.GetButton("Jump"))
                {
                    if (extrajump < extraJumpMax)
                    {
                        animator.SetBool("DoubleJump", true);
                        isJumping = true;
                        rigi2d.velocity = Vector2.up * JumpForce;
                        jumpTimeCounter = jumpTime;
                        extrajump++;
                    }
                }
            }
        }

        /////////////////////
        
        if (Input.GetButton("Jump") && isJumping == true)
        {
            animator.SetBool("IsJumping", true);
            if (jumpTimeCounter > 0)
            {
                jumpTimeCounter -= Time.deltaTime;
                rigi2d.velocity = Vector2.up * JumpForce;
            }
            else
            {
                isJumping = false;
            }
        }

        /////////////////////

        if (Input.GetButtonUp("Jump"))
        {
            isJumping = false;
        }

        /////////////////////

        if (rigi2d.velocity.y < fall)
        {
            falling = true;
        }
        else
        {
            animator.SetBool("IsFalling", false);
            falling = false;
        }

        if (falling)
        {
            animator.SetBool("IsFalling", true);
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        //Dash////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        if (Input.GetKeyDown(KeyCode.E) && canDash == true)
        {
            if (dashCoroutine != null)
            {
                StopCoroutine(dashCoroutine);
            }
            else if (dashCoroutineRight != null)
            {
                StopCoroutine(dashCoroutineRight);
            }
            
                dashCoroutine = Dash(dashDuration, dashCoolDown);
                StartCoroutine(dashCoroutine);
            
                dashCoroutineRight = DashRight(dashDuration, dashCoolDown);
                StartCoroutine(dashCoroutineRight);

        }
        
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        //WallSlid///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        isTouchingFront = Physics2D.OverlapCircle(checkWall.position, checkRadius, platformLayerMask);

        if (isTouchingFront == true)
        {
            animator.SetBool("IsWallSlid", true);
            wallSlide = true;
        }
        else
        {
            animator.SetBool("IsWallSlid", false);
            wallSlide = false;
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    }
    
    private bool isGrounded()
    {
        animator.SetBool("IsJumping", false);
        animator.SetBool("DoubleJump", false);
        animator.SetBool("IsWallSlid", false);
        float extraHightText = 0.05f;
        RaycastHit2D raycastHit2D = Physics2D.Raycast(boxCollider2D.bounds.center, Vector2.down, boxCollider2D.bounds.extents.y + extraHightText, platformLayerMask);
        return raycastHit2D.collider != null;
    }

    IEnumerator Dash(float dashDuration, float dashCoolDown)
    {
        Vector2 orginalVelocity = rigi2d.velocity;
        isDashing = true;
        canDash = false;
        animator.SetBool("IsDashLeft", true);
        rigi2d.gravityScale = 0;
        rigi2d.velocity = Vector2.zero;
        yield return new WaitForSeconds(dashDuration);
        rigi2d.velocity = orginalVelocity;
        isDashing = false;
        animator.SetBool("IsDashLeft", false);
        rigi2d.gravityScale = normalGravity;
        extrajump--;
        yield return new WaitForSeconds(dashCoolDown);
        canDash = true;
    }

    IEnumerator DashRight(float dashDuration, float dashCoolDown)
    {
        Vector2 orginalVelocity = rigi2d.velocity;
        isDashing = true;
        canDash = false;
        animator.SetBool("IsDash", true);
        rigi2d.gravityScale = 0;
        rigi2d.velocity = Vector2.zero;
        yield return new WaitForSeconds(dashDuration);
        rigi2d.velocity = orginalVelocity;
        isDashing = false;
        animator.SetBool("IsDash", false);
        rigi2d.gravityScale = normalGravity;
        extrajump--;
        yield return new WaitForSeconds(dashCoolDown);
        canDash = true;
    }

    void FixedUpdate()
    {
        //move
        rigi2d.velocity = new Vector2(movementX * Speed , rigi2d.velocity.y);

        //jump

        //dash
        if (isDashing)
        {
            rigi2d.AddForce(new Vector2(direction * dashSpeed, 0), ForceMode2D.Impulse);
        }

        //wallslide
        if (wallSlide)
        {
            rigi2d.velocity = new Vector2(rigi2d.velocity.x, Mathf.Clamp(rigi2d.velocity.y, -wallSlideSpeed, float.MaxValue));
        }
    }
    public void Flip()
    {
        lookingRight = !lookingRight;
        Vector3 myScale = transform.localScale;
        myScale.x *= -2;
        transform.localScale = myScale;
    }
}

Ich möchte dass wenn ich mich nach links bewege der Scale.X auf den Wert -2 wechselt. Es hat mal funktioniert, doch im moment bleibt der Wert auf 2, egal in welche Richtung ich mich bewege. Ich finde den fehler oder warum dies nicht gewechselt wird heraus. Hoffe jemand kann helfen und mir erklären was ich da genau flasch gemacht habe.

 

Vielleich grundsätzlich auch mal Kritik am Script selber anmerken? Damit ich es in Zukunft es besser machen kann.

 

Vielen Dank für eure Hilfe.

Link zu diesem Kommentar
Auf anderen Seiten teilen

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gast
Auf dieses Thema antworten...

×   Du hast formatierten Text eingefügt.   Formatierung jetzt entfernen

  Only 75 emoji are allowed.

×   Dein Link wurde automatisch eingebettet.   Einbetten rückgängig machen und als Link darstellen

×   Dein vorheriger Inhalt wurde wiederhergestellt.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Lädt...
×
×
  • Neu erstellen...