javascript - 角色旋转并快速移动到鼠标点击点

标签 javascript c# unity-game-engine rotation geometry

我需要在 2D 游戏项目中模拟 Angular 色的向前旋转。

我尝试创建我的 ActionScript ,但效果并不完美。原因是我的 2D actor 是宇宙飞船,它无法绕轴旋转,但我只需要向前旋转。

enter image description here

这是该运动的两个部分。

  • Actor 必须前进并旋转才能返回(半径必须是最佳的或预先定义的)。
  • Actor 必须使用鼠标点击位置沿线直线前进。

所以。我使用Unity引擎。需要一些解释才能发布它。

谢谢!

UPD:我当前的源代码:

using UnityEngine;
using System.Collections;

public class Move : MonoBehaviour {

	public float speed = 1.5f;
	public float rotationSpeed = 90f;
	private Vector3 pos;
	private Quaternion qTo;

	void Start () {
		pos = transform.position;
		qTo = transform.rotation;
	}

	void Update () {
		if (Input.GetMouseButtonDown(0) || Input.GetMouseButton(0)) {
			pos = Input.mousePosition;
			pos.z = transform.position.z - Camera.main.transform.position.z;
			pos = Camera.main.ScreenToWorldPoint(pos);
		}

		var dir = pos - transform.position;

		if (dir != Vector3.zero) {
			qTo = Quaternion.LookRotation(Vector3.forward, pos - transform.position);
			transform.rotation = Quaternion.RotateTowards (transform.rotation, qTo, Time.deltaTime * rotationSpeed);
		}

		transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
	}    

}

enter image description here

最佳答案

试试这个:

var dir = pos - transform.position;
qTo = Quaternion.LookRotation(Vector3.forward, pos - transform.position);

if (Quaternion.Angle(transform.rotation, qTo) >= rotPrecision) //just set your own precision
    transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, Time.deltaTime * rotationSpeed);

if(Vector3.Distance(transform.position,pos) > movePrecision) // 0.1f
    transform.Translate(Vector3.up * speed * Time.deltaTime);

关于javascript - 角色旋转并快速移动到鼠标点击点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39627836/

相关文章:

javascript - Google Chrome 扩展 - chrome.storage.sync.get 不起作用

javascript - jQuery $.each() 多维 JSON 数组

c# - 使用 Pure Razor 时的动态 Web 控件

unity-game-engine - 如何在 Unity C# 中单击时从 firebase 云消息传递打开特定场景

unity-game-engine - 统一错误CS0120

ios - 错误: Undefined symbols for architecture arm64 when Embed Unity(Vuforia) in IOS project(swift)

javascript - 有没有理由在 Internet Explorer 中用 Script 替换 JavaScript?

javascript - 设置选中的菜单项类 ="selected"服务器端还是使用 JavaScript 更专业?

c# - 使用 XmlDocument 向元素添加命名空间的正确方法

c# - 在 Express Checkout 审核页面中显示订单项