patterncsharpMinor
Triggered movement script for triggered objects
Viewed 0 times
scriptobjectsmovementfortriggered
Problem
I am trying to more or less perfect a movement script that activates when "triggered" by another object. Below I have the code for both the triggering object as well as the object being triggered.
I tried to provide summary data to make it obvious what I'm doing, but what can I improve here? And please don't tell me "everything". I am new to Unity3d, and am very much still in the process of learning. I need to know specifics on what I can do and work to improve my skill. Also, I know the models aren't the best thing ever made, but I'm also new to Blender!
Button / trigger object as well as its inspector
```
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PropActivateMovement : MonoBehaviour {
#region Public Declarations
private enum ActivationStyle
{
OnTriggerEnter = 0,
OnActionButton = 1,
OnTriggerOccupy = 2
}
public Animator animActivation = null;
#endregion
#region Public Declarations
public string AnimationActivationName = "PushTrigger";
///
/// The audio source when this object is activated.
///
public AudioSource AudioActivated = null;
///
/// The object to move.
///
public PropMovement ObjectToMove = null;
///
/// The direction to move this object.
/// A value greater than or equal to 1 moves in a positive direction.
/// A value less than or equal to -1 moves in a negative direction.
/// A value equal to 0 alternates direction.
///
public int ObjectMovementDirection = 0;
///
/// The style of activation for this object.
/// A value of 0 activates the object OnTriggerEnter.
/// A value of 1 activates the object only once pressing the action button while inside of the triggering location.
/// A value of 2 activates the object OnTriggerEnter and deactivates the object OnTriggerExit.
///
public int StyleOfActivation = 0;
public bool IsActivated = false;
#en
I tried to provide summary data to make it obvious what I'm doing, but what can I improve here? And please don't tell me "everything". I am new to Unity3d, and am very much still in the process of learning. I need to know specifics on what I can do and work to improve my skill. Also, I know the models aren't the best thing ever made, but I'm also new to Blender!
Button / trigger object as well as its inspector
```
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PropActivateMovement : MonoBehaviour {
#region Public Declarations
private enum ActivationStyle
{
OnTriggerEnter = 0,
OnActionButton = 1,
OnTriggerOccupy = 2
}
public Animator animActivation = null;
#endregion
#region Public Declarations
public string AnimationActivationName = "PushTrigger";
///
/// The audio source when this object is activated.
///
public AudioSource AudioActivated = null;
///
/// The object to move.
///
public PropMovement ObjectToMove = null;
///
/// The direction to move this object.
/// A value greater than or equal to 1 moves in a positive direction.
/// A value less than or equal to -1 moves in a negative direction.
/// A value equal to 0 alternates direction.
///
public int ObjectMovementDirection = 0;
///
/// The style of activation for this object.
/// A value of 0 activates the object OnTriggerEnter.
/// A value of 1 activates the object only once pressing the action button while inside of the triggering location.
/// A value of 2 activates the object OnTriggerEnter and deactivates the object OnTriggerExit.
///
public int StyleOfActivation = 0;
public bool IsActivated = false;
#en
Solution
I recommend you don't use public fields in most circumstances. See this link for more information.
I am aware that Unity's inspector will not serialize properties, but there's a nifty way around it, you can use this pattern:
The 'SerializeField' attribute will expose the private field in the inspector. This way you can still set (and change at runtime) the value of a field using Unity's inspector, and also retain the joys of properties.
The only caveat you must remember is that any setter logic you write will not be applied to changes you make in the inspector, because the value will be written straight to the backing field. It's a minor pain for debugging and development, but will not affect a published game.
If you find the pattern a bit much, I recommend creating a snippet using your favourite IDE (it's possible in both MonoDevelop and Visual Studio, at the very least) to automatically create such properties for you.
I am aware that Unity's inspector will not serialize properties, but there's a nifty way around it, you can use this pattern:
[SerializeField]
private float backingFloat;
public float PropertyFloat
{
get
{
return backingFloat;
}
set
{
backingFloat = value;
}
}The 'SerializeField' attribute will expose the private field in the inspector. This way you can still set (and change at runtime) the value of a field using Unity's inspector, and also retain the joys of properties.
The only caveat you must remember is that any setter logic you write will not be applied to changes you make in the inspector, because the value will be written straight to the backing field. It's a minor pain for debugging and development, but will not affect a published game.
If you find the pattern a bit much, I recommend creating a snippet using your favourite IDE (it's possible in both MonoDevelop and Visual Studio, at the very least) to automatically create such properties for you.
Code Snippets
[SerializeField]
private float backingFloat;
public float PropertyFloat
{
get
{
return backingFloat;
}
set
{
backingFloat = value;
}
}Context
StackExchange Code Review Q#70541, answer score: 5
Revisions (0)
No revisions yet.