如何以鼠标指针角度拍摄

本文关键字:鼠标指针 | 更新日期: 2023-09-27 18:33:06

我有一个玩家用子弹向敌人射击,子弹以正确的角度向右移动,但我的子弹没有指向那个角度,子弹无法改变它的角度,如何改变它,它不仅应该向那个角度移动,而且还应该指向它,目前我正在将其变换到屏幕右侧,敌人正在从右侧生成这里是我的移动和转换代码,任何帮助都感谢,这是方向和射门率的代码

using UnityEngine;
using System.Collections;
public class WeaponScript : MonoBehaviour
{
public Transform shotPrefab;
public float shootingRate = 0.25f;
private float shootCooldown;
void Start()
{
    shootCooldown = 0f;
}
void Update()
{
    if (shootCooldown > 0)
    {
        shootCooldown -= Time.deltaTime;
    }
}
public void Attack(bool isEnemy)
{
    if (CanAttack)
    {
        shootCooldown = shootingRate;
        // Create a new shot
        var shotTransform = Instantiate(shotPrefab) as Transform;
        // Assign position
        shotTransform.position = transform.position;
        // The is enemy property
        ShotScript shot = shotTransform.gameObject.GetComponent<ShotScript>();
        if (shot != null)
        {
            shot.isEnemyShot = isEnemy;
        }
        // Make the weapon shot always towards it
        MoveScript move = shotTransform.gameObject.GetComponent<MoveScript>();
        if (move != null)
        {
            move.direction = this.transform.right;
        }
    }
}
public bool CanAttack
{
    get
    {
        return shootCooldown <= 0f;
    }
}
}

这是移动的代码

using UnityEngine;
using System.Collections;
public class MoveScript : MonoBehaviour {
public Vector2 speed = new Vector2(10,10);
public Vector2 direction = new Vector2(1,0);
void Update () {
    Vector3 movement = new Vector3 (speed.x * direction.x, speed.y * direction.y, 0);
    movement *= Time.deltaTime;
    transform.Translate(movement);
}
}

如何以鼠标指针角度拍摄

使用 transform.LookAt(transform.position + direction) 将立即将对象指向指定方向。