drag the ball - 0hsyn1 - 28-10-2021
Hi, I have Ball(sphere) and it has soft body property. I want to click and drag with the mouse.
Problem: it breaks down when it moves.
Code inside top object:
Code: using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class BallMove : MonoBehaviour
{
const float speed = 500f;
private Vector3 mOffset;
private float mZCoord;
private bool active;
private Rigidbody rb;
float startRotationY;
void Awake()
{
startRotationY = transform.eulerAngles.y;
}
private void Start()
{
rb = GetComponent<Rigidbody>();
Debug.Log(startRotationY);
}
private void OnMouseDown()
{
mZCoord = Camera.main.WorldToScreenPoint(
gameObject.transform.position).z;
mOffset = gameObject.transform.position - GetMouseAsWorldPoint();
active = true;
rb.velocity = Vector3.zero;
rb.useGravity = false;
rb.constraints -= RigidbodyConstraints.FreezeRotationZ;
}
private void OnMouseUp()
{
active = false;
rb.useGravity = true;
rb.velocity = Vector3.zero;
rb.constraints &= ~RigidbodyConstraints.FreezeRotationZ;
}
private Vector3 GetMouseAsWorldPoint()
{
Vector3 mousePoint = Input.mousePosition;
mousePoint.z = mZCoord;
return Camera.main.ScreenToWorldPoint(mousePoint);
}
private void FixedUpdate()
{
if (active)
{
Vector3 desiredPosition = GetMouseAsWorldPoint() + mOffset;
rb.velocity = Vector3.Distance(desiredPosition, rb.position) > 0.1f ? (desiredPosition - rb.position) * Time.fixedDeltaTime * speed : Vector3.zero;
}
transform.eulerAngles = new Vector3(0, startRotationY, transform.eulerAngles.z);
}
private void Update()
{
transform.position = new Vector3(transform.position.x, transform.position.y, 0);
}
}
RE: drag the ball - josemendez - 28-10-2021
(28-10-2021, 03:04 PM)0hsyn1 Wrote: Hi, I have Ball(sphere) and it has soft body property. I want to click and drag with the mouse.
Problem: it breaks down when it moves.
Code inside top object:
Code: using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class BallMove : MonoBehaviour
{
const float speed = 500f;
private Vector3 mOffset;
private float mZCoord;
private bool active;
private Rigidbody rb;
float startRotationY;
void Awake()
{
startRotationY = transform.eulerAngles.y;
}
private void Start()
{
rb = GetComponent<Rigidbody>();
Debug.Log(startRotationY);
}
private void OnMouseDown()
{
mZCoord = Camera.main.WorldToScreenPoint(
gameObject.transform.position).z;
mOffset = gameObject.transform.position - GetMouseAsWorldPoint();
active = true;
rb.velocity = Vector3.zero;
rb.useGravity = false;
rb.constraints -= RigidbodyConstraints.FreezeRotationZ;
}
private void OnMouseUp()
{
active = false;
rb.useGravity = true;
rb.velocity = Vector3.zero;
rb.constraints &= ~RigidbodyConstraints.FreezeRotationZ;
}
private Vector3 GetMouseAsWorldPoint()
{
Vector3 mousePoint = Input.mousePosition;
mousePoint.z = mZCoord;
return Camera.main.ScreenToWorldPoint(mousePoint);
}
private void FixedUpdate()
{
if (active)
{
Vector3 desiredPosition = GetMouseAsWorldPoint() + mOffset;
rb.velocity = Vector3.Distance(desiredPosition, rb.position) > 0.1f ? (desiredPosition - rb.position) * Time.fixedDeltaTime * speed : Vector3.zero;
}
transform.eulerAngles = new Vector3(0, startRotationY, transform.eulerAngles.z);
}
private void Update()
{
transform.position = new Vector3(transform.position.x, transform.position.y, 0);
}
}
Hi,
Why does your code change the velocity/gravity of a rigidbody?
An object can either be a softbody or a rigidbody. It makes no sense whatsoever to add a rigidbody component to a softbody.
A softbody in Obi is made of particles, which are like tiny individual rigidbodies. If you want to drag a softbody, pick one of its particles and move it.
Obi already provides a helper component that does this: ObiParticleDragger. You can just use it.
RE: drag the ball - 0hsyn1 - 28-10-2021
(28-10-2021, 03:11 PM)josemendez Wrote: Hi,
Why does your code change the velocity/gravity of a rigidbody?
An object can either be a softbody or a rigidbody. It makes no sense whatsoever to add a rigidbody component to a softbody.
A softbody in Obi is made of particles, which are like tiny individual rigidbodies. If you want to drag a softbody, pick one of its particles and move it.
Obi already provides a helper component that does this: ObiParticleDragger. You can just use it.
thanks for your quick answer
I will review and try
if i have problems i will write again
RE: drag the ball - 0hsyn1 - 02-11-2021
(28-10-2021, 03:11 PM)josemendez Wrote: Hi,
Why does your code change the velocity/gravity of a rigidbody?
An object can either be a softbody or a rigidbody. It makes no sense whatsoever to add a rigidbody component to a softbody.
A softbody in Obi is made of particles, which are like tiny individual rigidbodies. If you want to drag a softbody, pick one of its particles and move it.
Obi already provides a helper component that does this: ObiParticleDragger. You can just use it.
Hi,
How can I fix the nested migrations issue?
I use the standard obi soft body ball.
when I put a ball on top of a ball a few times, it intertwines.
RE: drag the ball - josemendez - 02-11-2021
(02-11-2021, 12:16 PM)0hsyn1 Wrote: Hi,
How can I fix the nested migrations issue?
I use the standard obi soft body ball.
when I put a ball on top of a ball a few times, it intertwines.
Just make sure your particles are large enough that no/few gaps appear in between them, then use enough collision constraint iterations/substeps.
|