求各位大神解答一下:
我转动摄像头时,射出的球一直是初始角度,没有随着镜头转动而改变射击方向。
代码如下:
using UnityEngine;
using System.Collections;
public class ffc : MonoBehaviour {
// Use this for initialization
void Start () {
}
public int speed=5;
public int jiao=25;
public Transform newobject;
// Update is called once per frame
void Update () {
float x=Input.GetAxis("Horizontal")*Time.deltaTime*speed; //获取x轴移动;
float z=Input.GetAxis("Vertical")*Time.deltaTime*speed; //获取z轴移动;
transform.Translate (x, 0, z); //根据获取值移动物体。
//视角选择
//左旋转
if(Input.GetKey(KeyCode.Q)){
transform.Rotate (0,-jiao*Time.deltaTime,0,Space.Self);
}
//右旋转
if(Input.GetKey(KeyCode.E)){
transform.Rotate (0,jiao*Time.deltaTime,0,Space.Self);
}
//点击时发射球体
if (Input.GetButtonDown ("Fire1")) {
Transform n = Instantiate(newobject,transform.position,transform.rotation)as Transform;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
n.GetComponent<Rigidbody>().AddForce(Vector3.fwd*2800);
}
}
}
我转动摄像头时,射出的球一直是初始角度,没有随着镜头转动而改变射击方向。
代码如下:
using UnityEngine;
using System.Collections;
public class ffc : MonoBehaviour {
// Use this for initialization
void Start () {
}
public int speed=5;
public int jiao=25;
public Transform newobject;
// Update is called once per frame
void Update () {
float x=Input.GetAxis("Horizontal")*Time.deltaTime*speed; //获取x轴移动;
float z=Input.GetAxis("Vertical")*Time.deltaTime*speed; //获取z轴移动;
transform.Translate (x, 0, z); //根据获取值移动物体。
//视角选择
//左旋转
if(Input.GetKey(KeyCode.Q)){
transform.Rotate (0,-jiao*Time.deltaTime,0,Space.Self);
}
//右旋转
if(Input.GetKey(KeyCode.E)){
transform.Rotate (0,jiao*Time.deltaTime,0,Space.Self);
}
//点击时发射球体
if (Input.GetButtonDown ("Fire1")) {
Transform n = Instantiate(newobject,transform.position,transform.rotation)as Transform;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
n.GetComponent<Rigidbody>().AddForce(Vector3.fwd*2800);
}
}
}