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
0
Question by elpuerco63 · Nov 23, 2014 at 02:25 PM · collisiontransformtrigger

Trigger collision detection problem

I have a thin box that has an image of text on it that in landscape is two screen height high. I want the user to be able to scroll this text up and down freely to read the text.

I have a box off screen at the top and bottom in position to stop the text box going too far in either direction that have colliders set as triggers.

The text box is set with a rigid body.

The trigger code for each barrier box is as follows:

 public class TopTriggerController : MonoBehaviour {
 
     public static bool canScrollUp = true;
     
     void OnTriggerEnter (Collider other) {
 
         if (other.gameObject.name == "textCube") {
 
             canScrollUp = false;
         }
     }
 
     void OnTriggerExit (Collider other) {
 
         canScrollUp = true;
     }
 }
 
 public class BottomTriggerController : MonoBehaviour {
 
     public static bool canScrollDown = true;
     
     void OnTriggerEnter (Collider other) {
 
         if (other.gameObject.name == "textCube") {
 
             canScrollDown = false;
         }
     }
 
     void OnTriggerExit (Collider other) {
 
         canScrollDown = true;
     }
 }
 

Which makes sense to be in that if the text box moves up or down too far these triggers will set the flag accordingly to prevent further movement in that direction.

However I think the code in my update method may be at fault as although I do get a collision and the scroll stops it only does so once from the start in either direction. So I scroll up and it stops correctly I scroll down a bit then scroll back up and the trigger no longer fires?

Here is my update code that I am trying to get the text to scroll up and down as the users drags their finger up and down where dragSpeed is set to 3.0f

 void Update () {
 
         if (Input.touchCount > 0) {
     
             // grab reference to this touch
             Touch userTouch = Input.GetTouch (0);
             
             // ray from cameara to point of finger touch
             Ray ray = Camera.main.ScreenPointToRay (userTouch.position);
             
             // hit object to record details of what was hit (touched)
             RaycastHit hit;
             
             // if we got a hit and it was on one of the buttons
             if (Physics.Raycast (ray, out hit) && (hit.collider.gameObject.name == "textCube")) {
             
                 if (userTouch.phase == TouchPhase.Began) {
 
                     lastY = userTouch.position.y;
                 }
 
                 if (userTouch.phase == TouchPhase.Moved) {
                 
                     if (lastY < userTouch.deltaPosition.y && TopTriggerController.canScrollUp){
             
                         transform.Translate(new Vector3(transform.position.x,transform.position.y, -dragSpeed) * Time.deltaTime);
                     }
 
                     if (lastY > userTouch.deltaPosition.y && BottomTriggerController.canScrollDown) {
 
                         transform.Translate(new Vector3(transform.position.x,transform.position.y, dragSpeed) * Time.deltaTime);
                     }
 
                     lastY = userTouch.position.y;
                 }
             }
         }
     }

Am I making a silly schoolboy error?

Thanks in advance

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Detect collision/trigger between two body without rigidbody? 3 Answers

trigger is not working 0 Answers

move camera when it collides with a trigger 1 Answer

how to I zero out a parented child's position in X,Y,Z on collision? 1 Answer

Activate trigger if items colected 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