繁体   English   中英

如何将游戏 object 移动到其他游戏的正向方向 object Unity3D

[英]How to move a game object in the forwrard direction of an other game object Unity3D

我正在制作一个相机 controller 脚本,我在矢量方面遇到了很多困难。 我正在制作一个类似 rts 的相机,我在做所有事情时都遇到了很多麻烦,但现在几乎所有东西都可以工作,除了一件事:鼠标滚动。

为了实现这个相机,我不得不把它放在一个空游戏 object 中来重置它的旋转(因为相机有一些旋转(x 上 65 度),并且对于运动之类的事情它更简单,所以我可以使用 transform.forward东西(如果相机正在移动而不是其父级,则 go 会向前和向下移动))。

目前,我的鼠标滚动正在上下移动相机,但我想将它向前移动到go(带有相机的transform.forward,所以它就像一个缩放),但是因为凸轮在游戏中object,怎么办我将游戏 object 朝相机 transform.forward 的方向移动。

到目前为止,这是我的代码:


public class CameraController : MonoBehaviour
{

    public float panSpeed = 50f;
    public float rotSpeed = 50f;
    //marge pour le déplacement à la souris
    public float scrollSpeed = 5f;
    public float dragSpeed = 5f;
    private Vector3 dragOrigin;
    
    [Header("Y")]
    public float minY = 10f;
    public float maxY = 80f;

    [Header("X")]
    public float minX = 10f;
    public float maxX = 80f;

    [Header("Z")]
    public float minZ = 10f;
    public float maxZ = 80f;

    void Update()
    {
        Vector3 newPos = transform.position;

        if(GameManager.gameIsOver)
        {
            //plus pouvoir controller la cam lors du game over
            this.enabled = false;
            return;
        }

        //déplacement avant
        if(Input.GetKey(KeyCode.W))
        {
            newPos += new Vector3(this.transform.forward.x,0,this.transform.forward.z) * panSpeed * Time.unscaledDeltaTime;
        } 

        //déplacement arrière
        if(Input.GetKey(KeyCode.S))
        {
             newPos += new Vector3(-this.transform.forward.x,0,-this.transform.forward.z) * panSpeed * Time.unscaledDeltaTime;
        } 

        //déplacement gauche
        if(Input.GetKey(KeyCode.A))
        {
            newPos += -transform.right * panSpeed * Time.unscaledDeltaTime;
        }

        //déplacement droite
        if(Input.GetKey(KeyCode.D))
        {
            newPos += transform.right * panSpeed * Time.unscaledDeltaTime;
        }

        //rotation droite
        if(Input.GetKey(KeyCode.Q))
        {
            transform.Rotate(Vector3.down * rotSpeed * Time.unscaledDeltaTime, Space.World);
        }

        //rotation gauche
        if(Input.GetKey(KeyCode.E))
        {
            transform.Rotate(Vector3.up * rotSpeed * Time.unscaledDeltaTime, Space.World);
        }

        //chopper la molette (molette = un axis de la souris aussi)
        float scroll = Input.GetAxis("Mouse ScrollWheel");

        //zoomer/dezoomer
        newPos.y -= scroll * 1000 * scrollSpeed * Time.unscaledDeltaTime;
        //bloquer la cam entre le min et le max
        newPos.y = Mathf.Clamp(newPos.y, minY, maxY);
        newPos.x = Mathf.Clamp(newPos.x, minX, maxX);
        newPos.z = Mathf.Clamp(newPos.z, minZ, maxZ);
        transform.position = newPos;

        UpdateDrag();
    }

    void UpdateDrag()
    {
        if (Input.GetMouseButtonDown(2))
        {
            dragOrigin = Input.mousePosition;
        }
 
        if (Input.GetMouseButton(2))
        {
            Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
            Vector3 move = new Vector3(pos.x * dragSpeed, 0, pos.y * dragSpeed);
 
            transform.Translate(-move, Space.Self);
            dragOrigin = Input.mousePosition; 
        }
 
    }


}

