Hi,
I have A problem with my code.
I have one centerblock with two handles on each side. When I move the centerblock with Get axis (Horizontal) the handles move with the block.
At a certain moment, I want to hit my Spacebar and pull the handles to the left and the right.
I kept in mind that the objects cannot cross each other and that there is a wall on the left as well as on the right.
For so far the script is doing that.
The handles now go for either the first position or the last position. (on/off idea)
The thing I would like to add in this script is: I would like to make this transition between the positions smoothly.
I tried to use Lerp: Unity - Scripting API: Vector3.Lerp
and moveTowards Unity - Scripting API: Vector3.MoveTowards.
The gif is showing the movements to give a clear picture.
See the script below.
![using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Uber_control : MonoBehaviour {
public float xMin, xMax;
public float moveSpeed = -3f;
private Rigidbody rb;
public float HandleSpeed = 1f;
public GameObject Uber;
public GameObject leftside;
public GameObject rightside;
public GameObject Block_left;
public GameObject Block_right;
void Start()
{
//Start position handles
leftside.transform.position = (Uber.transform.position + new Vector3 (5, 0,0)) ;
rightside.transform.position = (Uber.transform.position - new Vector3 (5, 0,0));
rb = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
//move the centerblock (helper)
float moveHorizontal = Input.GetAxis ("Horizontal");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, 0.0f);
rb.velocity = movement * moveSpeed;
rb.position = new Vector3 (
Mathf.Clamp (rb.position.x, xMin, xMax),
0.0f,
0.0f
);
}
void Update ()
{
leftside.transform.position = (Uber.transform.position + new Vector3 (5, 0,0)) ;
rightside.transform.position = (Uber.transform.position - new Vector3 (5, 0,0));
if (Input.GetKey ("space")) {
//leftside.transform.position = (Uber.transform.position + new Vector3 (5, 0,0)) ;
//rightside.transform.position = (Uber.transform.position - new Vector3 (5, 0,0));
leftside.transform.position = (Block_left.transform.position);
rightside.transform.position = (Block_right.transform.position);
}
}
}][1]
Thanks in advance for your help.