How to make the character walk-jump and walk (and resize the collider when crouched) ?
I am new to unity and I wrote this code and I'm trying to make my character walk even when I press the keys: A S D. I only have an animation that moves forward. I want my character in 3d person, so maybe always moving forward can be fixed with: first turning around then walk (could this make it work or look better ?) (I don't know how to make the camera follow the character but not turing around with him tho). Also, I wish that my character could jump while walking. Now it responds very slow when I press the spacebar key. And last one: I try to resize the Capsule Collider when crouched but this code won't work. Maybe someone know how to fix one of this things? Please help :)
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Animator))]
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(CapsuleCollider))]
public class ActionsC : MonoBehaviour
{
private Animator animator;
CapsuleCollider m_Capsule;
float m_CapsuleHeight;
Vector3 m_CapsuleCenter;
private void Start()
{
animator = GetComponent<Animator>();
// for capsule collider resizing
m_Capsule = GetComponent<CapsuleCollider>();
m_CapsuleHeight = m_Capsule.height;
m_CapsuleCenter = m_Capsule.center;
}
private void Update()
{
Walk();
Run();
Sitting();
Jump();
Aiming();
}
// idle animation
public void Stay()
{
animator.SetBool("Aiming", false);
animator.SetFloat("Speed", 0f);
}
public void Walk()
{
float xMov = Input.GetAxisRaw("Horizontal");
float zMov = Input.GetAxisRaw("Vertical");
if (Input.GetKey(KeyCode.W))
{
animator.SetBool("Aiming", false);
animator.SetFloat("Speed", 0.5f);
}
// I don't have animation for these so idk what to do
else if (Input.GetKey(KeyCode.W))
{
}
else if (Input.GetKey(KeyCode.A))
{
}
else if (Input.GetKey(KeyCode.D))
{
}
else Stay();
}
public void Jump()
{
if (Input.GetKeyDown(KeyCode.Space)){
animator.SetBool("Squat", false);
animator.SetBool("Aiming", false);
animator.SetTrigger("Jump");
rigid.AddForce(Vector3.up * 100.0f);
}
// walk and jump
else if (Input.GetKeyDown(KeyCode.W))
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (Input.GetKeyDown(KeyCode.LeftShift)) { speed = 40.0f; }
animator.SetBool("Squat", false);
animator.SetFloat("Speed", 0f);
animator.SetBool("Aiming", false);
animator.SetTrigger("Jump");
rigid.AddForce(Vector3.up * 100.0f);
}
}
}
//crouching
public void Sitting()
{
if (Input.GetKeyDown(KeyCode.C)){
animator.SetBool("Squat", !animator.GetBool("Squat"));
animator.SetBool("Aiming", false);
}
}
// trying to scale the collider when crouched
void ScaleCapsuleForCrouching(bool crouch)
{
if (Input.GetKeyDown(KeyCode.C))
{
if (Input.GetKeyDown(KeyCode.C)) return;
m_Capsule.height = m_Capsule.height / 2f;
m_Capsule.center = m_Capsule.center / 2f;
}
}
}
Good day.
Your question is correct, but maybe you should do separate questions for each issue.
FAQ :
What are the guidelines for writing good questions?
We want Unity Answers to become a comprehensive database of questions and answers related to Unity. When you ask your questions, it can be helpful to keep a few things in $$anonymous$$d:
Don't write things that will be irrelevant in a few days. Writing that "This is urgent!" will probably not get you faster answers, but it may make people frown at your question.
Some reasons for getting a post rejected:
There exists duplicate questions with answers if you were to do a search, either on Answers or on the Forum or on Unity's tutorials
Your question isn't specific enough: asking for a script, asking multiple questions in one post or asking a question that could be either subjective or require extensive discussion
Your answer
Follow this Question
Related Questions
Fast start and ending at jump but slow middle. 0 Answers
How do I Instantiate a prefab at the x axis position of where the UI button was clicked 1 Answer
Moving an object in a curve along the z-axis 0 Answers
Still constant jumping even with raycasting. 0 Answers
Collision in the air after jumping 1 Answer