unity3d 中的 2d 自上而下射击游戏中的旋转控制有问题
something wrong with rotation controls in 2d top down shooter in unity3d
我试图通过绕鼠标的 y 轴旋转让我的角色面向鼠标。它旋转了一点,但不面向鼠标。相机的位置在玩家的正上方,在正交视图中我看到许多其他自上而下射击的例子,几乎和我的一模一样所以我不知道是什么问题请帮助谢谢。
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);
}
将尝试解释发生了什么...
target = camera.ViewportToWorldPoint (new Vector3(Input.mousePosition.x,Input.mousePosition.y,camera.transform.position.y - transform.position.y));
上面的代码试图将屏幕 Space 中的鼠标位置(以像素为单位测量从 (0,0) 到屏幕 width/height 的位置)转换为Viewport Space(测量相同的屏幕但使用不同的测量,它测量从(0,0)到(1,1)的位置)。
在 Unity 中
- 屏幕Space测量:原点(0,0)到最大值width/height
- 视口测量:原点 (0,0) 到 (1,1)
- 参考:http://docs.unity3d.com/ScriptReference/Camera.html
如果您仍希望使用 "ViewportToWorldPoint",则可以执行 "ScreenToViewportPoint",然后在后面加上 "ViewPortToWorldPoint"。
否则,您可以从一开始就考虑使用 "ScreenToWorldPoint"。
我试图通过绕鼠标的 y 轴旋转让我的角色面向鼠标。它旋转了一点,但不面向鼠标。相机的位置在玩家的正上方,在正交视图中我看到许多其他自上而下射击的例子,几乎和我的一模一样所以我不知道是什么问题请帮助谢谢。
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);
}
将尝试解释发生了什么...
target = camera.ViewportToWorldPoint (new Vector3(Input.mousePosition.x,Input.mousePosition.y,camera.transform.position.y - transform.position.y));
上面的代码试图将屏幕 Space 中的鼠标位置(以像素为单位测量从 (0,0) 到屏幕 width/height 的位置)转换为Viewport Space(测量相同的屏幕但使用不同的测量,它测量从(0,0)到(1,1)的位置)。
在 Unity 中
- 屏幕Space测量:原点(0,0)到最大值width/height
- 视口测量:原点 (0,0) 到 (1,1)
- 参考:http://docs.unity3d.com/ScriptReference/Camera.html
如果您仍希望使用 "ViewportToWorldPoint",则可以执行 "ScreenToViewportPoint",然后在后面加上 "ViewPortToWorldPoint"。
否则,您可以从一开始就考虑使用 "ScreenToWorldPoint"。