C# 如何在鼠标指针角度拍摄?
我有一个玩家,用子弹向敌人射击,子弹向右移动,以正确的角度,但我的子弹没有指向那个角度,子弹无法改变它的角度。如何改变它?它不仅应该以那个角度移动,而且应该指向它,目前我正在将其转换到屏幕右侧。敌人正在从右侧繁殖。这是我的移动和转换代码,有任何帮助吗, 这是指示方向和射击速度的代码C# 如何在鼠标指针角度拍摄?,c#,unity3d,C#,Unity3d,我有一个玩家,用子弹向敌人射击,子弹向右移动,以正确的角度,但我的子弹没有指向那个角度,子弹无法改变它的角度。如何改变它?它不仅应该以那个角度移动,而且应该指向它,目前我正在将其转换到屏幕右侧。敌人正在从右侧繁殖。这是我的移动和转换代码,有任何帮助吗, 这是指示方向和射击速度的代码 using UnityEngine; using System.Collections; public class WeaponScript : MonoBehaviour { public Transform
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;
}
}
}
使用transform.LookAttransform.position+方向将立即将对象指向指定的方向
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);
}
}