Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
avatar image
0
Question by jjain · Jun 20, 2019 at 04:54 PM · collisionrigidbodycollision detectionnewbieeasy

Make a sprite stop after a collision.

I am a first-day unity user and have made a program in which a small robot moves around randomly on a plane. At the end of the plane are walls and I want the robot not to be able to go through them. What can I do to achieve this? Here is the script for the robot.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class RobotMovement : MonoBehaviour { private Vector3 movement; private Rigidbody rb; private int Health; private float xvel; private float zvel; private float xacc; private float zacc; void Start() { xvel = 0F; zvel = 0F; xacc = 0F; zacc = 0F; }

 void Update()
 {
   transform.Translate(xvel,0,zvel);
   if (xvel > 0.1)  {xvel = .1F;}
   if (xvel < -0.1)  {xvel = .1F;}
   if (zvel > 0.1)  {zvel = .1F;}
   if (zvel < -0.1)  {zvel = .1F;}


   xacc = Random.Range(-0.01F,0.01F);
   xvel = xvel + xacc;
   zacc = Random.Range(-0.01F,0.01F);
   zvel = zvel + zacc;

 }

 void OnCollisionEnter(Collision Collision)
 {
   if (xacc > 0) {xacc = -1F;}
   if (xacc < 0) {xacc = 1F;}
   if (zacc > 0) {xacc = -1F;}
   if (zacc < 0) {xacc = 1F;}

 }

}

Thank you so much!

Comment
Add comment · Show 1
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 Divinitize1 · Jun 20, 2019 at 06:29 PM 0
Share

Do both the walls and your robot have some sort of Collider? When you say it goes through the wall, Is it either going through because the wall doesn't have collision, or is it just hitting the wall and attempting to continue moving?.

It all depends on which of these is the case. If you're making it act like a Roomba vacuum, you could do something like this.

 void OnCollisionEnter(Collision collision)  //$$anonymous$$ake sure second collision is lowercase.
 {
     if(collision.GameObject.Tag == "Wall" //Need to confirm this is correct, as im used to triggers
     {
         //Add a bool to check if robot can move to above part of script
         can$$anonymous$$ove = false;
         //Add code to rotate robot 90degrees (or whatever you want)
         cann$$anonymous$$ove = true;
     }
 }

Or you can just use the Can$$anonymous$$ove = false; to stop it. I'm not sure if this is the sort of thing you're after, but I hope it helps with something until someone more experienced can post.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Lightning_A · Jun 20, 2019 at 06:42 PM

All you have to do is put a collider component on each of the walls. Assuming the walls are rectangular, you probably want a BoxCollider (for 3D games) or a BoxCollider2D (for 2D games). Just make sure isTrigger is set to false (isTrigger means that the collider is used to detect collisions, but the game object isn't actually solid). Also, be sure that the size of the collider is the same as the scale of the walls (so that the entire wall is solid, not just part of it; the collider is what makes a game object "solid").

Here's the documentation: https://docs.unity3d.com/ScriptReference/Collider.html

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

191 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

Related Questions

How can I make my character after hitting a wall? 1 Answer

Player getting stuck in corners 0 Answers

Problem with softbodies collisions 0 Answers

Collision detection on children of rigidbodies 0 Answers

Collisions: do they need two rigidbodies? 2 Answers


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