- Home /
How do you duplicate/reuse scripts that have references to other scripts.
Anyways, here is my script:
using UnityEngine;
public class PC2D : MonoBehaviour
{
#region Variables
//================Speed and Basic Movement=====================================================================================================================
[Header("Speed and Basic Movement")]
public float speed; // Max speed of the player
private float hVelocity; // Horizontal velocity of character
//================Acceleration and Deceleration================================================================================================================
[Header("Acceleration and Decleration")]
public float hAccelRate = 10f;
public float hDampNormSpeed = 10f;
public float hDampNorm = 0.1f;
public float hDampOnStopSpeed = 10f;
public float hDampOnStop = 0.9f;
public float hDampOnTurn = 0.3f;
public float hDampOnTurnSpeed = 10f;
//================Jump Tweaks==================================================================================================================================
[Header("Jump Tweaks")]
public float jumpForce; // The force the character can jump at
public float jumpControl = 0.8f;
public int numberOfJumps; // Number of jumps they can use
private int currJumps; // Current number of available jumps
public float jumpMemory = 0.2f; // When you press the button a tiny bit before you're grounded, you don't jump. This makes the controls feel inactive. Feeling like there is missing input is the WORSE
private float currJumpMemory; // Current jump memory
public float coyoteTime = 0.2f; // Coyote time is when you fall off a cliff and you turn on this timer counting down where you can still jump. Makes the input feel MUCH more responsive
private float currCoyoteTime; // Current coyote time
//================Checks/Status Variables======================================================================================================================
[Header("Checks/Status Variables")]
private bool direction = true; // True = right and false = left
private bool isGrounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
//================External References==========================================================================================================================
[Header("References to External Scripts")]
public Rigidbody2D rb; // Reference to the rigidbody
public GravToggle gravToggle; // Reference to the GravToggle script
public InputManager inputManager; // Reference to InputManager script
#endregion
void Update()
{
DecrementJumpMemory();
if(inputManager.onJump)
{
JumpMemory();
} else if(inputManager.onFall && rb.velocity.y > 0) // Only when we let go and when we're going up
{
MoveCharacter(new Vector2(rb.velocity.x, rb.velocity.y * (1f - jumpControl))); // Cut the vertical velocity so we can have controllable jumps
}
DecrementCoyoteTime();
if(isGrounded)
{
CoyoteTime();
}
}
void FixedUpdate()
{
AccelAndDecelCalc();
if(currJumpMemory > 0 && currCoyoteTime > 0 && currJumps > 0)
{
// Reset the values so they don't double jump
currCoyoteTime = 0;
currJumpMemory = 0;
Jump();
}
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
if(isGrounded)
{
ResetJumps();
}
#region House
// Eva was here 5/10/2020 B) time: 1;29am
// You're complete garbage
/* ░░
░░░░░░▒▒▓▓▓▓
░░▒▒▒▒▒▒▒▒▓▓▓▓▒▒░░
░░▒▒▒▒▓▓▓▓▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓
░░▒▒▒▒▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▒▓▓▒▒▒▒▒▒▒▒
░░▒▒▒▒▓▓▓▓▓▓▓▓▒▒▒▒▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▒▒
▒▒▓▓▓▓▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▓▓▓▓▒▒▒▒▒▒▒▒
▒▒▓▓▒▒▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▓▓▓▓▓▓▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▒▒
▒▒▒▒▒▒▒▒▓▓▒▒▒▒▒▒▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▒▓▓▒▒▒▒▒▒▓▓▓▓▒▒
▒▒▓▓▒▒▒▒▒▒▒▒▓▓▓▓▓▓▒▒▒▒▒▒▓▓▓▓▓▓▓▓▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒
░░▒▒▒▒░░ ▒▒▒▒▒▒▒▒░░▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▒▒▒▒▒▒▓▓▓▓▓▓▒▒
░░░░▒▒░░▒▒ ▒▒▒▒▒▒▒▒▒▒░░░░░░▒▒▓▓▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▓▓▓▓▓▓▓▓▒▒▒▒▒▒▓▓▓▓
░░▒▒ ░░ ▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒▓▓▓▓▓▓▓▓▒▒▒▒▒▒▓▓▓▓▓▓▓▓▒▒▒▒▒▒▓▓▓▓▓▓▓▓▒▒
░░░░░░░░ ░░▒▒▒▒▓▓▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒▓▓▒▒▒▒▓▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▓▓▓▓▓▓▓▓▒▒▒▒░░
░░▒▒▒▒░░░░░░░░ ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▒▒▒▒▒▒▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒
▒▒▒▒▒▒░░░░░░ ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
░░▒▒▒▒░░░░ ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▒▒
▒▒▒▒░░░░░░░░░░░░ ▒▒░░░░░░▒▒▒▒▒▒▒▒▒▒ ▒▒▒▒▒▒▒▒▒▒░░░░░░▒▒▓▓▒▒░░▓▓▒▒
▒▒▒▒▒▒▒▒░░▒▒░░▒▒▒▒░░ ▒▒░░░░░░░░░░░░░░▒▒ ▒▒░░░░░░░░░░░░░░▒▒▓▓ ▓▓▒▒░░
▒▒ ▒▒▒▒░░▒▒░░▒▒▒▒ ▒▒░░░░░░░░░░░░░░▒▒ ░░▒▒░░░░░░░░░░░░░░▒▒▓▓ ▓▓▒▒
░░▒▒▒▒▒▒░░░░░░░░░░ ▒▒▒▒░░░░░░░░░░░░▒▒ ▒▒░░░░░░░░░░░░▒▒▒▒▓▓░░ ▓▓▒▒░░░░░░░░
▒▒▒▒░░░░▒▒▒▒░░ ▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░ ░░▒▒░░░░░░▒▒▒▒▒▒▒▒▒▒▓▓ ▓▓▒▒▒▒░░░░░░░░
░░░░▒▒░░░░░░▒▒ ░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░▒▒▒▒▒▒▒▒░░░░░░░░░░
░░▒▒▒▒░░░░░░▒▒░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░░░
░░▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒ ░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░
░░▒▒░░░░░░▒▒░░░░▒▒░░░░░░░░░░░░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░
░░▒▒▒▒░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░▓▓▓▓▓▓▓▓
████████████████████████████████████████████████████████████████████████████████████████████████
████▒▒▓▓▓▓▒▒▒▒▒▒▒▒░░██████████████████████████████████████████████████████████████████▓▓████████
██████▓▓▓▓██▓▓████▓▓██████████████████████████████████████████████████▓▓▓▓▓▓▓▓▓▓▓▓▓▓██▓▓████▓▓██
*/
#endregion
if((direction == true && inputManager.hInput < 0) || (direction == false && inputManager.hInput > 0)) // Flips the character when they're going the wrong way
{
// Debug.Log("Other way now!"); // test
Redirect();
}
MoveCharacter(new Vector2(hVelocity * speed * Time.deltaTime, rb.velocity.y)); // Moves the character side to side
}
public void Redirect() // Flips the character
{
direction = !direction; // Inverts direction from true -> false, false -> true
// Debug.Log(direction); // test
Vector3 scaler = transform.localScale; // Saves scale as variable
scaler.x *= -1f; // Multiplies it by -1, so if it's 1 -> -1, -1 -> 1
transform.localScale = scaler; // Updates the scale
}
//================Jumping Functions============================================================================================================================
void Jump() // big fat poo
{
// Debug.Log("YAHOO!"); // test
MoveCharacter(Vector2.up * jumpForce); // Upward force
currJumps--; // Decreasing the number of jumps
}
void ResetJumps()
{
currJumps = numberOfJumps;
}
//================Walking Functions============================================================================================================================
void MoveCharacter(Vector2 vel) // Better than "rb.velocity =" again and again
{
rb.velocity = vel;
}
void AccelAndDecelCalc()
{
hVelocity += hAccelRate * inputManager.hInput; // Acceleration
if(!inputManager.isWalking)
{
hVelocity *= Mathf.Pow(1f - hDampOnStop, Time.deltaTime * hDampOnStopSpeed); // On stop
} else if(Mathf.Sign(inputManager.hInput) != Mathf.Sign(hVelocity))
{
hVelocity *= Mathf.Pow(1f - hDampOnTurn, Time.deltaTime * hDampOnTurnSpeed); // On turning
} else
{
hVelocity *= Mathf.Pow(1f - hDampNorm, Time.deltaTime * hDampNormSpeed); // Nomrmal damping
}
}
//================Jump Memory Functions========================================================================================================================
void JumpMemory()
{
currJumpMemory = jumpMemory;
}
void DecrementJumpMemory()
{
currJumpMemory -= Time.deltaTime;
}
//================Coyote Time Functions========================================================================================================================
void CoyoteTime()
{
currCoyoteTime = coyoteTime;
}
void DecrementCoyoteTime()
{
currCoyoteTime -= Time.deltaTime;
}
}
I want to be able to duplicate this for other GameObjects, but it has references to the Input Manager
script and the GravToggle
script. Any tips on this?
Answer by Klarzahs · Oct 06, 2020 at 03:35 PM
Hi @LittleBigPlanetGeek ,
as far as I understand your problem: you have a single PC2D script attached to an object and would like to have PC2D's attached to other objects as well.
Assuming you only have one InputManager and GravToggle Script in the Scene and want to keep it that way, you can either:
use a static variable for both IM and GT scripts, either with lazy initialization or with onStart(). Static variables are bound to the class definition, not an class instance (i.e. object). They are therefore accessible from all objects of this class
You can also use the GameObject.Find() function to search for the IM and GT gameobjects, or find the gameobject with tags
Hope this helps. If not, please clarify
Also, next time, please $$anonymous$$ify your script to only the relevant parts
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do I go about creating a 2D world? 1 Answer
Script doesn't work on duplicate objects. Please help 1 Answer