简体   繁体   English

团结-需要球形重力帮助吗?

[英]Unity - Help needed with spherical gravity?

I am working on a game which has strong base on spherical gravity. 我正在开发一款基于球重力的游戏。 But somehow my code is not working as expected. 但是以某种方式我的代码无法按预期工作。 So please have a look at my code and tell me how can I make my spherical gravity work. 因此,请看一下我的代码,并告诉我如何使球形重力起作用。

public class CircularGravity : MonoBehaviour {
    private Rigidbody2D rigid;
    [SerializeField]
    Transform planet;

    [SerializeField]
    float acceleration = 0.81f;
    // Use this for initialization
    void Start () {
        rigid = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update () {
        rigid.AddForce((planet.position - transform.position).normalized * acceleration);
        transform.rotation = Quaternion.LookRotation(planet.position - transform.position, transform.up);
    }
}

在此处输入图片说明 Problem elaboration. 问题的阐述。 Project is 2d. 项目为2天。 it contains a circle sprite as a planet with a collider and hexagon has a player(just a prototype) with collider and rigidbody. 它包含一个圆形的精灵,作为一个带有碰撞器的行星,六角形具有一个带有碰撞器和刚体的播放器(只是原型)。 This script is attached to the player(hexagon). 该脚本附加在播放器(六边形)上。 According to my logic rigidbody should apply a force to the player and push it towards the planet and it should face the planet. 根据我的逻辑,刚体应向玩家施加力并将其推向行星,并且它应面向行星。 So even if the player is on the downward side of circle it shouldnot fall instead it shall be pushed toward the planet. 因此,即使玩家位于圆圈的下方,它也不应掉落,而应将其推向行星。 But all the colliders are being neglected and player is just going to strange position 但是所有的对撞机都被忽略了,而玩家正处于一个奇怪的位置

First things I would check... 我要检查的第一件事...

  • Make sure the origin of your sprite is actually in the center of the image. 确保精灵的原点实际上位于图像的中心。 If it's in the corner the player will be pulled toward the corner. 如果在角落,玩家将被拉向角落。

  • Make sure both the planet and player have rigid bodies and colliders. 确保行星和玩家都具有刚体和对撞机。 Click on the collider and make sure the edge lines up with you shapes. 单击对撞机,并确保边缘与您的形状对齐。

  • Make sure both the planet and the player have their z set to zero. 确保星球和玩家的z都设置为零。 Lock z in both rigidbodies. 将z锁定在两个刚体中。

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

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