简体   繁体   English

敌人跟随玩家旋转

[英]Enemy follow player, rotation


I created Enemy that follow Player and rotate too. 我创建了跟随Player并旋转的敌人。 My problem is that my spaceship model rotation is diffrent than while i was making it with Blender. 我的问题是,与使用Blender进行旋转时相比,我的飞船模型旋转不同。
This is how Enemy follow and how ship look like with rotation X:0 Y:0 Z: 0 这就是敌人的跟随方式以及旋转X:0 Y:0 Z:0时的船形
http://i.stack.imgur.com/bLbaz.png http://i.stack.imgur.com/bLbaz.png
In Blender, everything is fine, nothing wrong with rotation etc. 在Blender中,一切都很好,旋转没有问题,等等。
This is Enemy script 这是敌人的剧本

using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {

        public static float health;
        private float reloadTime;

        public Rigidbody laser;
        public GameObject explo;
        public Transform playerShip;

        // Use this for initialization
        void Start () {
                health = 20.0f;
                reloadTime = 0.3f;
        }

        // Update is called once per frame
        void Update () {

                //transform.LookAt(playerShip.transform.position);
                Quaternion rotation = Quaternion.LookRotation(playerShip.transform.position - this.transform.position);
                transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 2);
                transform.Translate(Vector3.forward * 2 * Time.deltaTime);

                reloadTime -= Time.deltaTime;

                if(reloadTime <= 0f)
                {
                        Rigidbody clone = Instantiate(laser, transform.position, transform.rotation) as Rigidbody;
                        clone.velocity = transform.TransformDirection(0, 0, 80);
                        Destroy(clone.gameObject, 3);
                        reloadTime = 0.3f;
                }

                if(health <= 0f)
                {
                        GameObject exp = Instantiate(explo, transform.position, transform.rotation) as GameObject;
                        Destroy(this.gameObject);
                        Destroy(exp.gameObject, 1.5f);
                }
        }
}

What can be wrong with this rotations? 这种轮换有什么问题?

How can I change rotation in this code so X will be always 270? 如何更改此代码的旋转度,使X始终为270?

            Quaternion rotation = Quaternion.LookRotation(playerShip.transform.position - this.transform.position);
            transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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