So, I’ve got this cube (player) and two points (spheres). There is a script on player, which is basically char controller:
using UnityEngine;
using System.Collections;
public class MoveToPoint1 : MonoBehaviour
{
public float smooth = 2;
private Vector3 newPosition;
void Awake ()
{
newPosition = transform.position;
}
void Update ()
{
Vector3 point1Position = GameObject.Find("Point1").transform.position;
Vector3 point2Position = GameObject.Find("Point2").transform.position;
PositionChanging();
if (Input.GetMouseButtonDown(0))
{
RaycastHit hitInfo = new RaycastHit();
bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
if (hit)
{
Debug.Log("Hit " + hitInfo.transform.gameObject.name);
if (hitInfo.transform.gameObject.name == "Point1")
{
newPosition = point1Position;
transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
}
if (hitInfo.transform.gameObject.name == "Point2")
{
newPosition = point2Position;
transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
}
else {
Debug.Log ("nopz");
}
} else {
Debug.Log("No hit");
}
}
}
void PositionChanging ()
{
Vector3 positionB = new Vector3(-5, 3, 0);
if(Input.GetKeyDown(KeyCode.B))
newPosition = positionB;
transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
}
}
So, when I click on either of the points, the cube lerps there. It’s okay and it’s cool.
The cube (some other model in the future) uses a rigidbody (to ‘walk’ upon the terrain, not just float through it), and the spheres use colliders, so the raycasthit would work.
You get my problem already, right?
If not, then the big problem is:
The cube, after clicking on one of the spheres, will try to place itself in the center of the sphere, which is impossible, since both of them have physic colliders, and they start colliding and cube acting all shaky and stuff (which is obvious).
What I want from you is: Help me somehow avoid this and suggest something. The only thing I have in mind is hang the spheres above the terrain (higher than the height of the cube) and somehow restrict the cube to fly up, when I click the sphere (it will try to go up, to where the sphere is), but the cube should be still able to go up and down upon the terrain…
I guess that’s complicated, but suggest me some variants, please, maybe it can be done easier!