【发布时间】:2017-06-22 22:35:20
【问题描述】:
我正在关注 Unity 文档中的 this 教程,但想更改我的版本以向鼠标位置发射。主机工作正常,但在运行客户端时,子弹会朝着相对于主机客户端的鼠标位置发射(通常就在一边)。
这是我的代码:
public class PlayerMove : NetworkBehaviour {
public GameObject bulletPrefab;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (!isLocalPlayer)
return;
var x = Input.GetAxis("Horizontal") * 0.1f;
var z = Input.GetAxis("Vertical") * 0.1f;
transform.Translate(x, 0, z);
if (Input.GetMouseButtonDown(0))
{
CmdFire();
}
}
public override void OnStartLocalPlayer()
{
GetComponent<MeshRenderer>().material.color = Color.red;
}
[Command]
void CmdFire()
{
// create the bullet object from the bullet prefab
// make the bullet move away in front of the player
Vector2 target = Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y));
Vector2 myPos = new Vector2(transform.position.x, transform.position.y + 1);
Vector2 direction = target - myPos;
GameObject projectile = (GameObject)Instantiate(bulletPrefab, myPos, transform.rotation);
projectile.GetComponent<Rigidbody2D>().velocity = direction * 4f;
// spawn the bullet on the clients
NetworkServer.Spawn(projectile);
// make bullet disappear after 2 seconds
Destroy(projectile, 2.0f);
}
}
我也尝试将“速度 = 方向”移动到新的项目符号脚本,但它总是执行相同的操作。
如果这是这里的解决方案,我如何使用“本地”鼠标位置?
【问题讨论】:
标签: unity3d networking 2d