- Home /
control object rigidbody individualy ?
i have two objects that use rigidbody , and 1 onbject has certain gravity twists it falls left and right , so when object 1 falls to the left/right , object 2 falls accordingly which is not wanted , so how can set this sideway gravity to control one object only ? like every object that uses rigidbody will be influenced by the gravity of object 1 heres a sample code :
if (transform.position.x > 0)
{
Physics2D.gravity = new Vector2(10f, 0);
}
else if (transform.position.x < 0)
{
Physics2D.gravity = new Vector2(-10f, 0);
}
Answer by DenisIsDenis · Jun 21, 2021 at 05:03 AM
In order not to change the gravity for all objects at once, it is necessary to implement your gravity for each object separately. This script does this:
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class YourGravity : MonoBehaviour
{
// gravity when the object is in the positive x-axis
public Vector2 gravity = Vector2.left * 9.81f;
Rigidbody2D selfRigidbody2D;
void Start()
{
// get the physical component
selfRigidbody2D = GetComponent<Rigidbody2D>();
// disable built-in gravity
selfRigidbody2D.gravityScale = 0;
// to make our gravity look smooth, turn on interpolation.
selfRigidbody2D.interpolation = RigidbodyInterpolation2D.Interpolate;
}
void FixedUpdate()
{
if (transform.position.x > 0)
{
selfRigidbody2D.velocity += gravity * Time.fixedDeltaTime;
}
else if (transform.position.x < 0)
{
selfRigidbody2D.velocity -= gravity * Time.fixedDeltaTime;
}
}
}
EDITED:
If you need to change the gravity for one object when you release the left mouse button, then the script must be changed:
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class YourGravity : MonoBehaviour
{
// gravity when the object is in the positive x-axis
public Vector2 gravity = Vector2.left * 9.81f;
Rigidbody2D selfRigidbody2D;
bool isClickedOnThisObject;
void Start()
{
// get the physical component
selfRigidbody2D = GetComponent<Rigidbody2D>();
// disable built-in gravity
selfRigidbody2D.gravityScale = 0;
// to make our gravity look smooth, turn on interpolation.
selfRigidbody2D.interpolation = RigidbodyInterpolation2D.Interpolate;
}
private void Update()
{
// Processing of keystrokes and mouse clicks
// should always be done in the Update () function.
//In this condition, we check if the player pressed
//the given object and released the left mouse button.
//If yes, then we change the gravity of this object.
if (isClickedOnThisObject && Input.GetMouseButtonUp(0))
{
isClickedOnThisObject = false;
gravity *= -1;
}
}
void FixedUpdate()
{
// Physical calculations should always be done
// in the FixedUpdate() function.
selfRigidbody2D.velocity += gravity * Time.fixedDeltaTime;
}
private void OnMouseDown()
{
// Here we use the OnMouseDown() function, because
// the OnMouseUp() function is called on all objects,
// regardless of the location of the cursor.
isClickedOnThisObject = true;
}
}
oh thank you a lot , just simple question , i have the lines that are in fixedupdate on OnMouseUp function , so is it okay if i added those line in OnMouseup instead of fixedupdate ?
Your answer
Follow this Question
Related Questions
Change GravityScale in runtime 2 Answers
Gravity not working in a sidescrolling 2d game after upgrade to Unity 5 2 Answers
Flickering issue on Rigidbody 0 Answers
Gravity in new scene is too slow 0 Answers