- Home /
Question by
RudyPython · Dec 16, 2014 at 07:53 PM ·
third-personthird-person-shooter
Third Person Shooter help?
Hi friends,
I am building this Third Person Shooter game, I already have the basic animations for the character such as Walk, Run, etc... I am having trouble making the character draw a weapon. Also, I want the left hand of the character to be in the gun. I know the Inverse Kinematics (IK) is used for this, but I don't really know IK.
Can anyone help me with this or reference me to a tutorial? Thanks in advance.
Here is a sample of my code:
using UnityEngine; using System.Collections;
[RequireComponent(typeof(Animator))] [RequireComponent(typeof(CapsuleCollider))] [RequireComponent(typeof(Rigidbody))] public class CharacterControl : MonoBehaviour {
public float animSpeed = 1.5f;
public float walkSpeed = 4.0f;
public float runSpeed = 8.0f;
private Animator anim;
private AnimatorStateInfo currentBaseState;
private CapsuleCollider col;
private bool run = false;
private bool IsGrounded = true;
private bool strafeR = false;
private bool strafeL = false;
static int idleState = Animator.StringToHash ("Base Layer.Idle");
static int walkState = Animator.StringToHash ("Base Layer.Walk");
static int strafeLeft = Animator.StringToHash ("Base Layer.StrafeLeft");
static int strafeRight = Animator.StringToHash ("Base Layer.StrafeRight");
static int runState = Animator.StringToHash ("Base Layer.Run");
static int jumpState = Animator.StringToHash ("Base Layer.Jump");
void Start ()
{
// initialising reference variables
anim = GetComponent<Animator> ();
col = GetComponent<CapsuleCollider> ();
if (anim.layerCount == 2)
anim.SetLayerWeight (1, 1);
run = false;
strafeR = false;
strafeL = false;
}
void FixedUpdate ()
{
float h = Input.GetAxis ("Horizontal");
float v = Input.GetAxis ("Vertical");
anim.SetFloat ("Speed", v);
anim.SetFloat ("Direction", h);
anim.speed = animSpeed;
anim.SetLookAtWeight (lookWeight);
currentBaseState = anim.GetCurrentAnimatorStateInfo (0);
CheckRun ();
StrafeWalk ();
}
void OnCollisionEnter (Collision collider)
{
IsGrounded = true;
}
public void CheckRun ()
{
if (Input.GetKey (KeyCode.LeftShift))
run = true;
else
run = false;
if (run == true) {
anim.SetBool ("Run", true);
}
if (run == false) {
anim.SetBool ("Run", false);
}
}
public void StrafeWalk ()
{
if (Input.GetKey (KeyCode.Q))
strafeL = true;
else
strafeL = false;
if (strafeL == true) {
anim.SetBool ("StrafeL", true);
}
if (strafeL == false) {
anim.SetBool ("StrafeL", false);
}
if (Input.GetKey (KeyCode.E))
strafeR = true;
else
strafeR = false;
if (strafeR == true) {
anim.SetBool ("StrafeR", true);
}
if (strafeR == false) {
anim.SetBool ("StrafeR", false);
}
}
}
Comment