04-02-2019, 04:08 PM
(04-02-2019, 09:52 AM)_gaoyu_ Wrote: I try to rest prevPositions, but no change
Hi there,
I'm assuming you want to teleport the object to a new position, right? In that case, you cannot set all particles to the same position. This will result in the object collapsing to a single point, then "exploding" after moving to the new location and gaining momentum in a random direction. You need to calculate their position relative to the transform prior to teleporting, then adding these delta positions to the new object position. Something like this:
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;
public class Teleport : MonoBehaviour {
public ObiSoftbody actor;
void Update () {
if (Input.GetKeyDown(KeyCode.A)){
Vector3 newPosition = new Vector3(Random.Range(-4.0f,4.0f),1.5f,Random.Range(-4.0f,4.0f));
for (int i = 0; i < actor.particleIndices.Length; ++i){
int pId = actor.particleIndices[i];
actor.Solver.prevPositions[pId] = actor.Solver.positions[pId] = ((Vector3)actor.Solver.positions[pId] - actor.transform.position) + newPosition;
actor.Solver.velocities[pId] = Vector3.zero;
}
actor.transform.position = newPosition;
}
}
}