Pun 2 Synchronizing - c#

I created a multiplayer game with pun2 (Photon Unity Networking) but when I move and look actually changes of my character will apply to other characters, also when my friends move their changes will apply to my character
void Update(){
isGrounded = Physic.CheckSqhere(groundCheck.position,groundDistance, LayeMask);
if (isGrounded && velocity.y<0)
{
velocity.y=-1f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
Vector3 Movement = transform.right * x * transform.forward *z;
if (isGrounded)
{
characterController.Move(Movement * speed * Time.deltaTime)
}
else{
characterController.Move(Movement * speedOnJumping * Time.deltaTime)
}
velocity.y +=gravity * Time.deltaTime;
characterController.Move(velocity * Time.deltaTime);}
and
void Start(){
PhotonNetwork.ConnectUsingsettings();
}
public override void onConnectedToMaster(){
PhotonNetwork. AutomaticallySyncScene = true;
createButton.onclick.AddListener(()=>{
PhotonNetwork.createRoom("Amin's Room");
});
joinButton.onclick.AddListener(()=>{
PhotonNetwork.JoinRoom("Amin'sRoom");
});
base.OnConnectedToMaster();
}
public override void OnJoinedRoom(){
if (PhotonNetwork.IsMasterClient){
Debug.Log("you are the master");
PhotonNetwork.LoadLevel(1);
}
base.OnJoined Room();
}

For the future: Please add code as TEXT not as images to your questions!
In your Update method you listen to global keyboard events. These will be executed on all instances of that script.
You should check if the component actually belongs to your own player and otherwise ignore it or even better disable the entire component!
See Photon User Input Management
// Note that inheriting from MonoBehaviourPun instead of MonoBehaviour is mandatory!
public class YourClass : MonoBehaviourPun
{
...
private void Update()
{
// As also described in the link the IsConnected is checked in order to allow
// offline debugging of your behavior without being in a network session
if(PhotonNetwork.IsConnected && !photonView.IsMine)
{
// optional but why keep an Update method running of a script that
// shall anyway only be handled by the local player?
//enabled = false;
// Or you could even remove the entire component
//Destroy(this);
// Do nothing else
return;
}
...
}
}

Related

How can I make this unity movement script work?

How can I make this script work, or another alternative player movement script? This basic movement script in Unity doesn't do anything when i press WASD for some reason. I am a beginner at C# and unity and I kind of need an alternative player movement script that actually works.
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
transform.Translate(Vector3.forward);
if (Input.GetKeyDown(KeyCode.S))
transform.Translate(Vector3.back);
if (Input.GetKeyDown(KeyCode.A))
transform.Translate(Vector3.left);
if (Input.GetKeyDown(KeyCode.D))
transform.Translate(Vector3.right);
I recommend using the GetAxis("Horizontal") and GetAxis("Vertical") methods which maps wasd and arrow keys by default to help slim down your big if tree.
Below is a small example from an old unversity project of mine.
Note: the "* speed * Time.deltaTime" as this is used to help translate as time/frames move on, speed is just a multiplier in this case.
public float speed = 10.0f;
private float translation;
private float straffe;
// Use this for initialization
void Start()
{
// turn off the cursor
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
// Input.GetAxis() is used to get the user's input
// You can further set it on Unity. (Edit, Project Settings, Input)
translation = Input.GetAxis("Vertical") * speed * Time.deltaTime;
straffe = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
transform.Translate(straffe, 0, translation);
if (Input.GetKeyDown("escape"))
{
// turn on the cursor
Cursor.lockState = CursorLockMode.None;
}
}
Input.GetKeyDown checks if you pressed the w key on that frame. You need Input.GetKey. That checks if you are holding
You should use GetAxis("Horizontal") & GetAxis("Vertical");
Here is my basic code for my projects default movement:
//movement
Rigidbody rb;
float xInput;
float yInput;
public float speed;
public float defSpeed;
public float sprint;
public bool isGrounded = true;
public float jump;
// Start is called before the first frame update
private void Awake()
{
rb = GetComponent<Rigidbody>();
}
void Start()
{
}
// Update is called once per frame
void Update()
{
xInput = Input.GetAxis("Horizontal");
yInput = Input.GetAxis("Vertical");
//Things is still can't understand
Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
float cameraRot = Camera.main.transform.rotation.eulerAngles.y;
rb.position += Quaternion.Euler(0, cameraRot, 0) * input * speed * Time.deltaTime;
//sprint
if (Input.GetKeyDown(KeyCode.LeftShift))
{
speed = sprint;
}
else if (Input.GetKeyUp(KeyCode.LeftShift))
{
speed = defSpeed;
}
//jump
if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
{
rb.AddForce(0, jump, 0);
//Debug.Log("Jump!");
}
Hope this helps:)

Moving Object Not Stopping On Collision Unity

I am developing an infinite tower jumping game using Unity2D, and currently working on a continually moving object which causes the player to die if contact is made. The player can also die if they either fall off of a platform or off-screen. All methods of death rely on a BoxCollider2D I am using as a Killbox (tagged accordingly) - the player sprite has a RigidBody2D and BoxCollider2D attached to it - so there is one attached to the main camera (it moves as the player sprite progresses through the level) and to the top of the moving object.
The current code I have works up to the point where the game over screen appears on player death, but the object continues to move whilst everything else stops.
Here is my code for the moving object:
public class Water : MonoBehaviour {
private Collider2D playerCollider;
public ControllerNew thePlayer;
private float speed = 2f;
public GameManager theGameManager;
//reference scoremanager
private ScoreManager theScoreManager;
bool shouldMove = true;
// Start is called before the first frame update
void Start()
{
theScoreManager = FindObjectOfType<ScoreManager>();
thePlayer = FindObjectOfType<ControllerNew>();
lastPlayerPosition = thePlayer.transform.position;
}
// Update is called once per frame
void Update()
{
if (shouldMove = true)
{
transform.position += Vector3.up * speed * Time.deltaTime;
if (theScoreManager.scoreCount > 100 && shouldMove)
{
transform.position += Vector3.up * (speed+1) * Time.deltaTime;
}
if (theScoreManager.scoreCount > 250 && shouldMove)
{
transform.position += Vector3.up * (speed +2) * Time.deltaTime;
}
if (theScoreManager.scoreCount > 500 && shouldMove)
{
transform.position += Vector3.up * (speed+4) * Time.deltaTime;
}
if (theScoreManager.scoreCount > 1000 && theScoreManager.scoreCount > theScoreManager.hiScoreCount && shouldMove)
{
transform.position += Vector3.up * (speed+5) * Time.deltaTime;
}
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player New")
{
transform.position += Vector3.zero * (speed * 0) * Time.deltaTime;
}
//call object by its tag
if (other.gameObject.tag == "Killbox")
{
shouldMove = false;
if (shouldMove = false){
theGameManager.RestartGame();
transform.position += Vector3.zero * (speed * 0) * Time.deltaTime;
}
}
}
Edit (Issue resolved)
So it turns out that after adding the Water.StopMoving() method into my controller script, the water had not been called as a GameObject in void Start(). Once this was added, the water object stopped on collision.
Just want to say thank you #D.B for your help and bearing with me - apologies if the info I gave wasn't everything you needed to be able to assist me
You made a mistake on the first line of the Update() method :
if (shouldMove = true)
You set the bool to true, not comparing it. Use double = otherwise it will set the bool to true at every frame.
if (shouldMove == true)
By the way you can simplify this part :
//call object by its tag
if (other.gameObject.tag == "Killbox")
{
shouldMove = false;
theGameManager.RestartGame();
transform.position += Vector3.zero * (speed * 0) * Time.deltaTime;
}
(You forgot a = too)
I made a test with this simplified script
void Update()
{
if (shouldMove == true)
{
Debug.Log("move");
transform.position += Vector3.up * speed * Time.deltaTime * GetDifficultyFactor();
}
}
private float GetDifficultyFactor()
{
float factor = 1f;
if(theScoreManager.scoreCount > 100)
{
factor += 1f;
}
if (theScoreManager.scoreCount > 250)
{
factor += 2f;
}
// Add all your speed modification condition here
return factor;
}
void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("trigger");
//call object by its tag
if (other.gameObject.tag == "Killbox")
{
Debug.Log("die");
shouldMove = false;
}
}
And it work fine. Are you sure you have a collider2D set to trigger on your charactert with the tag "Killbox" (with the first letter in uppercase ?). You should have a rigidbody2d on the character too.
Mistake come frome another part of your code or with some trouble with collider2D/tag/RigidBody2D. Without seeing all it's difficult to help you more.
You should try to add some Debug.Log() or use debugeur with breakpoint to be sure code enter into your "die" statement and then not going on the Update if statement. If yes, it's mean you probably set the shouldMove variable in another part of your script.
Answer regarding discussion in comments
I think you want to make this OnTriggerEnter2D logic in both script.
Without seeing all your project I suggest you to make a reference bewteen your character and your water script. Then when the player die the player script will call a method on water to stop it.
public class Player : MonoBehaviour
{
public Water Water;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Killbox")
{
Debug.Log("die");
Water.StopMoving();
}
}
}
public class Water : MonoBehaviour
{
private bool shouldMove;
public void Update()
{
...
}
public void StopMoving()
{
shouldMove = false;
}
// No Trigger logic here
}

Enabled method doesn't seem to work on my Game Object component?

I'm developing a game in Unity at the moment and I ran into some trouble. For the past 2 days I've been trying to get a slowdown powerup to work but trough various means but it everything that I tried doesn't seem to work.
I changed the code to the corroutine sugested since this already solved allot of problems and narrow down the issue:
using UnityEngine;
using System.Collections;
public class ClockCollision : MonoBehaviour
{
public void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("White Ball"))
{
StartCoroutine(SlowMoveSpeed());
//this.gameObject.SetActive(false);
Debug.Log("White Ball Sensed");
}
}
public IEnumerator SlowMoveSpeed()
{
Debug.Log("White Ball Sensed Twice");
InteractControl.moveSpeed = 2f; // set slow speed
yield return new WaitForSeconds(5); // suspend the process for 5 seconds
InteractControl.moveSpeed = 10f; // 5 seconds later, set speed back
}
}
InteractControl script. I just found the reason why it doesn't seem to react to the corroutine calls:
public class InteractControl : MonoBehaviour, IPooledObject
{
private Rigidbody2D rb;
GameObject target;
Vector3 directionToTarget;
public static int LevelStart = 0;
//public GameObject[] Balls;
Renderer m_Renderer;
public static float moveSpeed = 5f;
public void OnObjectSpawn()
{
if (ScoreScript.scoreValue > 4 && LevelStart == 0) //If statement is causing the powerup from not generating non powerup activation problem. It is above the InteractControl.moveSpeed call in the hierarchy
{
moveSpeed = 10f;
}
//m_Renderer = GetComponent<Renderer>();
target = GameObject.FindWithTag("White Ball");
rb = GetComponent<Rigidbody2D>();
//Movement speed of all the obstacles and powerups
MoveInteract(moveSpeed); //Method responsable for the movement of the obstacles and powerups, gets called at start
}
void MoveInteract(float moveSpeed) //Method responsable for the movement of the obstacles and stars
{
if (target != null)
{
if(ScoreScript.scoreValue > 4) //Determine when RedBall goes from going down in a straigh line to following white ball
{
directionToTarget = (target.transform.position - transform.position).normalized;
rb.velocity = new Vector2(directionToTarget.x * moveSpeed,
directionToTarget.y * moveSpeed);
// Debug.Log(getMoveSpeed());
}
else
{
directionToTarget = new Vector3(0, -1, 0);
rb.velocity = new Vector2(0, directionToTarget.y * moveSpeed);
}
}
else
{
rb.velocity = Vector3.zero;
}
}
}
Apparently the moveSpeed value in the if statement I just added takes presedence over the value changes I make to moveSpeed in my ClockCollision class.
Does anyone know how I can choose which value assignment of a static variable (in this case moveSpeed) takes presedence over the other
What should happen is that all the gameobjects that InteractControl is attached too should slow down to a velocity of the their rigidbody based on a moveSpeed value of 2f instead of the initial 10f and switch back to 10f after 5 seconds. However what actually happens is that they continue to have a the same velocity they had before the powerup was catched since the script Clocktime never gets enabled. The script itself does work(except for the fact that it doesn't switch back to 10f after 5 seconds) if update does manage to get executed however since I only want update to be executed in certain situations I need to be able to enable and disable it.
As you can see from the images below unless I missed something all my gameobjects and scripts are assigned and attached correctly in order for enabled to work:
Does anyone know how I could solve this problem?
Thanks in advance
As is stated in the comment above Update(), it is executed once every frame. So you create a new StopWatch every frame, that never has the chance to go up to 5 seconds because you check its elapsed time immediately after creating it. You should alter your code to only create a Stopwatch once, but check its elapsed time every Update. That will solve at least one of your problems.
frankhermes is correct, this code
InteractControl.LevelStart++;
var stopWatch = new System.Diagnostics.Stopwatch();
stopWatch.Start();
should go in Start().
However, I'd highly recommend using a Coroutine instead of the stopwatch. It's easier and more efficient. Unless I'm missing something, there shouldn't be an issue with having this all in one script. You'd do something like this:
public void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("White Ball"))
{
StartCoroutine(SlowMoveSpeed());
this.gameObject.SetActive(false);
}
}
IEnumerator SlowMoveSpeed() {
InteractControl.moveSpeed = 2f; // set slow speed
yield return new WaitForSeconds(5); // suspend the process for 5 seconds
InteractControl.moveSpeed = 10f; // 5 seconds later, set speed back
}
EDIT
The reason why it is still not working seems to be with this function:
void MoveInteract(float moveSpeed)
{
if (target != null)
{
if(ScoreScript.scoreValue > 4)
{
directionToTarget = (target.transform.position - transform.position).normalized;
rb.velocity = new Vector2(directionToTarget.x * moveSpeed,
directionToTarget.y * moveSpeed);
}
else
{
directionToTarget = new Vector3(0, -1, 0);
rb.velocity = new Vector2(0, directionToTarget.y * moveSpeed);
}
}
else
{
rb.velocity = Vector3.zero;
}
}
This is called once at the beginning, setting the velocity for the rigidbody. However, when the moveSpeed is updated in the coroutine, this function is not called again.
I haven't dealt much with static functions, but you could try to create a public static void function that passes in moveSpeed and re-sets the rb.velocity to that move speed. You could call this function from the Coroutine.
Another option that would be much less efficient would be to update the rigidbody velocity every frame to equal the moveSpeed.
Personally, I'd probably use delegates and events. You might want to try this if a static method doesn't work.
http://www.unitygeek.com/delegates-events-unity/
I would create an event in ClockCollision, something like
public delegate void OnSpeedChange(float speed);
public static event OnSpeedChange onSpeedChangeDelegate;
You'd also have to call the delegate in the coroutine:
if(onSpeedChangeDelegate != null)
{
onSpeedChangeDelegate(2f); // This invokes the delegate
yield return new WaitForSeconds(5); // suspend the process for 5 seconds
onSpeedChangeDelegate(10f); // 5 seconds later, set speed back
}
Then in InteractControl I'd subscribe to the delegate like this:
void Awake(){
ClockCollision.onSpeedChangeDelegate += UpdateSpeed;
}
//Don't forget to unsubscribe:
void OnDestroy(){
ClockCollision.onSpeedChangeDelegate -= UpdateSpeed;
}
// Update the speed
void UpdateSpeed(moveSpeed)
{
if(ScoreScript.scoreValue > 4)
{
directionToTarget = (target.transform.position - transform.position).normalized;
rb.velocity = new Vector2(directionToTarget.x * moveSpeed,
directionToTarget.y * moveSpeed);
}
else
{
directionToTarget = new Vector3(0, -1, 0);
rb.velocity = new Vector2(0, directionToTarget.y * moveSpeed);
}
}
DISCLAIMER: I can't guarantee that this code is error-free since I haven't tested it.