我不知道我的问题是否足够清楚,但在此先感谢您的帮助。

相机 controller 是父组件吗? 如果是的话,很好。 用相机给它一个参考您的孩子 object 并在滚动时更改孩子的 position 而不是父母。

当您现在滚动滚轮时,使用camera.forward ,其中 camera 是孩子。 对于拖动和其他所有内容,您仍然更改父 object 的 position。

非常感谢。 有效。

这是代码:

using UnityEngine;

public class CameraController : MonoBehaviour
{

    public float panSpeed = 50f;
    public float rotSpeed = 50f;
    //marge pour le déplacement à la souris
    public float scrollSpeed = 5f;
    public float dragSpeed = 5f;
    private Vector3 dragOrigin;
    
    [Header("Y")]
    public float minY = 10f;
    public float maxY = 80f;

    [Header("X")]
    public float minX = 10f;
    public float maxX = 80f;

    [Header("Z")]
    public float minZ = 10f;
    public float maxZ = 80f;

    public GameObject cam;

    void Update()
    {
        Vector3 newPos = transform.position;
        Vector3 newCamPos = cam.transform.localPosition;

        if(GameManager.gameIsOver)
        {
            //plus pouvoir controller la cam lors du game over
            this.enabled = false;
            return;
        }

        //déplacement avant
        if(Input.GetKey(KeyCode.W))
        {
            newPos += new Vector3(this.transform.forward.x,0,this.transform.forward.z) * panSpeed * Time.unscaledDeltaTime;
        } 

        //déplacement arrière
        if(Input.GetKey(KeyCode.S))
        {
             newPos += new Vector3(-this.transform.forward.x,0,-this.transform.forward.z) * panSpeed * Time.unscaledDeltaTime;
        } 

        //déplacement gauche
        if(Input.GetKey(KeyCode.A))
        {
            newPos += -transform.right * panSpeed * Time.unscaledDeltaTime;
        }

        //déplacement droite
        if(Input.GetKey(KeyCode.D))
        {
            newPos += transform.right * panSpeed * Time.unscaledDeltaTime;
        }

        //rotation droite
        if(Input.GetKey(KeyCode.Q))
        {
            transform.Rotate(Vector3.down * rotSpeed * Time.unscaledDeltaTime, Space.World);
        }

        //rotation gauche
        if(Input.GetKey(KeyCode.E))
        {
            transform.Rotate(Vector3.up * rotSpeed * Time.unscaledDeltaTime, Space.World);
        }

        //chopper la molette (molette = un axis de la souris aussi)
        float scroll = Input.GetAxis("Mouse ScrollWheel");

        //zoomer/dezoomer
        newCamPos -= cam.transform.forward * scroll * 1000 * scrollSpeed * Time.unscaledDeltaTime;
        //bloquer la cam entre le min et le max
        // newCamPos.y = Mathf.Clamp(newCamPos.y, minY, maxY);
        newPos.x = Mathf.Clamp(newPos.x, minX, maxX);
        newPos.z = Mathf.Clamp(newPos.z, minZ, maxZ);
        transform.position = newPos;
        cam.transform.localPosition = newCamPos;

        UpdateDrag();
    }

    void UpdateDrag()
    {
        if (Input.GetMouseButtonDown(2))
        {
            dragOrigin = Input.mousePosition;
        }
 
        if (Input.GetMouseButton(2))
        {
            Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
            Vector3 move = new Vector3(pos.x * dragSpeed, 0, pos.y * dragSpeed);
 
            transform.Translate(-move, Space.Self);
            dragOrigin = Input.mousePosition; 
        }
 
    }


}

我只是有一些问题,因为我不能像以前那样限制动作。

// newCamPos.y = Mathf.Clamp(newCamPos.y, minY, maxY);

这不再起作用了,因为移动也会影响 z 轴。

无论如何,我真的很感谢你的帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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