角色移动时Unity 2D相机会放大,但相机的属性不会改变

时间:2019-01-21 12:31:20

标签: unity3d camera zoom

点击播放后,相机会保持应有的状态,但是当我移动角色时,相机会无缘无故放大

相机的属性完全不改变,变焦并且所有内容保持不变。尝试从正交更改为透视图不变,移动z轴不变,更改比例不变,更改分辨率也没有变化,从而使相机不是父代,并且也没有变化,它的行为与父代和子代相同 before character walks

after character walks

我不认为这与代码有关,但这是我角色附加的代码,相机的行为与孩子和父母相同

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

public class movement : MonoBehaviour
{
    public float speed = 5f;
    public float jumpSpeed = 8f;
    private float movementX = 0f;
    private Rigidbody2D rigidBody;
    public Transform groundCheckPoint;
    public float groundCheckRadius;
    public LayerMask groundLayer;
    public bool isTouchingGround;
    public SpriteRenderer box;

    private bool canSpawn = true;
    private bool canAnimateWalk = true;
    private bool canAnimateIdle = true;
    private bool canAnimateJump = true;
    private bool stopJump = true;
    private int spawningSpeed = 1000;
    // Use this for initialization
    void Start()
    {
        rigidBody = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        isTouchingGround = Physics2D.OverlapBox(groundCheckPoint.position,new Vector2(0.9f,0.1f),0f, groundLayer);
        movementX = Input.GetAxis("Horizontal");
        if (movementX > 0f)
        {
            if(canAnimateWalk==true && isTouchingGround)
            {
                canAnimateWalk = false;
                StartCoroutine(AnimateWalk());
            }

            GetComponent<SpriteRenderer>().transform.localScale = new Vector3(2, 2, 1);
            rigidBody.velocity = new Vector2(movementX * speed, rigidBody.velocity.y);
        }
        else if (movementX < 0f)
        {
            if (canAnimateWalk == true && isTouchingGround)
            {
                canAnimateWalk = false;
                StartCoroutine(AnimateWalk());
            }
            GetComponent<SpriteRenderer>().transform.localScale = new Vector3(-2, 2, 1);
            rigidBody.velocity = new Vector2(movementX * speed, rigidBody.velocity.y);
        }
        else
        {
            if(isTouchingGround)
            {
                StopCoroutine(AnimateWalk());

                if(canAnimateIdle==true)
                {
                    canAnimateIdle = false;
                    StartCoroutine(AnimateIdle());
                }

            }
            rigidBody.velocity = new Vector2(0, rigidBody.velocity.y);
        }
        if (Input.GetButtonDown("Jump") && isTouchingGround)
        {
            canAnimateJump = false;
            rigidBody.velocity = new Vector2(rigidBody.velocity.x, jumpSpeed);
            StartCoroutine(AnimateJump());
        }
        else if(!isTouchingGround)
        {
            StopCoroutine(AnimateWalk());
        }
    }

    IEnumerator AnimateJump()
    {
        Debug.Log("Animating Jump");
        int counter = 0;
        while (counter < 10)
        {
            counter++;
            GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("img/j" + counter);
            yield return new WaitForSeconds(0.1f);
            if(isTouchingGround==true)
            {
                break;
            }
        }
        while(!isTouchingGround)
        {
            GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("img/j10");
            yield return new WaitForSeconds(0.1f);
        }
        GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("img/i1");
        canAnimateWalk = true;
        canAnimateJump = true;
    }

    IEnumerator AnimateIdle()
    {
        int counter = 0;
        while(Input.GetAxis("Horizontal")==0 && counter <10 && rigidBody.velocity.y==0)
        {
            counter++;
            GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("img/i"+counter);
            yield return new WaitForSeconds(0.2f);
        }
        canAnimateIdle = true;
    }
    IEnumerator AnimateWalk()
    {
        int counter = 0;
        while (Input.GetAxis("Horizontal")!=0 && counter < 8 && rigidBody.velocity.y==0)
        {
            counter++;
            GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("img/g" + counter);
            yield return new WaitForSeconds(0.08f);
        }

        canAnimateWalk = true;

    }
}

可能是什么?我尽力了

2 个答案:

答案 0 :(得分:0)

GetComponent<SpriteRenderer>().transform.localScale = new Vector3(-2, 2, 1);

如果您的移动脚本已附加到“ guy”游戏对象,则您正在更改其(本地)比例。所有孩子也将相应缩放。 由于您的相机是男孩子,因此它将缩放并产生您看到的结果。

尝试取消与您的摄像头的关联,并创建一个跟随您的摄像头的脚本,并将其附加到您的摄像头。

答案 1 :(得分:0)

我解决了我的问题

问题出在字符缩放上。相机没有变化,但是角色的大小确实使我​​相信可以放大。

我的角色x和y比例分别是1和1,但我在移动时使用了2和2比例

比例尺用于左右移动角色时旋转