Question by
yasloxamine · Apr 06, 2019 at 09:19 PM ·
scripting problem
How to change my script to 8 directional controller ?
Hi, i want to change my player script to 8 directional controller so when i push the left and right button he goes Horizontally , up and down button he goes Vertically, here is my script and thank you for your help.
script :
using UnityEngine; using System.Collections;
public class Player : MonoBehaviour {
private Animator anim;
private CharacterController controller;
public float speed = 12.0f;
public float turnSpeed = 200.0f;
private Vector3 moveDirection = Vector3.zero;
public float gravity = 20.0f;
void Start()
{
controller = GetComponent<CharacterController>();
anim = gameObject.GetComponentInChildren<Animator>();
}
void Update()
{
if (Input.GetKey("up"))
{
anim.SetInteger("AnimationPar", 1);
}
else
{
anim.SetInteger("AnimationPar", 0);
}
if (controller.isGrounded)
{
moveDirection = transform.forward * Input.GetAxis("Vertical") * speed;
}
float turn = Input.GetAxis("Horizontal");
transform.Rotate(0, turn * turnSpeed * Time.deltaTime, 0);
controller.Move(moveDirection * Time.deltaTime);
moveDirection.y -= gravity * Time.deltaTime;
}
}
Comment
Your answer
Follow this Question
Related Questions
Can not show/hide a game object in Unity 3D V2018.3.9 f1 2 Answers
SetSpeed float C# 1 Answer
How to find all AudioClips in Resources folder and play them? 1 Answer
Referencing instances of scripts based on the GameObject they are attached to. 0 Answers
Class structure for diferent item types and items that do different things. 0 Answers