简体   繁体   English

unity3d中2d自上而下的射击游戏中的旋转控件出了点问题

[英]something wrong with rotation controls in 2d top down shooter in unity3d

I am trying to get my character to face the mouse by rotating on its y axis. 我试图通过在y轴上旋转来使角色面对鼠标。 It rotates a little, but it do not face the mouse. 它会旋转一点,但不会面对鼠标。 The Camera's position is directly above the player, and its in orthographic view I see many other examples of top down shooters and there almost exactly like mine so i don't know whats the problem please help thank you. 相机的位置直接在播放器上方,在正交视图中,我看到了许多自上而下的射击游戏的例子,那里几乎和我的完全一样,所以我不知道出了什么问题,请帮忙谢谢。

using UnityEngine;
using System.Collections;

public class playermovementscript : MonoBehaviour { 

public float movementspeed = 5f; 
public float bulletspeed  = 10f;
public GameObject bullet; 
public GameObject shooter;  
public Vector3 target;  
public Camera camera;

// Use this for initialization
void Start () {  
    //refrence to main camera
    Camera camera;

}

// Update is called once per frame
void Update () {

    //call movement function in every frame
    movement (); 

    // cheek for input of f in every frame if true execute shoot function
    if (Input.GetKeyDown (KeyCode.F)) {
        shoot();
    } 

 } 




void movement(){

    // makes refrence to gameobjects rigidbody and makes its velocity varible to equal values of axis x and y
    gameObject.GetComponent<Rigidbody>().velocity = new Vector3(Input.GetAxisRaw("Horizontal")*movementspeed,0,Input.GetAxisRaw("Vertical")*movementspeed); 

// makes vector 3 type target equall to mouse position on pixial cordinates converted in to real world coordniates
    target = camera.ViewportToWorldPoint (new Vector3(Input.mousePosition.x,Input.mousePosition.y,camera.transform.position.y - transform.position.y)); 

    //target.x -= transform.position.x; 
    //target.z -= transform.position.z;
    // states the vector 3 values of target 
    Debug.Log (target);  
    // makes object local z face target and iniziates up axis
    transform.LookAt (target,Vector3.up);

}  

Going to make an attempt to explain what's going on... 试图解释发生了什么...

target = camera.ViewportToWorldPoint (new Vector3(Input.mousePosition.x,Input.mousePosition.y,camera.transform.position.y - transform.position.y)); 

The above code is trying to convert a mouse position which is in Screen Space (which measures the position from (0,0) to the width/height of the screen in pixels) to a Viewport Space (which measure the same screen but with a different measure, it measures the positions from (0,0) to (1,1)). 上面的代码试图将屏幕空间中的鼠标位置(该位置测量从(0,0)到屏幕的宽度/高度(以像素为单位)的位置)转换为视口空间(其测量相同的屏幕,但具有不同的度量,它度量从(0,0)到(1,1)的位置。

In Unity 在Unity中

If you desire to still use "ViewportToWorldPoint" then you could do a "ScreenToViewportPoint" then follow it with a "ViewPortToWorldPoint". 如果您仍然希望使用“ ViewportToWorldPoint”,则可以执行“ ScreenToViewportPoint”,然后再跟随“ ViewPortToWorldPoint”。

Otherwise, you could look into using "ScreenToWorldPoint" from the start. 否则,您可以从一开始就考虑使用“ ScreenToWorldPoint”。

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

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