1.根据之前的枪械朝向鼠标,程序是
我的枪械素材是如下图:
主体程序如下:
//枪械运动
private Vector2 mousePos;
private Vector2 direction;
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
direction = (mousePos - new Vector2(transform.position.x, transform.position.y)).normalized;
transform.right = direction;
2.实例出子弹后朝向认为也是如此(我子弹素材朝向也是和枪械一样)
即本以为代码:
//子弹朝向
private Vector2 mousePos;
private Vector2 direction1;
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
GameObject bullet = Instantiate(bulletPerfab, muzzlePos.position, Quaternion.identity);
direction1 = (mousePos - new Vector2(bullet.transform.position.x,bullet.transform.position.y)).normalized;
bullet.transform.right = direction1;
但是有问题,最后使用的程序是
//子弹朝向
private Vector2 mousePos;
private Vector2 direction1;
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
GameObject bullet = Instantiate(bulletPerfab, muzzlePos.position, Quaternion.identity);
direction1 = (mousePos - new Vector2(bullet.transform.position.x, bullet.transform.position.y)).normalized;
float angel = Mathf.Atan2(direction1.y, direction1.x) * Mathf.Rad2Deg;
bullet.transform.rotation = Quaternion.AngleAxis(angel, Vector3.forward);