I made simple script.Using raycast to hit lever collider.
Works fine,then I made that action happens when player press E on keyboard.
I tried to make when player press E object moves from its position to position that i set,but to move slowly and smooth.It does not work properly,object moves fast and object does not go all the way to position where I set.If i decrease the speed of object movement the object moves fast again but does not go all the way it goes even less than previously with higher speed.
CODE:
public class RayCast : MonoBehaviour {
public float RayDistance;
public GameObject ObjectToMove;
public GameObject TekstObject;
public Text message;
public float Speed;
public string Object;
bool pulled;
void Update()
{
//Ray Ray = new Ray(transform.position, transform.TransformDirection(Vector3.forward));
RaycastHit hit;
if (Physics.Raycast(transform.position,transform.TransformDirection(Vector3.forward),out hit,RayDistance))
{
if (hit.collider.tag == Object)
{
TekstObject.gameObject.SetActive(true);
if (Input.GetKeyDown(KeyCode.E))
if(pulled == false)
{
Debug.LogWarning("Lever Down");
ObjectToMove.transform.position = Vector3.Lerp(ObjectToMove.transform.position,new Vector3(-4.252f, 0.835f,3.747f), Speed);
message.text = "Press E to pull the lever down";
pulled = true;
}
else if (pulled == true)
{
Debug.LogWarning("Lever Up");
message.text = "Press E to pull the lever up";
pulled = false;
}
}
else { TekstObject.gameObject.SetActive(false); }
}
}
}
Okay, I read your code more closely and I think you want to have MoveTowards outside of your conditions.
That is, if the user only has to press ‘E’ once. If you want it to move while the key is held down, you should switch to ‘GetKey’ rather than GetKeyDown.
Lerp… I don’t think it means what you think it means…
You are calling it a single time, how could it move the lever smooth?
it’s basically this:
if my first value is 0 and my second number is 100, we could imagine a linear graph that starts at 0 and goes to 100
if i interpolate by 1 i get to the end of the graph and get 100, if i put .5 in i’ll get 50, if i put 2 in i’ll get 200
(this did not happen because you used the clamped version, that, err, well, clamps the interpolation value to one)
if the numbers are 10 and 20 it’s like this:
1 = 20
.5 = 15
0 = 10
i hope this make a bit of sense, i have a particular hard time explaining things
I don’t think that’s what he want’s(GetKey vs GetKeyDown), i think that’s a perfect place for a coroutine.
if i’m the player and i release the key, the lever can get stuck mid-way, if i don’t release the key it’s gonna giggle back and forth. plus it’s never gonna rest at the exact end, and that wrecks my OCD, lol
You’re absolutely right. It would take more work to make it work with GetKey there, but it could be done.
My post was not as complete as it could have been to mention that, or even to avoid suggesting it because of that.
A coroutine would certainly be nice or just a destination Vector3 that the game object can travel towards, but still run in Update.
Edit: Just to be clear, though several options are possible, I’d probably write a coroutine and use Lerp
Is it true that the lever can’t go back once moved?
In any event, coroutines are handy… Hope this helps.
IEnumerator MoveLever() {
Transform otrans = ObjectToMove.transform;
Vector3 startpos = otrans.position;
Vector3 endpos = new Vector3(-4.252f, 0.835f,3.747f);
float moveTime = 1f; // can change this.
for(float f = 0; f <= moveTime; f += Time.deltaTime) {
otrans.position = Vector3.Lerp(startpos, endpos, f / moveTime);
yield return null;
}
otrans.position = endpos;
}
Erase your Lerp and put : StartCoroutine(MoveLever()); after “pulled = true”.
A little more work may be required if you can move the lever up and down…