banner



How To Program Wasd With Reference To The Camera In Unity

In this paper, we share with yous the specific code of WASD Unity keyboard for moving objects, for your reference, the details are equally follows.

First, a Capsule is built in the scene and the principal camera is dragged nether its object.

ii. Hang the script under the Capsule object. WASD controls the direction of movement. The space moves upwardly the Y-axis and downward the F-Y-axis.

              using System.Collections; using System.Collections.Generic; using UnityEngine;   public class MoveCam : MonoBehaviour {  private Vector3 m_camRot;  Private Transform m_camTransform; //Camera Transform  Private Transform m_transform;//Camera parent object Transform  Public float m_movSpeed = x;//mobility coefficient  Public float m_rotateSpeed = ane;//rotation coefficient  private void Get-go()  {   m_camTransform = Photographic camera.principal.transform;   m_transform = GetComponent<Transform>();  }  private void Update()  {   Control();  }  void Control()  {   if (Input.GetMouseButton(0))   {    // Getting Mouse Move Distance    float rh = Input.GetAxis("Mouse X");    float rv = Input.GetAxis("Mouse Y");      // Rotating camera    m_camRot.x -= rv * m_rotateSpeed;    m_camRot.y += rh*m_rotateSpeed;     }     m_camTransform.eulerAngles = m_camRot;     // To marshal the orientation of the protagonist with that of the camera   Vector3 camrot = m_camTransform.eulerAngles;   camrot.x = 0; camrot.z = 0;   m_transform.eulerAngles = camrot;     // Define 3 values to control movement   float xm = 0, ym = 0, zm = 0;     // Keyboard W moves up   if (Input.GetKey(KeyCode.West))   {    zm += m_movSpeed * Time.deltaTime;   }   Other if (Input. GetKey (KeyCode. Due south)// Primal Disk S moves downward   {    zm -= m_movSpeed * Time.deltaTime;   }     If (Input. GetKey (KeyCode. A)//Keyboard A moves left   {    xm -= m_movSpeed * Time.deltaTime;   }   Other if (Input. GetKey (KeyCode. D)// Cardinal Disk D moves to the right   {    xm += m_movSpeed * Time.deltaTime;   }   if (Input.GetKey(KeyCode.Infinite) && m_transform.position.y <= iii)   {    ym+=m_movSpeed * Time.deltaTime;   }   if (Input.GetKey(KeyCode.F) && m_transform.position.y >= i)   {    ym -= m_movSpeed * Time.deltaTime;   }   m_transform.Translate(new Vector3(xm,ym,zm),Infinite.Cocky);  } }            

The above is the whole content of this article. I hope it will be helpful to anybody'south study, and I hope y'all will back up developpaer more than.

Source: https://developpaper.com/unity-keyboard-wasd-realizes-object-movement/

Posted by: fergusonwitiou.blogspot.com

0 Response to "How To Program Wasd With Reference To The Camera In Unity"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel