Hello everyone. I finally got most of the code for third person controller working as far as animations goes, but its still having issues. I ended up scrapping everything and started over.
Here is the problem.
- No errors are showing in Engine for this script, yet…
A: Gravity/Grounded settings are not working.
B: Jump power settings are not working at all.
C: Character falls slowly when running off edge/after jump:
D: Character stays in jump if holding space-bar down.
Additional issue:
- One of the key features i also need this script to do is allow me to double hit the left control key the change states from running to walking and double hit again to change back from running to walk. This way i do not need to “hold down the left ctrl” or any other button to remain in a constant run or walk state. Currently this code allows me to use either WASD or Arrows which i wish to keep.
If anyone here knows how to fix this script and willing to volunteer the assist so it works with my Player Animator, it would be much appreciated. (A good percent of this code simply is not working and I do not understand where to place additional functions such as crouching, swimming etc.).
Here is the code:
public Animator anim;
public Rigidbody Rb;
public float speed;
public float inputH = 20f;
public float inputV = 50f;
private bool run;
private bool jump;
[System.Serializable]
public class MoveSettings
{
public float jumpVel = 20;
public float distToGrounded = 1.1f;
public LayerMask ground;
}
[System.Serializable]
public class PhysSettings
{
public float downAccel = 1.5f;
}
[System.Serializable]
public class InputSettings
{
public float InputDelay = 0f;
}
public MoveSettings moveSetting = new MoveSettings();
public PhysSettings physSetting = new PhysSettings();
public InputSettings inputSetting = new InputSettings();
Vector3 velocity = Vector3.zero;
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
Rb = GetComponent<Rigidbody>();
run = false;
jump = false;
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.LeftControl)) // want to changee this to double click to swicth from walk to run and back again.
{
run = true;
}
else
{
run = false;
}
if (Input.GetKey(KeyCode.Space))
{
jump = true;
}
else
{
jump = false;
}
velocity.y = moveSetting.jumpVel;
{
velocity.y -= physSetting.downAccel;
}
inputH = Input.GetAxis("Horizontal");
inputV = Input.GetAxis("Vertical");
Rb.velocity = transform.TransformDirection(velocity);
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;
}
Rb.velocity = new Vector3(moveX, 0f, moveZ * speed);
///Need to add physics.
}
bool Grounded()
{
return Physics.Raycast(transform.position, Vector3.down, moveSetting.distToGrounded, moveSetting.ground);
}
}
Also I am not entirely sure I did not do something wrong here in the Animator.