- Home /
How would I Trigger a Change in Inputs
I have 2 scripts attached to my Player.
Here:
(SSController)
public var cont1 : int;
private var motor : CharacterMotor;
// Use this for initialization
function Awake () {
motor = GetComponent(CharacterMotor);
}
// Update is called once per frame
function Update () {
if (cont1 == 1){
// Get the input vector from kayboard or analog stick
var directionVector = new Vector3(Input.GetAxis("SSVertical"), 0, Input.GetAxis("SSHorizontal"));
if (directionVector != Vector3.zero) {
// Get the length of the directon vector and then normalize it
// Dividing by the length is cheaper than normalizing when we already have the length anyway
var directionLength = directionVector.magnitude;
directionVector = directionVector / directionLength;
// Make sure the length is no bigger than 1
directionLength = Mathf.Min(1, directionLength);
// Make the input vector more sensitive towards the extremes and less sensitive in the middle
// This makes it easier to control slow speeds when using analog sticks
directionLength = directionLength * directionLength;
// Multiply the normalized direction vector by the modified length
directionVector = directionVector * directionLength;
}
}
// Apply the direction to the CharacterMotor
motor.inputMoveDirection = transform.rotation * directionVector;
motor.inputJump = Input.GetButton("Jump");
}
// Require a character controller to be attached to the same game object
@script RequireComponent (CharacterMotor)
@script AddComponentMenu ("Character/FPS Input Controller")
(SSControler2)
public var cont1 : int;
private var motor : CharacterMotor;
// Use this for initialization
function Awake () {
motor = GetComponent(CharacterMotor);
}
// Update is called once per frame
function Update () {
if (cont1 == 2){
// Get the input vector from kayboard or analog stick
var directionVector = new Vector3(Input.GetAxis("SSHorizontal2"), 0, Input.GetAxis("SSVertical2"));
if (directionVector != Vector3.zero) {
// Get the length of the directon vector and then normalize it
// Dividing by the length is cheaper than normalizing when we already have the length anyway
var directionLength = directionVector.magnitude;
directionVector = directionVector / directionLength;
// Make sure the length is no bigger than 1
directionLength = Mathf.Min(1, directionLength);
// Make the input vector more sensitive towards the extremes and less sensitive in the middle
// This makes it easier to control slow speeds when using analog sticks
directionLength = directionLength * directionLength;
// Multiply the normalized direction vector by the modified length
directionVector = directionVector * directionLength;
}
}
// Apply the direction to the CharacterMotor
motor.inputMoveDirection = transform.rotation * directionVector;
motor.inputJump = Input.GetButton("Jump");
}
// Require a character controller to be attached to the same game object
@script RequireComponent (CharacterMotor)
@script AddComponentMenu ("Character/FPS Input Controller")
and another script attached to a trigger that changes the int on each script accordingly.
The int changes on the scripts like it's supposed to.. But for some reason when Cont1 becomes 2
I loose all movement. . where my SSController2 script should begin it doesnt seem to want to work????
Whats going on?
If There is a better way to script an input change let me know please.. if this is a stupid way of changing controls that's fine just tell me what to do.. I'm a noob I wont get offended.
Answer by DaveA · Aug 23, 2012 at 09:19 PM
If I understand correctly, you want to switch between these 2 controller scripts, one active at a time, right?
First, using cont1 in both scripts does not make them both use the same variable. You would need one of them to be a static, and the other would not exist (other script would access the static var) or you would need to get the instance of that other component and get cont1 on it.
The way I've done this is have 3rd script with knows about the other 2 and just sets the 'enabled' flag on them accordingly
I do have a third script on the trigger... it looks like this
function OnTriggerEnter (other:Collider) { var cam = GameObject.FindGameObjectWithTag("$$anonymous$$ainCamera"); if(other.CompareTag("SSPlayer")) {
//ASSINGS Interger Value To $$anonymous$$ainCamera Angle Script
var ca1 = cam.GetComponent(camAngle);
ca1.cameraAngle1 = 1;
//ASSIGNS Interger Value To SSPlayer Control Script
var ssp = GameObject.FindGameObjectWithTag("SSPlayer");
var sp1 = ssp.GetComponent(SSController);
sp1.cont1 = 2;
var sp2 = ssp.GetComponent(SSController2);
sp2.cont1 = 2;
}
}
Like I said.. It changes the integer in both scripts correctly, but it's like it's not updating on the second script.. Im very confused.. It's completely possible Im overseeing something simple and not getting the point you're both making.
I got it working thanks to ya.. Im not sure if this si what you were saying, but I changed the public variable for the second 1 to cont2 and made revisions in my trigger script as well.. If that's what you meant or not.. It made me realize it. thanks. I still think its strange that the way it was written b4 didnt work..
Answer by BLarentis · Aug 23, 2012 at 09:17 PM
Hello, Well you don't need two scripts for that, one simple alteration that you can do is this:
(SSController)
public var cont1 : int;
private var motor : CharacterMotor;
// Use this for initialization
function Awake () {
motor = GetComponent(CharacterMotor);
}
// Update is called once per frame
function Update () {
if (cont1 == 1){
// Get the input vector from kayboard or analog stick
var directionVector = new Vector3(Input.GetAxis("SSVertical"), 0, Input.GetAxis("SSHorizontal"));
if (directionVector != Vector3.zero) {
// Get the length of the directon vector and then normalize it
// Dividing by the length is cheaper than normalizing when we already have the length anyway
var directionLength = directionVector.magnitude;
directionVector = directionVector / directionLength;
// Make sure the length is no bigger than 1
directionLength = Mathf.Min(1, directionLength);
// Make the input vector more sensitive towards the extremes and less sensitive in the middle
// This makes it easier to control slow speeds when using analog sticks
directionLength = directionLength * directionLength;
// Multiply the normalized direction vector by the modified length
directionVector = directionVector * directionLength;
}
}else if(cont1 == 2){
// Get the input vector from kayboard or analog stick
var directionVector = new Vector3(Input.GetAxis("SSHorizontal2"), 0, Input.GetAxis("SSVertical2"));
if (directionVector != Vector3.zero) {
// Get the length of the directon vector and then normalize it
// Dividing by the length is cheaper than normalizing when we already have the length anyway
var directionLength = directionVector.magnitude;
directionVector = directionVector / directionLength;
// Make sure the length is no bigger than 1
directionLength = Mathf.Min(1, directionLength);
// Make the input vector more sensitive towards the extremes and less sensitive in the middle
// This makes it easier to control slow speeds when using analog sticks
directionLength = directionLength * directionLength;
// Multiply the normalized direction vector by the modified length
directionVector = directionVector * directionLength;
}
}
// Apply the direction to the CharacterMotor
motor.inputMoveDirection = transform.rotation * directionVector;
motor.inputJump = Input.GetButton("Jump");
}
// Require a character controller to be attached to the same game object
@script RequireComponent (CharacterMotor)
@script AddComponentMenu ("Character/FPS Input Controller")
Well, see if that works for you. This way changing the variable at the first script will make the same script do your second script functions.
If this doesn't work try to see if your configurations on SSHorizontal2 and SSVertical2 axis are OK. If your intention is to invert the inputs when you enter the trigger, you could try and use this:
var directionVector = new Vector3(Input.GetAxis("SSHorizontal"), 0, Input.GetAxis("SSVertical"));
instead of this:
var directionVector = new Vector3(Input.GetAxis("SSHorizontal2"), 0, Input.GetAxis("SSVertical2"));
After your else if. If that isn't what you want, please ignore. Take care.
I tried doing something like this originally but I get the error that I already defined the variable earlier on in the script for both the direction Vector and The Direction Length... So I tried renai$$anonymous$$g the variable to dv and dl and it tells me I cannot do that... this is why i have two scripts.. I tried his script you posted but it wont run. for the same reasons.. Im confused about it..
Your answer
Follow this Question
Related Questions
unity input system help 2 Answers
My player keeps launching into the sky 1 Answer
Don't jump continuously when button held down 0 Answers
Turning to face movement direction 0 Answers
Player inversed inputs 0 Answers