Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by RGI · Dec 31, 2012 at 06:59 PM · androidjavascriptcollisioncontrols

Collision not working.

Hi, I've spent about 3 hours trying every single possible way of doing this but nothing seems to work. I have an object with a mesh collider and rigidbody. Iam moving this object on Z with 2 touch controls (android platform) Script for moving:

 var leftcontrol : GUITexture;
 var rightcontrol : GUITexture;
 
 
 function awake(){
     leftcontrol = GameObject.Find("GUIleft").guiTexture;
     leftcontrol = GameObject.Find("GUIright").guiTexture;
 }
 
 
 function Start () {
 
 }
 
 function Update () {
 
         for (var touch : Touch in Input.touches)
         {
             if (touch.phase == TouchPhase.Stationary && leftcontrol.HitTest (touch.position)){
                 transform.position.z -= 10;
             
         }}
         for (var touch : Touch in Input.touches)
         {
             if (touch.phase == TouchPhase.Stationary && rightcontrol.HitTest (touch.position)){
                 transform.position.z += 10;
             
         }
 
         }
 }
 
 

and then there are 2 objects with box colliders that are supposed to block the object from moving too far but it just isnt working. The Collision isnt working by itself so I tried adding this script but it isnt working either.

 function OnCollisionEnter(theCollision : Collision){
      if(theCollision.gameObject.name == "lWall"){
          transform.position.z += 10;
      }
      
      if(theCollision.gameObject.name == "rWall"){
       transform.position.z -= 10;
      }
     }
 

Any suggestions?

Comment
Add comment · Show 8
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 whydoidoit · Dec 31, 2012 at 07:01 PM 0
Share

Are you getting the OnCollisionEnter at all?

avatar image RGI · Dec 31, 2012 at 07:13 PM 0
Share

Probably not.

avatar image Josh707 · Dec 31, 2012 at 07:16 PM 0
Share

I believe the way it's executing, if you're still holding the button it will keep adding 10 to the z axis position. In your collisionEnter/Exit functions have a bool set to true and false, and in the Update if that bool is false as well as the input add 10 to the position.

Oh yeah and attach a collider set as a trigger to your character and attach the collision enter/exit checking script to it. The script is essentially checking if it collided with itself, if you attach it to the wall. Also I would just tag each wall as "Wall" in the inspector and check the tag. You'll have a much shorter script.

avatar image jogo13 · Dec 31, 2012 at 11:32 PM 0
Share

If you're not sure if OnCollision is working you can place a Debug command to log the name of collided objects. (Don't leave it in release build!): Debug.Log(theCollision.gameObject.name);

avatar image jogo13 · Jan 01, 2013 at 12:41 AM 1
Share

I think if your setting position directly on the object it can pass through physics...you can try moving the object via addforce: http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by aldonaletto · Jan 01, 2013 at 05:15 AM

Moving the object by setting its transform.position (or using Translate) fools the collision system. You should move a rigidbody by adding forces or setting its velocity, but it would be easier to use a CharacterController instead. Unfortunately, the CharacterController has a capsule collider always aligned to the Y axis - you can't change it to a box collider, nor tilt it, but can control its height and radius, and even transform it in a sphere. If a capsule or sphere works for you, the code could be as simple as this one:

 var leftcontrol : GUITexture;
 var rightcontrol : GUITexture;
 private var controller : CharacterController;
 
 function Start(){ // find the controls at Start, not Awake!
     leftcontrol = GameObject.Find("GUIleft").guiTexture;
     rightcontrol = GameObject.Find("GUIright").guiTexture;
     controller = GetComponent(CharacterController);
 }
 
 function Update () {
   for (var touch : Touch in Input.touches){
     if (touch.phase == TouchPhase.Stationary){
       if (leftcontrol.HitTest (touch.position)){
         controller.Move(Vector3(0, 0, -10));
       }
       else
       if (rightcontrol.HitTest (touch.position)){
         controller.Move(Vector3(0, 0, 10));
       }
     }
   }
 }

NOTE: CharacterController and Rigidbody are mutually exclusive - delete the Rigidbody after adding the CharacterController.

Comment
Add comment · Show 2 · 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 RGI · Jan 02, 2013 at 01:04 PM 0
Share

Thank you, that pretty much solved my problem. One thing though, you are missing a ")" at line 15 and 19. Thank you

avatar image aldonaletto · Jan 04, 2013 at 07:26 PM 0
Share

Thanks for the warning - the answer is fixed now.

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

14 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

Related Questions

BCE0019: 'collision' is not a member of 'Object'. 1 Answer

How to change speed transition between materials 2 Answers

Coins work in engine but not on phone 0 Answers

Collide detection with tag [JS] 0 Answers

Car dynamics? 0 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