I wrote a script to do this:
When user press and hold a specific key, the rock will be lifted out of the ground (begin slowly, and then faster, and then vibrate only at a pre-defined height)
My script was:
function SmoothLift(startpos : Vector3, endpos : Vector3)
{
if (transform.position.y < NewTransform.y)
{
t += Time.deltaTime/5;
transform.position = Vector3.Lerp(startpos, endpos, Mathf.SmoothStep(0.0, 1.0, t));
//CurrentTransform = transform.position;
yield;
}
else
{
Vibrate(transform.position);
}
}
function Vibrate(currPos : Vector3)
{
var delta = Random.Range(-0.1,0.1);
transform.position = currPos + Vector3(delta,0,delta);
}
My first question is: is there some better approach to my problem (in other words, can you help me to write better code)
My second question is: Now I want to lift a pillar by one base of it. (Lift a lying pillar to a standing pillar - sorry for my bad english). I don’t know how to do this.
The code looks pretty good - if it works, it works!
As for raising the pillar, the easiest way to go is to design the pillar model so that the anchor point is near the base. Then, when you rotate around, say, the X axis, the pillar will raise from the base. If you can’t redesign the model, then put the pillar inside a parent object which has the anchor point in the right position.
You might try using something similar to your SmoothLift function. In Unity, rotations are handled using the Quaternion class. One of the Quaternion methods is called Slerp - this interpolates between two rotations in the same way that Vector3.Lerp interpolates between two points. You could define a SmoothRotate function something like this:-
function SmoothRotate(startRot : Quaternion, endRot : Quaternion)
{
t += Time.deltaTime/5;
transform.rotation = Quaternion.Slerp(startRot, endRot, Mathf.SmoothStep(0.0, 1.0, t));
yield;
}
The other issue is how to get the start and end rotation quaternions. Another handy quaternion method is Euler (named after a mathematician) - this gives you a rotation corresponding to certain angles around the X, Y and Z axes. If your pillar has the Y axis pointing upward when it is upright, then you need to rotate 90º around the X or Z axis to make it fall over. If we use the X axis, the code will look like this:-
endRot = Quaternion.Euler(90, 0, 0);
To raise/lower the pillar, you just need to save its original rotation (from transform.rotation) and use the SmoothRotate function to interpolate between the two positions.
if (Gameobject.position > (position))
{
rigidbody.AddRelativeForce(0,-20,0,ForceMode.Acceleration);
Gameobject.position = (position);
}
would that work for you?
because if the physics system really is a physics system it will just detect if the object is great than a certain amount and then set the acceleration to 0 and then set the position to the position you want.
(if you don’t do that your object will slowly move up because it adds it and then for that brief moment it will go up and then stop so that is why you need the code there)