C# 团结在前进中旋转
我有我的2D游戏的以下代码,它使物体在屏幕上随机产生惊奇。我遇到的问题是,当一个物体看到它将要到达的点时,我希望它在向前移动时旋转。现在发生的是,它会立即旋转到它要去的地方。那么,我怎样才能让它慢慢旋转,同时向前移动呢C# 团结在前进中旋转,c#,rotation,unity3d,C#,Rotation,Unity3d,我有我的2D游戏的以下代码,它使物体在屏幕上随机产生惊奇。我遇到的问题是,当一个物体看到它将要到达的点时,我希望它在向前移动时旋转。现在发生的是,它会立即旋转到它要去的地方。那么,我怎样才能让它慢慢旋转,同时向前移动呢 using UnityEngine; using System.Collections; public class Wonder : MonoBehaviour { protected Vector2 wayPoint; protected float spe
using UnityEngine;
using System.Collections;
public class Wonder : MonoBehaviour {
protected Vector2 wayPoint;
protected float speed;
// Use this for initialization
void Start () {
speed = gameObject.GetComponent<Move>().playerSpeed;
wonder();
}
void wonder(){
wayPoint = Random.insideUnitCircle * 10;
}
// Update is called once per frame
void Update () {
Vector2 dir = wayPoint - new Vector2(transform.position.x, transform.position.y);
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0,Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg - 90));
transform.position = Vector2.MoveTowards(transform.position, wayPoint, Time.deltaTime * speed);
float magnitude = (new Vector2(transform.position.x, transform.position.y) - wayPoint).magnitude;
if(magnitude < 3){
wonder();
}
}
}
使用UnityEngine;
使用系统集合;
公共阶层奇迹:单一行为{
受保护矢量2航路点;
保护浮动速度;
//用于初始化
无效开始(){
速度=gameObject.GetComponent().playerSpeed;
奇迹();
}
void wonder(){
航路点=Random.InsideuUnitCircle*10;
}
//每帧调用一次更新
无效更新(){
Vector2 dir=航路点-新矢量2(transform.position.x,transform.position.y);
浮动角度=数学Atan2(方向y,方向x)*数学Rad2Deg;
transform.rotation=Quaternion.Euler(新向量3(0,0,Mathf.Atan2(dir.y,dir.x)*Mathf.Rad2Deg-90));
transform.position=矢量2.移动方向(transform.position、航路点、Time.deltaTime*速度);
浮动幅度=(新矢量2(transform.position.x,transform.position.y)-航路点)。幅度;
如果(震级<3){
奇迹();
}
}
}
下面是一个示例图像:
因此,一旦飞船到达它的位置,另一艘飞船就会被创造出来,并移动到那里。我想我必须有一个5+点的列表,然后计算船需要的拱度,当船到达一个航路点时添加新的点,然后删除旧的点。但我不知道如何使用UnityEngine实现这一点……;
using UnityEngine;
using System.Collections;
public class Wander : MonoBehaviour {
protected Vector3 velocity;
protected Vector2 waypoint;
protected float speed;
// Use this for initialization
void Start () {
speed = gameObject.GetComponent<Move>().playerSpeed;
RandomizeWaypoint();
}
void RandomizeWaypoint(){
waypoint = Random.insideUnitCircle * 10;
}
// Update is called once per frame
void Update () {
transform.position = Vector3.SmoothDamp( transform.position, waypoint, ref velocity, Time.deltaTime * speed );
transform.rotation = Quaternion.AngleAxis( Mathf.Atan2( velocity.y, velocity.x ) * Mathf.Rad2Deg, Vector3.forward );
if( Vector3.Distance( transform.position, waypoint ) < 3 ){
RandomizeWaypoint();
}
}
}
使用系统集合;
公共阶层游荡:单一行为{
保护矢量3速度;
受保护矢量2航路点;
保护浮动速度;
//用于初始化
无效开始(){
速度=gameObject.GetComponent().playerSpeed;
随机点();
}
void RandomizeWaypoint(){
航路点=Random.InsideuUnitCircle*10;
}
//每帧调用一次更新
无效更新(){
transform.position=矢量3.SmoothDamp(transform.position、航路点、参考速度、Time.deltaTime*速度);
transform.rotation=四元数角度轴(数学Atan2(速度y,速度x)*数学Rad2Deg,矢量3.forward);
if(矢量3.距离(变换位置、航路点)<3){
随机点();
}
}
}
未经测试。Vector3.SmoothDamp非常方便。还要注意拼写。@Ryan最终会明白这一点吗?