'Unity Game Engine: Making 3D player Jump Failure
Every time I add something new to the script, it works once then never works again for my player movement. The basic movements and run works (although the transition from walk to run and run to walk when hitting the r key is a bit sticky) mostly as intended (I want to keep the negative movements in the scripting and dont want the player turning to face the camera).
The problem is I have tried to add several different Jump scripting from such as Unity's Doc samples, and even the various YouTube tutorials. If I get it working without animation one of 4 issues occur.
- Never works at all
- It jumps but then wont allow any other movement (jumps only in place).
- It jumps a couple times and then is permanently disabled (no longer works) even though there hasn't been any changes.
- If it does work for a few minutes, the moment I try to start adding animations, it won't fully play through a lend tree for those animations regardless what adjustments are made to the animations and the animator, and then the script ceases to allow any jumping (as if its no longer in the script).
The Scripting
`using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField]
public enum State { loco, jump };
[SerializeField]
public State state = State.loco;
[SerializeField]
[Range(1, 10)] public float moveSpeed;
[SerializeField]
[Range(0, 10)] public float speedMultiplier = 5;
[SerializeField]
[Range(0, 10)] public float rotSpeed = 2;
[SerializeField]
public bool isGrounded;
[SerializeField]
public bool isRunning;
[SerializeField]
public Vector3 jump;
[SerializeField]
[Range(0, 10)] public float jumpForce = 0.01f;
private Animator anim;
private Rigidbody rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
anim = GetComponent<Animator>();
jump = new Vector3(0.0f, 2.0f, 0.0f);
}
void OnCollisionStay()
{
isGrounded = true;
}
// Update is called once per frame
void Update()
{
Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
anim.SetFloat("InputX", input.x * moveSpeed);
anim.SetFloat("InputY", input.z * moveSpeed);
//Uses reverse/mirrored movement
if (state == State.loco)
{
float x = Input.GetAxisRaw("Vertical") < 0 ? -input.x : input.x;
rb.angularVelocity = rotSpeed * x * Vector3.up;
}
//Jump is still not working and as shown there is no attempted call of animator
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(jump * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
}
void FixedUpdate()
{
//Run usinf 'r' to toggle run on or off.
if (Input.GetButtonDown("Run"))
{
isRunning = !isRunning;
}
if (isRunning)
{
moveSpeed = 1.5f;
}
else
{
moveSpeed = 0.5f;
MovePlayer();
}
}
//Applies Movement
void MovePlayer()
{
Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
transform.TransformVector(new Vector3(0, 0, input.z * moveSpeed * speedMultiplier));
}
}`
Everything, and expecting it to work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
