New Movement Script Not Registering Keyboard Input
When I create a movement script apart from the ones included in the standard assets, in play mode the keyboard does not work. I have tried re writing the script and there are no errors. and when I try importing other scripts or using other tutorials, none of them work. This seems to only happen in Unity 5.1 and onwards. It seems to be an ongoing problem and all of the "remedies" that are detailed do not work. I have tried editing the registry as seen here "http://answers.unity3d.com/questions/372239/basic-keyboard-input-not-working.html". But I cannot find the Input registry key. I have also tried many other remedies to no avail.
Here is my script:
using UnityEngine; using System.Collections;
public class SpyController: MonoBehaviour { #region Component Variables Rigidbody r; Animator anim; CapsuleCollider capCol; [SerializeField] PhysicMaterial zfriction; [SerializeField] PhysicMaterial mfriction; Transform cam; #endregion
[SerializeField] float speed = 4;
[SerializeField] float turnSpeed = 5;
[SerializeField] float jumpPower = 1;
Vector3 directionPos;
Vector3 lookPos;
Vector3 storeDir;
float horizontal;
float vertical;
bool jumpInput;
bool onGround;
void Start()
{
r = GetComponent<Rigidbody>();
cam = Camera.main.transform;
capCol = GetComponent<CapsuleCollider>();
}
void Update()
{
HandleFriction();
}
void FixedUpdate()
{
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
jumpInput = Input.GetButtonDown("Jump");
if (horizontal == 0)
{
storeDir = cam.right;
}
if (onGround)
{
r.AddForce(((storeDir * horizontal) + (cam.forward * vertical)) * speed / Time.deltaTime);
if (jumpInput)
{
anim.SetTrigger("Jump");
r.AddForce(Vector3.up * jumpPower, ForceMode.Impulse);
}
}
directionPos = transform.position + (storeDir * horizontal) + (cam.forward * vertical);
Vector3 dir = directionPos - transform.position;
dir.y = 0;
float animValue = Mathf.Abs(vertical) + Mathf.Abs(horizontal);
anim.SetFloat("Forward", animValue, .1f, Time.deltaTime);
if (horizontal != 0 || vertical != 0)
{
float angle = Quaternion.Angle(transform.rotation, Quaternion.LookRotation(dir));
if (angle != 0)
r.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(dir), turnSpeed * Time.deltaTime);
}
}
void OnCollisionEnter(Collision other)
{
onGround = true;
r.drag = 5;
anim.SetBool("onAir", false);
}
void OnCollisionExit(Collision other)
{
onGround = false;
r.drag = 0;
anim.SetBool("onAir", true);
}
void HandleFriction()
{
if (horizontal == 0 && vertical == 0)
{
capCol.material = mfriction;
}
else
{
capCol.material = zfriction;
}
}
}
I need help because any script that requires input from the user doesn't work.
Any help would be appreciated!
Not sure if this is the only problem, but in general you should never get input in FixedUpdate(), only in Update(). Try moving the code where you read the input axes and see if that helps.
Hmm, I guess not, eh? I re-read your question and it sounds like an environment-wide problem if all the scripts you try don't work. Have you tried re-installing Unity? If it's still not working, try starting a thread on the forum. The QA staff check it regularly and could probably help you.
Your answer
![](https://koobas.hobune.stream/wayback/20220612045333im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Touch Controls without virtual joystick 0 Answers
How do I adjust code so gyro translates player to center position when device is being held in hands 1 Answer
Making an object move non stop after button input? 1 Answer
What is the key mapping for a Logitech Extreme 3D Pro joystick in Unity? 0 Answers
Mobile touch 0 Answers