简体   繁体   中英

how to shoot in the mouse pointer angle?

I have a player which is shooting with bullets to the enemy,the bullets are moving towards the right,in a correct angle,but my bullet is not pointing towards that angle,the bullets is unable to change its angle.,how to change it?,it should not only move in that angle but also point towards it,currently i am transforming it to right of the screen.,the enemy are spawning from the right.here is my code for movement and transformation,any help thanx, this is the code for direction,and for the shooting rate

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

this is the code for movement

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)将立即将您的对象指向指定的方向。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM