英文:
(SOLVED) How do I make a throwing (using drag) system for items in my 2D game?
问题
I've looked near and far but I can't find a definitive answer to how I can do this. I'm using image GameObjects with dynamic Rigidbodies and 2D colliders. The dragging function works well but I can't get the items to fly off into the distance whenever you swipe the item. Here is my drag-and-drop code:
[SerializeField] private Canvas canvas;
private RectTransform rectTransform;
private Rigidbody2D rigidbody;
private void Awake() {
rectTransform = GetComponent<RectTransform>();
rigidbody = GetComponent<Rigidbody2D>();
}
public void OnBeginDrag(PointerEventData eventData) {
rigidbody.gravityScale = 0;
}
public void OnDrag(PointerEventData eventData) {
rectTransform.anchoredPosition += eventData.delta / canvas.scaleFactor;
}
public void OnEndDrag(PointerEventData eventData) {
rigidbody.gravityScale = 50;
}
(sorry for the indent)
I tried this over and over with springs and stuff, and I thought it would throw the item across the screen when you swiped, but it either gives me irrelevant errors or makes no change. Please help!
英文:
I've looked near and far but I can't find a definitive answer to how I can do this. I'm using image GameObjects with dynamic Rigidbodies and 2D colliders. The dragging function works well but I can't get the items to fly off into the distance whenever you swipe the item. Here is my drag-and-drop code:
[SerializeField] private Canvas canvas;
private RectTransform rectTransform;
private Rigidbody2D rigidbody;
private void Awake() {
rectTransform = GetComponent<RectTransform>();
rigidbody = GetComponent<Rigidbody2D>();
}
public void OnBeginDrag(PointerEventData eventData) {
rigidbody.gravityScale = 0;
}
public void OnDrag(PointerEventData eventData) {
rectTransform.anchoredPosition += eventData.delta / canvas.scaleFactor;
}
public void OnEndDrag(PointerEventData eventData) {
rigidbody.gravityScale = 50;
}
(sorry for the indent)
I tried this over and over with springs and stuff, and I thought it would throw the item across the screen when you swiped, but it either gives me irrelevant errors or makes no change. Please help!
答案1
得分: 1
已解决!以下是我的完整脚本。我会注释我所做的任何更改。
[SerializeField] private Canvas canvas;
private RectTransform rectTransform;
private Rigidbody2D rigidbody2d;
private SpringJoint2D _spring; // 只是对弹簧的引用
private void Awake() {
rectTransform = GetComponent<RectTransform>();
rigidbody2d = GetComponent<Rigidbody2D>();
_spring = GetComponent<SpringJoint2D>(); // 将弹簧设置为引用的弹簧
//我放在我的游戏对象上
_spring.connectedAnchor = rectTransform.position; // 将弹簧的连接点设置为我的物体位置
}
public void OnBeginDrag(PointerEventData eventData) {
rigidbody2d.gravityScale = 0;
_spring.enabled = true; // 启用弹簧(跟踪移动)
}
public void OnDrag(PointerEventData eventData) {
if(_spring.enabled == true) { // 如果弹簧启用...
Vector2 mousePos = Input.mousePosition; // ... 获取鼠标在世界坐标中的位置 ...
_spring.connectedAnchor = mousePos; // .... 并将弹簧的连接点设置为mousePos变量。
}
}
public void OnEndDrag(PointerEventData eventData) {
rigidbody2d.gravityScale = 50;
_spring.enabled = false; // 禁用弹簧。
}
这是我的弹簧设置:
启用碰撞为false。
没有连接的刚体。
不自动配置连接锚点。
锚点是0,0。
(不用担心连接的锚点。这不重要。)
自动配置距离为false。
距离是0.005(最小值)。
阻尼是0。
频率是2。
BF是无穷大。
就是这样!
英文:
SOLVED! Here's my full script. I'll comment anything that I've changed.
[SerializeField] private Canvas canvas;
private RectTransform rectTransform;
private Rigidbody2D rigidbody2d;
private SpringJoint2D _spring; // Just a reference to a spring
private void Awake() {
rectTransform = GetComponent<RectTransform>();
rigidbody2d = GetComponent<Rigidbody2D>();
_spring = GetComponent<SpringJoint2D>(); // Set the spring to the spring
//I put on my GameObject
_spring.connectedAnchor = rectTransform.position; // Set the spring's
//connected anchor to my object's position
}
public void OnBeginDrag(PointerEventData eventData) {
rigidbody2d.gravityScale = 0;
_spring.enabled = true; // Enable the spring (to track movement)
}
public void OnDrag(PointerEventData eventData) {
if(_spring.enabled == true) { // If the spring is on...
Vector2 mousePos = Input.mousePosition; // ... Get the mouse position
in world coordinates ...
_spring.connectedAnchor = mousePos; // .... and set the spring's
connected anchor to the mousePos variable.
}
}
public void OnEndDrag(PointerEventData eventData) {
rigidbody2d.gravityScale = 50;
_spring.enabled = false; // Disable the spring.
}
Here are my spring settings:
Enable collision is false.
No connected Rigidbodies.
Don't auto-config connected anchor.
anchor is 0, 0.
(Don't worry about the connected anchor. It doesn't matter.)
Auto-config distance is false.
distance is 0.005 (minimum).
Damping is 0.
Frequency is 2.
BF is infinity.
And that's all!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论