Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从主摄像机拍摄射弹时左右瞄准_C#_Unity3d - Fatal编程技术网

C# 从主摄像机拍摄射弹时左右瞄准

C# 从主摄像机拍摄射弹时左右瞄准,c#,unity3d,C#,Unity3d,我有一个脚本设置,这样我可以从摄像机的角度拍摄无限的预制件,但球直接拍摄。。。我不确定我的脚本是否需要调整,或者主摄像机上是否有需要调整的检查器设置。基本上,如果我的光标在左边,我希望球朝那个方向射击,而不是在屏幕中间。p> using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Person : MonoBehaviour

我有一个脚本设置,这样我可以从摄像机的角度拍摄无限的预制件,但球直接拍摄。。。我不确定我的脚本是否需要调整,或者主摄像机上是否有需要调整的检查器设置。基本上,如果我的光标在左边,我希望球朝那个方向射击,而不是在屏幕中间。p>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Person : MonoBehaviour {

public GameObject projectilePrefab;
public Text countText;

private int count;
void OnGUI(){
    GUI.Box(new Rect(Screen.width/2,Screen.height/2, 10, 10), "");
}

// Use this for initialization
void Start () {
    count = 0;
    setCountText();

}

private void setCountText()
{
    countText.text = "Ammo Fired: " + count;
}

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

}

void LateUpdate()
{
    float x = Input.GetAxis("Mouse X") * 2;
    float y = Input.GetAxis("Mouse Y");

    float yClamped = transform.eulerAngles.x + y;

    transform.rotation = Quaternion.Euler(yClamped,
        transform.eulerAngles.y, transform.eulerAngles.z);

    transform.RotateAround(new Vector3(0, 3, 0), Vector3.up, x);
}

void FixedUpdate()
{
    if (Input.GetButtonDown("Fire2")) //Right click. use Fire1 for left click
    {
        GameObject projectile = Instantiate(projectilePrefab, 
            transform.position, transform.rotation) as GameObject;
        Rigidbody rb = projectile.GetComponent<Rigidbody>();
        rb.AddRelativeForce(Vector3.forward * 500000);

        count++;
        setCountText();
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共阶层人士:单一行为{
公共游戏对象投影;
公共文本;
私人整数计数;
void OnGUI(){
GUI.Box(新矩形(Screen.width/2,Screen.height/2,10,10),“”);
}
//用于初始化
无效开始(){
计数=0;
setCountText();
}
私有void setCountText()
{
countText.text=“发射的弹药:”+计数;
}
//每帧调用一次更新
无效更新(){
}
void LateUpdate()
{
float x=Input.GetAxis(“鼠标x”)*2;
float y=Input.GetAxis(“鼠标y”);
float yClamped=transform.eulerAngles.x+y;
transform.rotation=四元数.Euler(循环采样,
transform.eulerAngles.y,transform.eulerAngles.z);
transform.RotateAround(新向量3(0,3,0),向量3.up,x);
}
void FixedUpdate()
{
if(Input.GetButtonDown(“Fire2”)//右键单击。使用Fire1进行左键单击
{
游戏对象投射=实例化(ProjectlePrefab,
变换。位置,变换。旋转)作为游戏对象;
刚体rb=射弹。GetComponent();
rb.添加相对力(矢量3.0*500000);
计数++;
setCountText();
}
}

}扎克。您将需要使用新预制件的前进位置

projectile.GetComponent<Transform>().forward
sparket.GetComponent().forward
与使用编辑器/世界前进位置相反。

使用:

projectile.GetComponent<Transform>().forward
rb.AddRelativeForce(projectile.transform.forward * 500000);

实际上,
sparket.transform.forward
将为您提供刚刚生成的投射物的前进方向,而
Vector3.forward
为您提供世界的前进方向(它基本上只是指向Vector3(0,0,1)的快捷方式)

你知道为什么这张纸条在vuforia使用AR摄像头时不起作用,但在常规的统一环境下会起作用吗