Lifting a rock

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.

Thank you for your help!

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.

thank you

my second problem still needs help. I tried some approach, such as rigidbody.rotation, but they didn’t work.

Thank you.

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.

In fact, I want to make the movement as real as possible, just using Unity script. I tried this code (to lift the rock):

function SmoothMove(startpos : Vector3, endpos : Vector3)
{
	  
	if (transform.position.y < NewTransform.y)
	{			rigidbody.AddForce(Vector3.up*10,ForceMode.Acceleration);
      yield;
	}
	else
	{
		isOK = true;
		Vibrate(transform.position);
		//yield new WaitForSeconds(3);
	}
	
}

function Vibrate(currPos : Vector3)
{
	var delta = Random.Range(-0.1,0.1);
	transform.position = currPos + Vector3(delta,0,delta); 
}

and it seems to be better. I tried out with the pillar but it’s didn’t work right.

Thank you for your help

Still need help. I’m out of hope :cry:

Don’t give up hope just yet… :wink:

Can you post the code you are using to move the pillar? Without seeing the code, I don’t really know which part is going wrong.

Update:
I did this:
I created a plane and remove it mesh renderer. Then, I attached it with the pillar by a spring joint.

function Update () {
if(Input.GetKey("b"))
	
{			rigidbody.AddRelativeForce(0,20,0,ForceMode.Acceleration);

}
}

It work, but I don’t know how to stop when the pillar is raised. I’m still looking for a better solution

Still waiting for you.

 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)

After three weeks of trying several approach, my script works well now.
Thank you so much for you supports!