Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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

alt text

The Object in question is the cube

capture3.png (257.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
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;
         }
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

256 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Tilemap Collision 2D colliding with ground. 0 Answers

Colliders for tunnels 0 Answers

My character falls through the mesh collider ground.,My character falls straight through my mesh collider ground. 0 Answers

I want my player to walk through walls 1 Answer

thisCollider and otherCollider from collision.GetContacts() 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges