简体   繁体   English

Unity C#“将精灵位置设置为鼠标指针”使精灵剪辑通过其他精灵

[英]Unity C# 'set sprite position to mouse pointer' makes sprite clip through other sprites

(sorry if formatting or anything else is incorrect, have been using stackoverflow for quite a while, never asked a question before. Plus, I'm new to unity and c#) (很抱歉,如果格式或其他任何内容不正确,已经使用stackoverflow了很长一段时间,之前从未问过问题。此外,我是unity和c#的新手)

I can't post images but here's a link to an imgur 我无法发布图片,但这是指向imgur的链接

https://imgur.com/a/jcki1qd https://imgur.com/a/jcki1qd

(I'm sorry I have lots of trouble describing things and I think the image will help) (很抱歉,我在描述事物时遇到了很多麻烦,我认为图像会有所帮助)

I have a simple code for a circular sprite with a circular spritemap that is attached to a spring, and when I hold the sprite, drag it back, and release my mouse, the object flies in the opposite direction to where I dragged it back to (almost angry birds like launch). 我有一个简单的代码,用于带有连接到弹簧的圆形spritemap的圆形sprite,当我握住sprite并将其向后拖动并释放鼠标时,该对象的飞行方向与将其拖动回的方向相反(几乎愤怒的小鸟喜欢发射)。 But, when I hold the sprite, I am able to clip it through other sprites on my level, and even out of bounds. 但是,当我拿着精灵时,我可以将其裁剪到水平上的其他精灵上,甚至超出范围。 The ball is floating and the play is supposed to be able to drag and launch it in any direction 球是漂浮的,而且比赛应该能够向任意方向拖动和发射

I have tried setting the break force of the spring to a pretty low level, so the ball doesn't launch if I pull it back too far, but the ball still follows my mouse when I drag it around, and when I release it after the spring breaks, the ball just falls down. 我已经尝试将弹簧的断裂力设置得很低,所以如果将它向后拉得太远,球就不会发射,但是当我将其拖动到周围并在释放之后再释放时,球仍然跟随着鼠标春天破了,球刚落下。

Also, I tried limiting the radius of the spring by limiting it's length. 另外,我尝试通过限制弹簧的长度来限制弹簧的半径。 But, it didn't work. 但是,它没有用。

            using System.Collections;
            using System.Collections.Generic;
            using UnityEngine;

            public class LaunchScript : MonoBehaviour
            {
              public Rigidbody2D rb;
              private bool isPressed = false;
              public float releaseTime = 0.15f;

                private void Update()
                {
                    if (isPressed)
                    {
                        rb.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                    }
                }
                // Start is called before the first frame update
                void OnMouseDown()
                {
                    isPressed = true;
                    rb.isKinematic = true;
                }

                // Update is called once per frame
                private void OnMouseUp()
                {
                    isPressed = false;
                    rb.isKinematic = false;
                    StartCoroutine(Release());
                }
                IEnumerator Release()
                {
                    yield return new WaitForSeconds(releaseTime);
                    GetComponent<SpringJoint2D>().enabled = false;
                    this.enabled = false;
                }
        }

My expected results were: the ball would follow the mouse and be able to drag back, but it wouldn't clip through the green ground blocks, the basket, or the border blocks (blocks used to keep ball from flying out of bounds). 我的预期结果是:球将跟随鼠标并能够向后拖动,但不会穿过绿色的地面块,篮筐或边界块(用于阻止球飞出边界的块)。 My actual results were: I could drag the ball to any poin to the screen, including straight into the green ground and out of bounds. 我的实际结果是:我可以将球拖到屏幕上的任何位置,包括直接进入绿色地面和越界。

Kinematic rigidbody collider can detect collision only with rigidbody collider (collider with non-kinematic rigidbody) in non-trigger mode. 运动刚体对撞机只能在非触发模式下检测与刚体对撞机(非运动刚体的对撞机)的碰撞。

From Unity Manual: Colliders : 从Unity 手册:碰撞机

Even when immobile, kinematic rigidbody colliders have different behavior to static colliders. 即使是静止不动的运动刚体对撞机,其行为也与静态对撞机不同。 For example, if the collider is set to as a trigger then you also need to add a rigidbody to it in order to receive trigger events in your script. 例如,如果将对撞机设置为触发器,则还需要向其添加刚体,以便在脚本中接收触发器事件。 If you don't want the trigger to fall under gravity or otherwise be affected by physics then you can set the IsKinematic property on its rigidbody. 如果您不希望触发器落入重力或受到物理影响,则可以在其刚体上设置IsKinematic属性。

Try to play with isKinematic (for example reverse enabling/disabling or remove it) and disable isTrigger. 尝试使用isKinematic(例如,反向启用/禁用或将其删除)并禁用isTrigger。 And don't forget about colliders and rigidbodies on your ball and environment. 并且不要忘记球和环境上的碰撞器和刚体。

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

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