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 Mint92 · Nov 04, 2013 at 12:35 PM · gameobjecttilt

Making script only effect one gameobject when attached to multiple

hey everyone, im making a short game for university and im having some problems with my script. The game is a tilting boardgame and i need it so that when the ball lands on a platform it only tilts and when it goes onto the next platform below the board above needs to stop tilting

so far i have this code however all the platforms in the scene are tilting where as I only want the platform that the ball to tilt

 using UnityEngine;
 using System.Collections;
 
 public class Tilt : MonoBehaviour 
 {
     float smooth = 1.0f;
     float tiltAngle = 15.0f;
     
     // Update is called once per frame
     void Update ()
     {
         //check if the user is pressing left or right, up or down on the keyboard
         //by default Unity assigns the "a" and "d" key as well as the left and right arrow keys to the horizontal axis and the "w" and "s" key as well as the up and down arrows keys to the vertical axis.
         float tiltAroundX = Input.GetAxis("Horizontal") * tiltAngle;
         float tiltAroundZ = Input.GetAxis("Vertical") * tiltAngle;
         
         Quaternion target = Quaternion.Euler(tiltAroundX,0, tiltAroundZ);
         
         this.gameObject.transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth); 
     }
     
     void OnTriggerEnter(Collider other)
     {
         
         GetComponent<Tilt>().enabled = true;
     }
     
     void OnTriggerExit(Collider other)
     {
         GetComponent<Tilt>().enabled = false;
     }
 }
 
Comment
Add comment · Show 5
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 drallim33 · Nov 04, 2013 at 12:47 PM 0
Share

Is there anything else that could be colliding with the platforms and triggering OnTriggerEnter? $$anonymous$$ight be worth putting an if(other == ball) in there, or setting up the layers so that the platforms only collide with the ball. You can debug.log(other) to check if something else is causing the trigger prematurely.

Or perhaps the platforms start in an enabled state when the game runs, so they will be always enabled until they get a trigger exit event?

avatar image mattssonon · Nov 04, 2013 at 12:49 PM 0
Share

You can just call enabled, you don't need the GetComponent(). part in front. Are the OnTriggerEnter() and OnTriggerExit() functions being run? Try putting a Debug.Log("PRINT"); line in both of them to check.

avatar image Jamster · Nov 04, 2013 at 05:26 PM 0
Share

@mattssonon I think if he gets rid of the GetComponent() then it will disable his whole object... thus all the graphics for it as well! :P

avatar image mattssonon · Nov 04, 2013 at 06:01 PM 0
Share

@Jamster The method for disabling the GameObject is SetActive(false), typing enabled is simply shorthand for GetComponent(Tilt).enabled in this case, just like rigidbody is shorthand for GetComponent(Rigidbody).

avatar image Jamster · Nov 04, 2013 at 11:01 PM 0
Share

Oops! I see now sorry :)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Nov 04, 2013 at 05:41 PM

Add a "am I turned on" variable:

 public bool activeTilt=false;
 // public only added for debugging

 Update() {
   if(!activeTilt) return;
   ...
 }

 OnTrigEnter() { activeTilt=true; }

A better solution is probably to move tilting code into the ball, with public Transform activePlatform; changed by the ball's triggerEnter. You can think of the ball as the player, and the player tilts the "current" platform.

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
avatar image
0

Answer by Mint92 · Nov 05, 2013 at 03:40 PM

Ive got it to work, Yes putting that code in the tilt script wasn't the best way

 using UnityEngine;
 using System.Collections;
 
 public class TiltActivator : MonoBehaviour 
 {
     public GameObject fromBoard;
     public GameObject toBoard;
     
     
     void OnTriggerEnter(Collider other)
     {        
         if(other.tag != "Player")
         {
             fromBoard.GetComponent<Tilt>().enabled = false;
             toBoard.GetComponent<Tilt>().enabled = true;
         }
     }
 }

I would attach this script to each of my Exit triggers and attach each board to its slot

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

18 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

Related Questions

Multiple Cars not working 1 Answer

Detection if a GameObject is below you or next to you? 1 Answer

Unity3D Pressure Plate request. 3 Answers

Add force to GameObject 2 Answers

c# destroy gameobject on 0 hp 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