I have a character in my game that I want to control using the arrowkeys or WASD-keys. I can get the character to face the right direction if the arrowkeys are pressed, but I don’t know how to move him in 3D-space upon holding the key(s) down.
The script I have is a modified version from the Stealth-tutorial… I couldn’t use the exact same code for some reason, don’t know if it’s because the model isn’t the same or what it is. But I couldn’t import the animations to the Animator…
Anyways, is there a simple way to implement movement to my code? Oh and another thing… When the player pressed the Attack keys, the whole animation won’t play unless the Attack-keys are held down. How can I make it so that when the player presses the Attack-keys the whole animation will be played eventhou the player released the key?
I’m sorry for posting such stupid questions on the forum, I’ve searched the forum but couldn’t find a solution. Maybe I just suck at searching…
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
public float turnSmoothing = 7f;
void FixedUpdate()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
bool attack01 = Input.GetButton("Attack01");
bool attack02 = Input.GetButton("Attack02");
MovementManagement(h, v, attack01, attack02);
}
void MovementManagement(float horizontal, float vertical, bool attack01, bool attack02)
{
if(horizontal != 0f || vertical != 0f)
{
animation.CrossFade("Run");
Rotating(horizontal, vertical);
}
else
{
animation.CrossFade("Idle");
}
if(attack01)
{
animation.CrossFade("Attack01");
}
if(attack02)
{
animation.CrossFade("Attack02");
}
}
void Rotating(float horizontal, float vertical)
{
Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
rigidbody.MoveRotation(newRotation);
}
}