Question by
izzanfuad · Mar 16 at 09:33 AM ·
collisionunityeditorcolliderswater
How to Stop from MoveTowards then Move again
So I got an Object that cannot go to Water, if by chance he collide to Water he will stop. I have done that so that it will stop, but cannot seem to find a way to make it move again. I also wanted if he can actually move again, if i clicked on the other side of the water he will not move pass the water.
I was using NavMesh back then but because my work wanted me to not using NavMesh I cannot seem to find a way to do it.
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Vector : MonoBehaviour
{
public static Vector instance;
public Transform obj;
[SerializeField] Vector3 a;
[SerializeField] Vector3 b;
[SerializeField] Vector3 c;
public Transform water;
public float speed;
private RaycastHit hit;
Vector3 previousPosition;
Vector3 targetPosition;
float lerpMoving = 1f;
public List<Transform> selectedUnit = new List<Transform>();
private Vector3 mousePos;
public bool insidetrigger = false;
public float stopdistance = 5;
public bool onwater = false;
// Start is called before the first frame update
void Start()
{
instance = this;
}
void Awake()
{
}
// Update is called once per frame
void Update()
{
// transform.position = obj.position;
HandleUnitMovement();
}
public void HandleUnitMovement()
{
if (Input.GetMouseButtonDown(0))
{
mousePos = Input.mousePosition;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
LayerMask layerHit = hit.transform.gameObject.layer;
switch (layerHit.value)
{
case 8:
SelectUnit(hit.transform, Input.GetKey(KeyCode.LeftShift));
break;
case 10:
SelectUnit(hit.transform, Input.GetKey(KeyCode.LeftShift));
break;
default:
// isDragging = true;
DeselectUnit();
break;
}
}
}
if (Input.GetKeyDown(KeyCode.Mouse1) && HaveSelectedUnit())
{
if (Input.GetMouseButtonDown(1) && selectedUnit.Contains(obj))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
LayerMask layerHit = hit.transform.gameObject.layer;
switch (layerHit.value)
{
case 4:
break;
case 8:
break;
case 9:
break;
default:
foreach(Transform unit in selectedUnit)
{
previousPosition = transform.position;
targetPosition = new Vector3 (hit.point.x, previousPosition.y,
hit.point.z);
lerpMoving = 0;
}
break;
}
}
else
{
}
}
}
if(lerpMoving < 1)
{
movePlayer();
}
}
public void movePlayer()
{
if(!onwater)
{
lerpMoving += Time.deltaTime;
transform.position = Vector3.MoveTowards(previousPosition, targetPosition,
speed * lerpMoving);
if(Vector3.Distance(obj.position, water.position) < stopdistance)
{
onwater = true;
// yield return null;
}
}
else if(onwater)
{
lerpMoving += Time.deltaTime;
transform.position = Vector3.MoveTowards(previousPosition, targetPosition,
speed * lerpMoving);
}
}
private void SelectUnit(Transform unit, bool canMultiselect = false)
{
if(!canMultiselect)
{
DeselectUnit();
}
selectedUnit.Add(unit);
unit.Find("Highlight").gameObject.SetActive(true);
}
private void DeselectUnit()
{
for (int i = 0; i < selectedUnit.Count; i++)
{
selectedUnit[i].Find("Highlight").gameObject.SetActive(false);
}
selectedUnit.Clear();
}
private bool HaveSelectedUnit()
{
if (selectedUnit.Count > 0)
{
return true;
}
else
{
return false;
}
}
}
Here's my map
The Object in question is the cube
capture3.png
(257.3 kB)
Comment
Best Answer
Answer by izzanfuad · Mar 17 at 04:03 AM
Never mind guys, found my answer just now.
Here's my code
public float stopdistance = 10f;
public bool onwater = false;
public float jarakStopYgHilang;
void Update()
{
if(Vector3.Distance(obj.position, water.position) < stopdistance)
{
onwater = false;
}
HandleUnitMovement();
}
public void HandleUnitMovement()
{
if (Input.GetMouseButtonDown(0))
{
mousePos = Input.mousePosition;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
LayerMask layerHit = hit.transform.gameObject.layer;
switch (layerHit.value)
{
case 8:
SelectUnit(hit.transform, Input.GetKey(KeyCode.LeftShift));
break;
case 10:
SelectUnit(hit.transform, Input.GetKey(KeyCode.LeftShift));
break;
default:
// isDragging = true;
DeselectUnit();
break;
}
}
}
if (Input.GetKeyDown(KeyCode.Mouse1) && HaveSelectedUnit())
{
if (Input.GetMouseButtonDown(1) && selectedUnit.Contains(obj))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
LayerMask layerHit = hit.transform.gameObject.layer;
switch (layerHit.value)
{
case 4:
break;
case 8:
break;
case 9:
break;
default:
foreach(Transform unit in selectedUnit)
{
onwater = false;
previousPosition = transform.position;
targetPosition = new Vector3 (hit.point.x,
previousPosition.y, hit.point.z);
lerpMoving = 0;
}
break;
}
}
else
{
}
}
}
if(lerpMoving < 1)
{
movePlayer();
}
}
public void movePlayer()
{
if(!onwater)
{
if(Vector3.Distance(obj.position, water.position) > stopdistance)
{
lerpMoving += Time.deltaTime;
transform.position = Vector3.MoveTowards(previousPosition,
targetPosition,
speed * lerpMoving);
}
else
{
onwater = true;
stopdistance -= .01f;
}
}
}
public void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "water")
{
OutTrigger();
jarakStopYgHilang = 10f - stopdistance;
stopdistance = jarakStopYgHilang + stopdistance;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613054237im_/https://answers.unity.com/themes/thub/images/avi.jpg)