Hi, this is my first time posting a thread on the Unity Forums, so bare with me
Since a few weeks I’ve been playing around with Unity and so far I love it. Have build some nice levels and have a great idea for a game.
Been following tutorials from -Digital Tutors- on the introduction and creating your first game tutorials and lots more from Unity itself.
Now I’m not a scripter and never used C# extensively before. So I’m quite happy about the fact that I got this far with little understanding of what it is (haha)
The thing is: I got a basic movement on my player (which is walking)
I can change the parameters and animations of the animation, aswell as the code that I made for the walking.
So I have the animation “walking” (which works) but want to add a running animation when you press and hold the “Left Shift” key.
How can I place that correctly in my script (which i learned from DT).
using UnityEngine;
using System.Collections;
public class CharacterMovement : MonoBehaviour
{
public float speed = 6f; //The speed that the player will move.
public float turnSpeed = 60f; //
public float turnSmooting = 15f;
private Vector3 movement;
private Vector3 turning;
private Animator anim;
private Rigidbody playerRigidbody;
void Awake ()
{
//Get references
anim = GetComponent<Animator>();
playerRigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
//Store input axes
float lh = Input.GetAxisRaw ("Horizontal");
float lv = Input.GetAxisRaw ("Vertical");
Move (lh, lv);
Animating (lh, lv);
}
void Move(float lh, float lv)
{
//Move the player
movement.Set (lh, 0f, lv);
movement = movement.normalized * speed * Time.deltaTime;
playerRigidbody.MovePosition (transform.position + movement);
if(lh != 0f || lv != 0f)
{
Rotating (lh, lv);
}
}
void Rotating(float lh, float lv)
{
Vector3 targetDirection = new Vector3 (lh, 0f, lv);
Quaternion targetRotation = Quaternion.LookRotation (targetDirection, Vector3.up);
Quaternion newRotation = Quaternion.Lerp (GetComponent<Rigidbody> ().rotation, targetRotation, turnSmooting * Time.deltaTime);
GetComponent<Rigidbody> ().MoveRotation (newRotation);
}
void Animating(float lh, float lv)
{
bool running = lh != 0f || lv != 0f;
anim.SetBool ("IsRunning", running);
}
}
Thank you in advance.
PS: If there is another way to get a script to work in a faster or more understandable way :p, please.