【问题标题】:how to shoot in the mouse pointer angle?如何在鼠标指针角度拍摄?
【发布时间】:2014-12-05 01:20:50
【问题描述】:

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

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);
}
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2017-04-12
      • 1970-01-01
      • 1970-01-01
      • 2012-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-02
      相关资源
      最近更新 更多