Updating X,Y,Z floats and specific While Loop Scenario C#

I have 2 problems I want to get through.
1st problem. Changing X,Y,Z movement and updating it.
I have a driving mechanic in my game where if the user presses wasd the car moves. The problem is that I want the speed to be reduced from 15f to 5f when S(backwards) is pressed and held until released.
Here is my code below of my player script.
Please refer to void playerwalk() and backwards motion()
public class Move : MonoBehaviour
{
public float speed;
private Rigidbody rb;
private int count;
public Text countText;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
}
void playerWalk()
{
var x = Input.GetAxis("Horizontal") * Time.deltaTime * 75f;
var z = Input.GetAxis("Vertical") * Time.deltaTime * 15f;
transform.Rotate(0, x, 0);
transform.Translate(0, 0, -z);
}
void backwardMotion()
{
if (Input.GetKeyDown(KeyCode.S))
{
var z = Input.GetAxis("Vertical") * Time.deltaTime * 5f;
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Collectable"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
}
// Update is called once per frame
void Update ()
{
backwardMotion();
playerWalk();
}
}
This currently is not affecting anything and the void backwardsmotion() seems to have no effect.
Do I need a while loop?
What is the syntax for do until in C#?
2nd Problem : Do until loop?
To those of you who haven't seen my recent posts, I also have a flashlight mechanic on my car model where if you press L, the flashlights will turn on(toggle) and if you press S the backlights will as you reverse.
Here is my flashlight game mechanic code:
Please refer to Void switchoffLights()
public class headLights : MonoBehaviour
{
public Light frontlights;
public Light frontlights2;
public Light frontlights3;
public Light frontlights4;
public Light backlights;
public Light backlights2;
public Light backlights3;
public Light backlights4;
// Use this for initialization
void Start()
{
frontlights.enabled = !frontlights.enabled;
frontlights2.enabled = !frontlights2.enabled;
frontlights3.enabled = !frontlights3.enabled;
frontlights4.enabled = !frontlights4.enabled;
}
void switchoffLight()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.L))
{
if (frontlights && frontlights2 && frontlights3 && frontlights4 != null)
{
frontlights.enabled = !frontlights.enabled;
frontlights2.enabled = !frontlights2.enabled;
frontlights3.enabled = !frontlights3.enabled;
frontlights4.enabled = !frontlights4.enabled;
}
}
if (Input.GetKeyDown(KeyCode.S))
{
if (backlights && backlights2 && backlights3 && backlights4 != null)
{
backlights.enabled = !backlights.enabled;
backlights2.enabled = !backlights2.enabled;
backlights3.enabled = !backlights3.enabled;
backlights4.enabled = !backlights4.enabled;
}
}
}
}
However the backlights do not turn off when the S key is released.
I could not clearly understand what was going on with other codes and answers which is why I am asking here.
Thank you in advance! I am only just starting for my grade 10 programming class so please explain like you would to a younger person.
You are not applying any change to transform or rotation when pressing
if (Input.GetKeyDown(KeyCode.S))
{var z = Input.GetAxis("Vertical") * Time.deltaTime * 5f;
}
// Where this z go ?????
// Add this or whatever you want "transform.Translate(0, 0, -z);"
For Second question Use GetKeyUp() in another condition or use getkey in same condition
For the first question, you declared var z twice but they are actually individual values.
var z = Input.GetAxis("Vertical") * Time.deltaTime * 15f; // playerWalk()
and
var z = Input.GetAxis("Vertical") * Time.deltaTime * 5f; // backwardMotion()
refer to two different variables with the same name z but each only live in the method it is declared in. The z in playerWalk() and the z in backwardMotion() has nothing to do with each other. The z in backwardMotion() essentially serves no purpose as the variable is declared but never used anywhere in the method.
The correct way to achieve what you want will be:
void playerWalk()
{
var x = Input.GetAxis("Horizontal") * Time.deltaTime * 75f;
var z = Input.GetAxis("Vertical") * Time.deltaTime * 15f;
if (Input.GetKeyDown(KeyCode.S))
{
// If "S" is pressed, the z speed would be reduced to 5f.
z = Input.GetAxis("Vertical") * Time.deltaTime * 5f;
}
transform.Rotate(0, x, 0);
transform.Translate(0, 0, -z);
}
For the second question, there are three way to get input: Input.GetKeyDown, Input.GetKey and Input.GetKeyUp.
Input.GetKeyDown is called only once on the frame you pressed the key.
Input.GetKey is called every frame the key is held down.
Input.GetKeyUp is called only once on the frame you released the key.
In your case, your car backlight would be turned on when it detects a Input.GetKeyDown(KeyCode.S) but nothing lets it turn back off when it detects a release.
The solution would be to simply check for a release and turn off the headlight:
if (Input.GetKeyUp(KeyCode.S)) // Detects a release of "S" key
{
if (backlights && backlights2 && backlights3 && backlights4)
{
backlights.enabled = false;
backlights2.enabled = false;
backlights3.enabled = false;
backlights4.enabled = false;
}
}

