如何解决旋转关闭 90 度统一
我正在尝试围绕鼠标旋转我的 2d 对象。这是我的代码:
extension String {
subscript(_ range: CountableRange<Int>) -> String {
let start = index(startIndex,offsetBy: max(0,range.lowerBound))
let end = index(start,offsetBy: min(self.count - range.lowerBound,range.upperBound - range.lowerBound))
return String(self[start..<end])
}
subscript(_ range: CountablePartialRangeFrom<Int>) -> String {
let start = index(startIndex,range.lowerBound))
return String(self[start...])
}
func highlight(substring: String) -> String {
let stringWithoutCaseOrLetters = self.folding(options: .diacriticInsensitive,locale: .current)
if let range = lowercased()
.folding(options: .diacriticInsensitive,locale: .current)
.range(of: substring
.lowercased()
.folding(options: .diacriticInsensitive,locale: .current)) {
let startPos:Int = stringWithoutCaseOrLetters.distance(from: startIndex,to: range.lowerBound)
let endPos:Int = stringWithoutCaseOrLetters.distance(from: startIndex,to: range.upperBound)
let firstSubstring = self[0..<startPos]
let secondSubstring = self[startPos..<endPos]
let thirdSubstring = self[endPos...]
return "\(firstSubstring)[\(secondSubstring)]\(thirdSubstring)"
}
return ""
}
}
extension StringProtocol {
func distance(of element: Element) -> Int? { firstIndex(of: element)?.distance(in: self) }
func distance<S: StringProtocol>(of string: S) -> Int? { range(of: string)?.lowerBound.distance(in: self) }
}
extension Collection {
func distance(to index: Index) -> Int { distance(from: startIndex,to: index) }
}
extension String.Index {
func distance<S: StringProtocol>(in string: S) -> Int { string.distance(to: self) }
}
let string = "Curaçao"
print(string[0..<string.count])
print(string.highlight(substring: "aca"))
print("Curaçaoçao".highlight(substring: "acaoca"))
我的对象是一个箭头,默认情况下是向下的,所以在我开始脚本之前我将它的 z 旋转设置为 90,所以它面向右。如果我删除了 transform.rotation 线,显示的角度将是对,当我的光标在上面时它说 90,在左边它说 180 等等。所以我的问题是,为什么我需要将角度添加 90 度才能使其真正起作用?为什么这个版本不起作用?谢谢!
解决方法
Mathf.Atan2
(另见Wikipedia - Atan2)
返回值是 x 轴之间的夹角 [=Vector3.right
] 和一个从零开始到 (x,y) 终止的二维向量。
如果默认情况下您的箭头指向右侧,但您的箭头指向右侧,则它会按预期工作
默认向下
你可以简单地在顶部添加偏移旋转
transform.rotation = Quaternion.Euler(0,angle + 90);
或者,如果您需要为具有不同偏移量的多个对象使用此选项,请使用可配置字段,如
[SerializeField] private float angleOffset;
...
transform.rotation = Quaternion.Euler(0,angle + angleOffset);
或者您可以在启动应用程序之前手动旋转它以正确面向右侧并存储默认偏移旋转,如
private Quaternion defaultRotation;
private void Awake ()
{
defaultRotation = transform.rotation;
}
然后做
transform.rotation = defaultRotation * Quaternion.Euler(0,angle);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。