Third Person Controller C#
I need to know where to fix my Jump feature in this script. Here are the basic issues.
I cannot add gravity with isGrounded. I don't know how to apply it and tried several ways.
When i hold spacebar character turns slightly to the left and continues to climb.
If i hold forward movement down while jumping character is locked in jump pose till I stop holding down one of the keys.
I would also like to have a double hit option on '/?' key to shift from run or walk but don't know how to do so and stuck using the left ctrl button.
Here is the code; any modifications will be greatly appreciated and please ass a note where the code was added in the same manner as this code has been provided or I will likely mess it up.
using System;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Animator anim;
public Rigidbody Rb;
public float speed;
private bool run;
private bool jump;
public float inputH = 20f;
public float inputV = 50f;
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
Rb = GetComponent<Rigidbody>();
inputV = inputH = 0;
run = false;
jump = false;
}
// Update is called once per frame
void Update()
{
inputH = Input.GetAxis("Horizontal");
inputV = Input.GetAxis("Vertical");
anim.SetFloat("inputH", inputH);
anim.SetFloat("inputV", inputV);
anim.SetBool("run", run);
anim.SetBool("jump", jump);
float moveX = inputH * 20f * Time.deltaTime;
float moveZ = inputV * 50f * Time.deltaTime;
if (moveZ <= 0f)
{
moveX = 0f;
}
else if (run)
{
moveX *= 6f;
moveZ *= 6f;
}
if (Input.GetKey(KeyCode.LeftControl))
{
run = true;
}
else
{
run = false;
}
if (Input.GetKey(KeyCode.Space))
{
jump = true;
}
else
{
jump = false;
}
}
}
I don't know anything about applying any other physics for things like slope detection or walking up and down various stairs or ramps but it seems to be working fine as is aside from the jump and gravity issues, and of course my interest in coding the changing run to walk so i only have to hold down one key. Its intended for a Online PC game.
Your answer
Follow this Question
Related Questions
How to prevent jumping off held object? 0 Answers
Remove Rigidbody2D "Jelly" Collisions? 1 Answer
How to create a generic script for bouncing behaviour 1 Answer
Rigidbody 2D Not Working Stable 0 Answers