- Home /
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;
}
}
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?
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.
@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
@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)
.
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.
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
Your answer
Follow this Question
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