- Home /
 
Player moving without input
Hi. I had modified the FPScontroller to make it work with a joystick, in a multiplayer split screen. It works great, until I try to use the second player. The second player has the same script, but obviously with different buttons (and axes) assigned. The problem is that when I hit play, the second player starts to move by itself on the Z axis. The inputs are assigned as they should in the proyect settings and on the inspector(2 days checking the same). I think the problem could be at the fixed update that allows the character to move.
 using System;
 using UnityEngine;
 using UnityStandardAssets.CrossPlatformInput;
 
 namespace UnityStandardAssets.Characters.FirstPerson
 {
     [RequireComponent(typeof(Rigidbody))]
     [RequireComponent(typeof(CapsuleCollider))]
 
     public class NewcharController : MonoBehaviour
 {
 
      
         [Serializable]
     public class MovementSettings
     {
         public float ForwardSpeed = 8.0f;   // Speed when walking forward
         public float BackwardSpeed = 4.0f;  // Speed when walking backwards
         public float StrafeSpeed = 4.0f;    // Speed when walking sideways
         public float RunMultiplier = 2.0f;   // Speed when sprinting
         public KeyCode RunKey = KeyCode.LeftShift;
         public float JumpForce = 30f;
         public AnimationCurve SlopeCurveModifier = new AnimationCurve(new Keyframe(-90.0f, 1.0f), new Keyframe(0.0f, 1.0f), new Keyframe(90.0f, 0.0f));
         [HideInInspector] public float CurrentTargetSpeed = 8f;
 
            
 
 
 
 #if !MOBILE_INPUT
             private bool m_Running;
 #endif
 
         public void UpdateDesiredTargetSpeed(Vector2 input)
         {
             if (input == Vector2.zero) return;
             if (input.x > 0 || input.x < 0)
             {
                 //strafe
                 CurrentTargetSpeed = StrafeSpeed;
             }
             if (input.y < 0)
             {
                 //backwards
                 CurrentTargetSpeed = BackwardSpeed;
             }
             if (input.y > 0)
             {
                 //forwards
                 //handled last as if strafing and moving forward at the same time forwards speed should take precedence
                 CurrentTargetSpeed = ForwardSpeed;
             }
 #if !MOBILE_INPUT
             if (Input.GetKey(RunKey))
             {
                 CurrentTargetSpeed *= RunMultiplier;
                 m_Running = true;
             }
             else
             {
                 m_Running = false;
             }
 #endif
         }
 
 #if !MOBILE_INPUT
         public bool Running
         {
             get { return m_Running; }
         }
 #endif
     }
 
 
     [Serializable]
     public class AdvancedSettings
     {
         public float groundCheckDistance = 0.01f; // distance for checking if the controller is grounded ( 0.01f seems to work best for this )
         public float stickToGroundHelperDistance = 0.5f; // stops the character
         public float slowDownRate = 20f; // rate at which the controller comes to a stop when there is no input
         public bool airControl; // can the user control the direction that is being moved in the air
         [Tooltip("set it to 0.1 or more if you get stuck in wall")]
         public float shellOffset; //reduce the radius by that ratio to avoid getting stuck in wall (a value of 0.1f is nice)
     }
 
 
     public Camera cam;
     public MovementSettings movementSettings = new MovementSettings();
     public MouseLook mouseLook = new MouseLook();
     public AdvancedSettings advancedSettings = new AdvancedSettings();
 
 
     private Rigidbody m_RigidBody;
     private CapsuleCollider m_Capsule;
     private float m_YRotation;
     private Vector3 m_GroundContactNormal;
     private bool m_Jump, m_PreviouslyGrounded, m_Jumping, m_IsGrounded;
        
         /// <JOYROTATIONVARS>
         float xSpeed = 250.0f;
         float ySpeed = 120.0f;
         public int yMinLimit = -20;
         public int yMaxLimit = 80;
         private float x = 0.0f;
         private float y = 0.0f;
         /// </summary>
         /// JOYSTICKBUTTONSNAMES//
         public string AbuttonName;
         public string BbuttonName;
         public string XbuttonName;
         public string YbuttonName;
         public string LeftJHName;
         public string LeftJVName;
         public string RigthJHName;
         public string RigthJVName;
         Vector3 moveDirection = Vector3.zero;
         public KeyCode joysticknumber;
 
 
         public Vector3 Velocity
     {
         get { return m_RigidBody.velocity; }
     }
 
     public bool Grounded
     {
         get { return m_IsGrounded; }
     }
 
     public bool Jumping
     {
         get { return m_Jumping; }
     }
 
     public bool Running
     {
         get
         {
 #if !MOBILE_INPUT
             return movementSettings.Running;
 #else
                 return false;
 #endif
         }
     }
 
 
     private void Start()
     {
         m_RigidBody = GetComponent<Rigidbody>();
         m_Capsule = GetComponent<CapsuleCollider>();
         mouseLook.Init(transform, cam.transform);
         }
 
 
     private void Update()
     {
         //RotateView();
 
         if (Input.GetKeyDown(joysticknumber) && !m_Jump)
         {
             m_Jump = true;
         }
     }
 
     
     private void FixedUpdate()
     {
         GroundCheck();
         Vector2 input = GetInput();
 
         if ((Mathf.Abs(input.x) > float.Epsilon || Mathf.Abs(input.y) > float.Epsilon) && (advancedSettings.airControl || m_IsGrounded))
         {
             // always move along the camera forward as it is the direction that it being aimed at
             Vector3 desiredMove = cam.transform.forward * Input.GetAxis(LeftJVName) + cam.transform.right * Input.GetAxis(LeftJHName);
             desiredMove = Vector3.ProjectOnPlane(desiredMove, m_GroundContactNormal).normalized;
        
             desiredMove.x = desiredMove.x * movementSettings.CurrentTargetSpeed;
             desiredMove.z = desiredMove.z * movementSettings.CurrentTargetSpeed;
             desiredMove.y = desiredMove.y * movementSettings.CurrentTargetSpeed;
             if (m_RigidBody.velocity.sqrMagnitude <
                 (movementSettings.CurrentTargetSpeed * movementSettings.CurrentTargetSpeed))
             {
                 m_RigidBody.AddForce(desiredMove * SlopeMultiplier(), ForceMode.Impulse);
             }
         }
 
         if (m_IsGrounded)
         {
             m_RigidBody.drag = 5f;
 
             if (m_Jump)
             {
                 m_RigidBody.drag = 0f;
                 m_RigidBody.velocity = new Vector3(m_RigidBody.velocity.x, 0f, m_RigidBody.velocity.z);
                 m_RigidBody.AddForce(new Vector3(0f, movementSettings.JumpForce, 0f), ForceMode.Impulse);
                 m_Jumping = true;
             }
 
             if (!m_Jumping && Mathf.Abs(input.x) < float.Epsilon && Mathf.Abs(input.y) < float.Epsilon && m_RigidBody.velocity.magnitude < 1f)
             {
                 m_RigidBody.Sleep();
             }
         }
         else
         {
             m_RigidBody.drag = 0f;
             if (m_PreviouslyGrounded && !m_Jumping)
             {
                 StickToGroundHelper();
             }
         }
         m_Jump = false;
     }
 
 
     private float SlopeMultiplier()
     {
         float angle = Vector3.Angle(m_GroundContactNormal, Vector3.up);
         return movementSettings.SlopeCurveModifier.Evaluate(angle);
     }
 
         void LateUpdate()
         {
             #region Rotacion
             x += (Input.GetAxis(RigthJHName) * xSpeed * 0.02f);
             y -= (Input.GetAxis(RigthJVName) * ySpeed * 0.02f);
             y = ClampAngle(y, yMinLimit, yMaxLimit);
             var rotation = Quaternion.Euler(y, x, 0);
             transform.rotation = rotation;
             #endregion
 
         }
         float ClampAngle(float angle, float min, float max)
         {
             if (angle < -360) angle += 360;
             if (angle > 360) angle -= 360;
             return (Mathf.Clamp(angle, min, max));
         }
 
 
         private void StickToGroundHelper()
     {
         RaycastHit hitInfo;
         if (Physics.SphereCast(transform.position, m_Capsule.radius * (1.0f - advancedSettings.shellOffset), Vector3.down, out hitInfo,
                                ((m_Capsule.height / 2f) - m_Capsule.radius) +
                                advancedSettings.stickToGroundHelperDistance, Physics.AllLayers, QueryTriggerInteraction.Ignore))
         {
             if (Mathf.Abs(Vector3.Angle(hitInfo.normal, Vector3.up)) < 85f)
             {
                 m_RigidBody.velocity = Vector3.ProjectOnPlane(m_RigidBody.velocity, hitInfo.normal);
             }
         }
     }
 
 
     private Vector2 GetInput()
     {
 
         Vector2 input = new Vector2
         {
             x = Input.GetAxis(LeftJHName),
             y = Input.GetAxis(LeftJVName)
         };
         movementSettings.UpdateDesiredTargetSpeed(input);
         return input;
     }
 
 
     private void RotateView()
     {
         //avoids the mouse looking if the game is effectively paused
         if (Mathf.Abs(Time.timeScale) < float.Epsilon) return;
 
         // get the rotation before it's changed
         float oldYRotation = transform.eulerAngles.y;
 
         mouseLook.LookRotation(transform, cam.transform);
 
         if (m_IsGrounded || advancedSettings.airControl)
         {
             // Rotate the rigidbody velocity to match the new direction that the character is looking
             Quaternion velRotation = Quaternion.AngleAxis(transform.eulerAngles.y - oldYRotation, Vector3.up);
             m_RigidBody.velocity = velRotation * m_RigidBody.velocity;
         }
     }
 
     /// sphere cast down just beyond the bottom of the capsule to see if the capsule is colliding round the bottom
     private void GroundCheck()
     {
         m_PreviouslyGrounded = m_IsGrounded;
         RaycastHit hitInfo;
         if (Physics.SphereCast(transform.position, m_Capsule.radius * (1.0f - advancedSettings.shellOffset), Vector3.down, out hitInfo,
                                ((m_Capsule.height / 2f) - m_Capsule.radius) + advancedSettings.groundCheckDistance, Physics.AllLayers, QueryTriggerInteraction.Ignore))
         {
             m_IsGrounded = true;
             m_GroundContactNormal = hitInfo.normal;
         }
         else
         {
             m_IsGrounded = false;
             m_GroundContactNormal = Vector3.up;
         }
         if (!m_PreviouslyGrounded && m_IsGrounded && m_Jumping)
         {
             m_Jumping = false;
         }
     }
 }
 }
 
              Answer by javim93 · Oct 06, 2017 at 01:12 PM
I fixed it by chaging the dead zone of the second joystick in the input settings on the inspector.
Answer by R1P4R14N · Feb 02 at 03:53 AM
This type of behavior is also caused by a physics related issue and friction causing your object to spin/move just as described. If the input problem isn't your issue look into your rigidbody/collider interactions.
Your answer
 
             Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Cant move FPS controller 1 Answer
UnityScript How to Detect Movement from FPS Controller 0 Answers
FPS controller, only for use on object 0 Answers
Help with fps script 2 Answers