Tuesday, December 7, 2021

Move your player using keyboard key(A,D,S,W)

Move your player using keyboard key

First create a player to which you want to move. See Below here i have taken a cube to which i will move using keyboard keys.(A,D,S,W).


 
Now Add cube in your game scene.
Select your player(here cube) and give it some height.
Here I have given Y=3 (set position Y=3 in Transform).


Now Time to add a c# script to your player.

for this Go to Assets right click select create and again select c#script. Give it some name whatever you want , here i have given playermove.

Left:    A
Right:    D
Up:    W
Down:    S
UpArrow :    forward
DownArrow :    Back

Playermove.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Playerove : MonoBehaviour
{
    public float speed = 5.0f;
    void Update()
    {
        if(Input.GetKey(KeyCode.A))
        {
            transform.position += Vector3.left * speed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.D))
        {
            transform.position += Vector3.right * speed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.W))
        {
            transform.position += Vector3.up * speed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.S))
        {
            transform.position += Vector3.down * speed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.UpArrow))
        {
            transform.position += Vector3.forward * speed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.DownArrow))
        {
            transform.position += Vector3.back * speed * Time.deltaTime;
        }
    }
}

Attach this Playermove.cs script to your player. 
and its all done! 

See the video how it will look in game mode:

Other Script Which you can use to move player:
Left:    A
Right:    D
Up:    W
Down:    S
UpArrow :    forward
DownArrow :    Back

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Playerove : MonoBehaviour
{
    public float speed = 5.0f;
    void Update()
    {
            if (Input.GetKey(KeyCode.A))
            {
                transform.Translate(-0.1f, 0f, 0f);
            }
            if (Input.GetKey(KeyCode.D))
            {
                transform.Translate(0.1f, 0f, 0f);
            }
            if (Input.GetKey(KeyCode.S))
            {
                transform.Translate(0.0f, -0.1f, 0.0f);
            }
            if (Input.GetKey(KeyCode.W))
            {
                transform.Translate(0.0f, 0.1f, 0.0f);
            }
            if (Input.GetKey(KeyCode.UpArrow))
            {
                transform.Translate(0.0f, 0.0f,0.1f);
            }
            if (Input.GetKey(KeyCode.DownArrow))
            {
                transform.Translate(0.0f, 0.0f, -0.1f);
            }
    }
    }

We have one more way to move our player in Horizontal and Vertical direction : By using GetAxis!
See the Script below and attach it to your player.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Playerove : MonoBehaviour
{
    public float speed = 5.0f;
    Vector3 Vec;
    void Update()
    {
        Vec = transform.localPosition;
        Vec.x += Input.GetAxis("Horizontal") * Time.deltaTime * speed;
        Vec.z += Input.GetAxis("Vertical") * Time.deltaTime * speed;
        transform.localPosition = Vec;
    }
}




 






0 Comments:

Post a Comment

Thank you for your comment !