简体   繁体   中英

i have a bug with my ship, it doesn't want to appear in my game side and i dont know why

these are the settings of my ship

这些是我的船的设置

And these are my camera settings

这些是我的相机设定

this is the code that is added to the ship, my problem as you can see is that i can't find my ship in the game which later on caused a lot of problems :(

using UnityEngine;
using System.Collections;

public class PlayerControll : MonoBehaviour {
    public float speed = 5.0f;

    float xmin;
    float xmax;

    void Start(){
        float distance = transform.position.z - Camera.main.transform.position.z;
        Vector3 leftmost = Camera.main.ViewportToWorldPoint(new Vector3(0,0,distance));
        Vector3 rightmost = Camera.main.ViewportToWorldPoint(new Vector3(1,0,distance));
        xmin = leftmost.x;
        xmax = rightmost.x;

    }
    void Update () {
        if(Input.GetKey(KeyCode.LeftArrow)){

            transform.position += Vector3.left * speed * Time.deltaTime;

        }else if (Input.GetKey(KeyCode.RightArrow)){

            transform.position += Vector3.right * speed * Time.deltaTime;

        }
        float newX = Mathf.Clamp (transform.position.x, xmin, xmax);
        transform.position = new Vector3 (newX, transform.position.y, transform.position.z);
    }
}

First of all your PlayerControll script has some compile time errors which must be fixed before you can proceed. Make sure that the code doesn't have any errors and as @Programmer mentioned just restart Unity or recreate the script.

And then try changing the z position of your ship. Currently both your camera and ship are at z=0 which might be the reason why you don't see your ship. Move it back/front a bit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM