Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 lib87 · Sep 22, 2015 at 05:14 PM · collisionmovementtriggerstopdoesnt

How to make my moving object (in game) stop once it collides

hi

I have a problem. Im creating a game and basically i have 2 boxes. if these boxes loook each other from the side that i define (i put a plane to their surfaces) then a button appears.. If i click to this button one piece moves to another until it collides and it doesnt stop until it reaches to the middle of the other cube..

Im detecting hits (raycasting) from my first plane to another object plane.. IF hit happens then i add them to an array. If there are 2 objects in array button shows up... I tried many things including deactivating script but then there are other problems shows up.. Like i cannot use raycasting again cuz i deactivate it...

Here is my code:

//This is how i move from one object to another

 Transform a = gameTag.gameO[0].transform;
 Transform b = gameTag.gameO[1].transform;
           
 a.position = Vector3.MoveTowards(a.position,b.position, moveSpeed * Time.deltaTime);


//this is my raycast system. Im putting this script to each of my objects..

 RaycastHit hit;
     if (flagTrigger==false) 
     {
      if (Physics.Raycast(transform.position, -transform.forward, out hit, 10))
            {
              var forward = transform.TransformDirection(Vector3.forward) * 10;
       arrayListForSelectables gameTag = GameObject.FindGameObjectWithTag("TheGame").GetComponent<arrayListForSelectables>();
     if (hit.transform.tag == "Select")
                     {
                         if (!gameTag.gameO.Contains(this.gameObject))
                             gameTag.gameO.Add(this.gameObject);
                     }
                     else
                     {
                         gameTag.gameO.Remove(this.gameObject);
                     }
                 }

//Lastly here my kind of trigger system

 void OnTriggerEnter(Collider other)
 {
     Raycast u = other.GetComponent<Raycast>();
     if (u != null)
     {
 arrayListForSelectables gameTag = GameObject.FindGameObjectWithTag("TheGame").GetComponent<arrayListForSelectables>();
             if (enabled && gameTag.gameO.Contains(this.gameObject))
             {
                 gameTag.gameO.Remove(this.gameObject);
                 flagTrigger = true;
                 //enabled = false;
             }
         }
     }

I commented out the enabled part for now.. If i use it i cannot use the same script again... I know the problems but i donno the answer for it.. Any help please..

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

Answer by AMU4u · Feb 25, 2017 at 04:28 PM

You are over thinking it, is why.

Change your movement code from moveTowards to the objects rigidbody and change it's velocity. If it hits another object, regardless of forces acting in script, it won't move passed another rigidbody on it's collision layer (or another collision layer it is able to interact with).

 rb.velocity = new Vector3(someDirection.x * someSpeed * Time.deltaTime, 0f, rb.velocity.z);



This will be you're new script. You wont need OnCollisionEnter anymore.

If you want to move towards the other cube by the button, go;

 void GetTargetPos()
 {
 
 Vector3 target = new Vector3(targetPos.x, 0f, targetPos.z);
 
 }
 
 
 void FixedUpdate()
 {
 GetTargetPos();
 rb.velocity = new Vector3(targetPos.x * someSpeed * Time.deltaTime, 0f, targetPos.z * someSpeed * Time.deltaTime);
 
 }

Comment
Add comment · Show 1 · 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
avatar image AMU4u · Feb 25, 2017 at 04:38 PM 0
Share

You could even make this neater by putting the V3 velocity method in it's own function outside of your FixedUpdate command center.

 void GetTargetPos()
  {
  
  Vector3 target = new Vector3(targetPos.x, 0f, targetPos.z);
  
  }
 
 void $$anonymous$$oveTowardTargetByTime()
 {
 rb.velocity = new Vector3(targetPos.x * someSpeed * Time.deltaTime, 0f, targetPos.z * someSpeed * Time.deltaTime);
 }
  
  
  void FixedUpdate()
  {
  GetTargetPos();
  $$anonymous$$oveTowardTargetByTime();
  
  }


That way you keep your update functions nice and easy to unit test.

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

30 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

Related Questions

How can I remove movement or camera rotation in one direction with a trigger 0 Answers

Can't seem to get collision to work among my sprites 1 Answer

Stop Movement On Trigger 1 Answer

How do I count Hits ?? 2 Answers

Rigidbody going through Colliders help! 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