How should I configure the Sprint in the InputManager?

This script is attached to Player object:
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (Rigidbody))]
[RequireComponent (typeof (BoxCollider))]
public class PlayerController : MonoBehaviour {
public float walkSpeed = 6;
public float runSpeed = 10;
public float strafeSpeed = 5;
public float gravity = 20;
public float jumpHeight = 2;
public bool canJump = true;
private bool isRunning = false;
private bool isGrounded = false;
public bool IsRunning
{
get { return isRunning; }
}
void Awake () {
GetComponent<Rigidbody>().freezeRotation = true;
GetComponent<Rigidbody>().useGravity = false;
}
void FixedUpdate () {
// get correct speed
float forwardAndBackSpeed = walkSpeed;
// if running, set run speed
if (isRunning) {
forwardAndBackSpeed = runSpeed;
}
// calculate how fast it should be moving
Vector3 targetVelocity = new Vector3(Input.GetAxis("Horizontal") * strafeSpeed, 0, Input.GetAxis("Vertical") * forwardAndBackSpeed);
targetVelocity = transform.TransformDirection(targetVelocity);
// apply a force that attempts to reach our target velocity
Vector3 velocity = GetComponent<Rigidbody>().velocity;
Vector3 velocityChange = (targetVelocity - velocity);
velocityChange.y = 0;
GetComponent<Rigidbody>().AddForce(velocityChange, ForceMode.VelocityChange);
// jump
if (canJump && isGrounded && Input.GetButton("Jump")) {
GetComponent<Rigidbody>().velocity = new Vector3(velocity.x, Mathf.Sqrt(2 * jumpHeight * gravity), velocity.z);
isGrounded = false;
}
// apply gravity
GetComponent<Rigidbody>().AddForce(new Vector3 (0, -gravity * GetComponent<Rigidbody>().mass, 0));
}
void Update() {
// check if the player is touching a surface below them
checkGrounded();
// check if the player is running
if (isGrounded && Input.GetButtonDown("Sprint")) {
isRunning = true;
}
// check if the player stops running
if (Input.GetButtonUp("Sprint")) {
isRunning = false;
}
}
void checkGrounded() {
/* ==============
* REMEMBER
* ==============
* If you change the size of the prefab, you may have
* to change the length of the ray to ensure it hits
* the ground.
*
* All obstacles/walls/floors must have rigidbodies
* attached to them. If not, Unity physics may get
* confused and the player can jump really high
* when in a corner between 2 walls for example.
*/
float rayLength = 0.7f;
RaycastHit hit;
Ray ray = new Ray(transform.position, -transform.up);
//Debug.DrawRay(ray.origin, ray.direction * rayLength);
// if there is something directly below the player
if (Physics.Raycast(ray, out hit, rayLength)) {
isGrounded = true;
}
}
}
There are some parts in the script it's using "Sprint"
For example:
// check if the player is running
if (isGrounded && Input.GetButtonDown("Sprint")) {
isRunning = true;
}
// check if the player stops running
if (Input.GetButtonUp("Sprint")) {
isRunning = false;
}
But "Spring" is not defined in the editor input:
Edit > Project Settings > Input:
I can change the size in the Input Manager to 19 and it will duplicate the Cancel so I changed the name to Sprint. But what should be the config for the Sprint ? It's now the Cancel config.
When running the game I'm getting this exception:
ArgumentException: Input Button Sprint is not setup.
To change the input settings use: Edit -> Project Settings -> Input
PlayerController.Update () (at Assets/My Scripts/Character1/PlayerController.cs:62)
This might sound silly, but did you save your project? If you save your scene, that is different than saving your project which is where the Project Settings are located.
File -> Save Scene (CTRL + S) is different than File -> Save Project
So this may be very old but I'm going to respond for those that may still be searching in vain:
When adding or changing an input in input manager to "Sprint". It would cause me an error saying it was not set up even if I placed correct naming conventions for every positive or negative button even the alts.
The only fix was to change the name from "Sprint" to "Run" or just about anything works except for "Sprint" for whatever crazy Unity reason there is. Hope this helps those that have been going insane like I did when I first encountered this.

Categories

Resources