0

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);

}  
4

1 に答える 1

1

何が起こっているのかを説明しようとしています...

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

上記のコードは、スクリーン スペース ((0,0) からスクリーンの幅/高さまでの位置をピクセル単位で測定) にあるマウスの位置をビューポート スペース (同じスクリーンを測定しますが、 (0,0) から (1,1) までの位置を測定します)。

団結して

「ViewportToWorldPoint」を引き続き使用したい場合は、「ScreenToViewportPoint」を実行してから、「ViewPortToWorldPoint」を続けます。

それ以外の場合は、最初から「ScreenToWorldPoint」を使用することを検討できます。

于 2015-10-10T00:36:14.773 に答える