统一3.3,在物体上产生局部重力 [英] unity 3.3, create local gravity on object

查看:105
本文介绍了统一3.3,在物体上产生局部重力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Unity 3.3. Visual Studio Express 2010和C#.

Using Unity 3.3. visual studio express 2010. and c#.

我需要制作一个将附加到对象的脚本. 该对象将创建自己的重力场,该重力场将吸引环境中的所有对象.

I need to make a script that will be attached to an object. This object will create its own gravity field that will attract- repel all objects in the environment.

不幸的是,由于物理原因,我不确定如何执行此操作.我需要该对象覆盖所有其他对象吗?我是否可以超越统一的主要物理原理?这项工作如何? 还是当它是一个简单的脚本时,我会使事情变得过于复杂吗?

Unfortunately I'm not sure how to do this due to unitys physics. would I need the object to overide all other objects? do I overide unitys main physics ? hows this work? Or am I overcomplicating things when its a simple script?

不,我不想添加rigig主体以使对象随重力下落,它必须创建更强大的重力.

No I do not want to add a rigig body so the object falls with gravity, it must CREATE gravity that is a lot more powerfull.

欢呼

更新!

public float radius = 5.0f;
public float power = -20f;
void Start () 
{
    //GameObject gravityObject = GameObject.FindGameObjectWithTag("GravitySwell");
    //gravSlime = gravityObject.transform;      
}

void Update () 
{
    Vector3 explosionPos = transform.position;
    Collider[] colliders = Physics.OverlapSphere(explosionPos, radius, 3);
    foreach (Collider hit in colliders)
    {
        if (!hit)
        {
            continue;
        }
        if (hit.rigidbody)
        {
            hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3);
        }
    }
}
}

这是有效的,因为它会增加吸引物体的负力. 问题在于,当该脚本分配给有吸引力的对象时,它处于空中.任何被捕获的物体都朝着它飞去,但随后似乎只是被再次吸引而掉落. 有什么暗示吗?

this works as it adds a negative force that attracts the object. Problem is that when the attractive object this script is assigned to is in mid air. any objects caught fly towards it, but then seems to fall only to be attracted again. Any hints as to why ?

我在想类似的事情,但是什么也没发生:

I was thinking something like this, but nothing happens:

Vector3 direction = this.transform.position - hit.transform.position;
direction.Normalize();
            //hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3);
            //hit.transform.position.;
hit.rigidbody.isKinematic = false;
hit.rigidbody.AddForce(direction);

更新:: 好的,我尝试了一下,它似乎工作良好,但是它附着在被吸引的物体上.我也希望有人能有更好的主意吗?

UPDATE:: Ok I was trying this out, its seems to work well but its attached to the object that is being attracted. Also I was hoping if someone had a better idea?

public void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "GravitySwell")
{
    Debug.Log("i hit it!!!");
    rigidbody.velocity = Vector3.zero;
}
}

推荐答案

您可以做的是添加一个碰撞箱,该碰撞箱作为一个组件添加到任何gameObjects中,然后检查是否有任何东西进入该碰撞箱并添加力碰撞的物体.如果需要,我会为您提供示例代码.

what you can do is add a collision box that unity has as a component to add to any gameObjects and then you check if anything enters that collision box and add force to the object that is colliding. i will get you sample code if needed.

//编辑

您可以将自己的重力添加到脚本中,并具有自己的功能来添加力以使其朝某个方向移动,但是正好您知道可以更改多少重力.身体

you can add your own gravity to a script and have your own function for adding force to make it move in a direction but just so you know you can change how much gravity a rid. body does

但是如果你想做自己的重力,你可以做

but if you want to do your own gravity you can do

bool grounded = false;

void gravity(){

    gameObject.trasform.position.x -= gravity

}

void onCollisionEnter(collision c){
     //check and see what you are colliding with and if its the ground then set grounded to true and that will get you falling gravity. there is more you can do for more adv. gravity but ill leave that up to you
}

编辑*

如果我对粘附对象有何看法,您需要做的就是使该对象成为需要粘附的对象的父对象

if i get what your saying about sticking to an object what you need to do is make the object the parent of the object that needs to stick to it

这篇关于统一3.3,在物体上产生局部重力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