Пожалуйста помогите написать код ** движение wasd (В Unity) 35 баллов

+727 голосов
2.2m просмотров

Пожалуйста помогите написать код на движение wasd (В Unity) 35 баллов


Информатика | 2.2m просмотров
Дан 1 ответ
+94 голосов

Ответ:

using UnityEngine;

using System.Collections;

[RequireComponent(typeof(Rigidbody2D))]

public class Player2DControl : MonoBehaviour {

public enum ProjectAxis {onlyX = 0, xAndY = 1};

public ProjectAxis projectAxis = ProjectAxis.onlyX;

public float speed = 150;

public float addForce = 7;

public bool lookAtCursor;

public KeyCode leftButton = KeyCode.A;

public KeyCode rightButton = KeyCode.D;

public KeyCode upButton = KeyCode.W;

public KeyCode downButton = KeyCode.S;

public KeyCode addForceButton = KeyCode.Space;

public bool isFacingRight = true;

private Vector3 direction;

private float vertical;

private float horizontal;

private Rigidbody2D body;

private float rotationY;

private bool jump;

void Start ()  

{

 body = GetComponent();

 body.fixedAngle = true;

 if(projectAxis == ProjectAxis.xAndY)  

 {

  body.gravityScale = 0;

  body.drag = 10;

 }

}

void OnCollisionStay2D(Collision2D coll)  

{

 if(coll.transform.tag == "Ground")

 {

  body.drag = 10;

  jump = true;

 }

}

 

void OnCollisionExit2D(Collision2D coll)  

{

 if(coll.transform.tag == "Ground")

 {

  body.drag = 0;

  jump = false;

 }

}

 

void FixedUpdate()

{

 body.AddForce(direction * body.mass * speed);

 if(Mathf.Abs(body.velocity.x) > speed/100f)

 {

  body.velocity = new Vector2(Mathf.Sign(body.velocity.x) * speed/100f, body.velocity.y);

 }

 if(projectAxis == ProjectAxis.xAndY)

 {

  if(Mathf.Abs(body.velocity.y) > speed/100f)

  {

   body.velocity = new Vector2(body.velocity.x, Mathf.Sign(body.velocity.y) * speed/100f);

  }

 }

 else

 {

  if(Input.GetKey(addForceButton) && jump)

  {

   body.velocity = new Vector2(0, addForce);

  }

 }

}

void Flip()

{

 if(projectAxis == ProjectAxis.onlyX)

 {

  isFacingRight = !isFacingRight;

  Vector3 theScale = transform.localScale;

  theScale.x *= -1;

  transform.localScale = theScale;

 }

}

 

void Update ()  

{

 if(lookAtCursor)

 {

  Vector3 lookPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));

  lookPos = lookPos - transform.position;

  float angle  = Mathf.Atan2(lookPos.y, lookPos.x) * Mathf.Rad2Deg;

  transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

 }

 if(Input.GetKey(upButton)) vertical = 1;

 else if(Input.GetKey(downButton)) vertical = -1; else vertical = 0;

 if(Input.GetKey(leftButton)) horizontal = -1;

 else if(Input.GetKey(rightButton)) horizontal = 1; else horizontal = 0;

 if(projectAxis == ProjectAxis.onlyX)  

 {

  direction = new Vector2(horizontal, 0);  

 }

 else  

 {

  if(Input.GetKeyDown(addForceButton)) speed += addForce; else if(Input.GetKeyUp(addForceButton)) speed -= addForce;

  direction = new Vector2(horizontal, vertical);

 }

 if(horizontal > 0 && !isFacingRight) Flip(); else if(horizontal < 0 && isFacingRight) Flip();

}

ИЛИ

using UnityEngine;

using System.Collections;

public class Move : MonoBehaviour {

public GameObject player;

public int speedRotation = 3;

public int speed = 5;

public AnimationClip anima;

public int jumpSpeed = 50;

void Start () {  

 player = (GameObject)this.gameObject;  

 animation.AddClip(anima, "animCube");

 }

 void Update(){

if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))  

 {  

 player.transform.position += player.transform.forward * speed * Time.deltaTime;  

 animation.CrossFade("animCube");

 }  

if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))  

 {  

 player.transform.position -= player.transform.forward * speed * Time.deltaTime;  

 }  

if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))  

 {  

 player.transform.Rotate(Vector3.down * speedRotation);  

 }  

 if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))  

 {  

 player.transform.Rotate(Vector3.up * speedRotation);  

 }  

if (Input.GetKeyDown(KeyCode.Space))  

 {  

 player.transform.position += player.transform.up * jumpSpeed * Time.deltaTime;  

 }  

}

}

Объяснение:

+131

Rigibody нужен на игроке?

+145

(Using unity 2018.3.23.f

+185

All complier errors have to be fixed to enter play mode!