unity_控制物体移动代码

目录

2D游戏控制

简单的上下左右移动

第一种 使用Rigidbody2D 

第二种 上下左右移动加上旋转

2D空战飞机的移动

汽车、坦克等移动

坦克的控制


2D游戏控制

简单的上下左右移动

第一种 使用Rigidbody2D 

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

public class PlayerMove : MonoBehaviour
{
    private Rigidbody2D rb;
    private float moveH, moveV;

    [SerializeField] private float moveSpeed = 5.0f;

    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        moveH = Input.GetAxis("Horizontal") * moveSpeed;
        moveV = Input.GetAxis("Vertical") * moveSpeed;
    }

    private void FixedUpdate()
    {
        rb.velocity = new Vector2(moveH, moveV);
    }

}

第二种 上下左右移动加上旋转

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

public class PlayerMove : MonoBehaviour
{
    public int step;    //步长
    public float velocity = 0.35f;  //速度

    private int x;
    private int y;
    private Vector3 playerPos;

    void Start()
    {
        InvokeRepeating("Move", 0, velocity);
        x = 0; y = step;
    }

    void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
            gameObject.transform.localRotation = Quaternion.Euler(0, 0, 0);
            x = 0;
            y = step;
        }
        if (Input.GetKey(KeyCode.S))
        {
            gameObject.transform.localRotation = Quaternion.Euler(0, 0, 180);
            x = 0;
            y = -step;
        }
        if (Input.GetKey(KeyCode.A))
        {
            gameObject.transform.localRotation = Quaternion.Euler(0, 0, 90);
            x = -step;
            y = 0;
        }
        if (Input.GetKey(KeyCode.W))
        {
            gameObject.transform.localRotation = Quaternion.Euler(0, 0, 0);
            x = 0;
            y = step;
        }
        if (Input.GetKey(KeyCode.D))
        {
            gameObject.transform.localRotation = Quaternion.Euler(0, 0, -90);
            x = step;
            y = 0;
        }
    }

    void Move()
    {
        playerPos = gameObject.transform.localPosition;
        gameObject.transform.localPosition = new Vector3(playerPos.x + x, playerPos.y + y, playerPos.z);
    }

}

2D空战飞机的移动

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

public class PlayerController : MonoBehaviour
{
    //RectTransform引用,
    RectTransform rectTransform;
    // Use this for initialization
    void Start()
    {
        //当对象被放入内存调用
        rectTransform = GetComponent<RectTransform>();
    }

    // Update is called once per frame
    void Update()
    {
        Move();
    }
    void Move()
    {
        float x_speed = 0;   //横轴速度
        float y_speed = 0;   //纵轴
        if (Input.GetKey(KeyCode.A) == true
            && Input.GetKey(KeyCode.D) == false)
        {
            x_speed = -30;
        }
        else if (Input.GetKey(KeyCode.D) == true
            && Input.GetKey(KeyCode.A) == false)
        {
            x_speed = +30;
        }
        if (Input.GetKey(KeyCode.W) == true
            && Input.GetKey(KeyCode.S) == false)
        {
            y_speed = +30;
        }
        else if (Input.GetKey(KeyCode.S) == true
            && Input.GetKey(KeyCode.W) == false)
        {
            y_speed = -30;
        }

        //上下左右斜向的速度一样
        if (Mathf.Abs(x_speed) > 1 && Mathf.Abs(y_speed) > 1)
        {
            x_speed *= Mathf.Sqrt(2) / 2;
            y_speed *= Mathf.Sqrt(2) / 2;
        }
        //取出飞机的当前位置
        Vector2 position = rectTransform.anchoredPosition;
        //修改x坐标
        position.x += x_speed * Time.deltaTime;
        position.y += y_speed * Time.deltaTime;
        //限制x,y的范围,避免超出屏幕
        position.x = Mathf.Clamp(position.x, -2.3f, 2.3f);
        position.y = Mathf.Clamp(position.y, -4.5f, 4.2f);
        //位置修改后再放回去
        rectTransform.anchoredPosition = position;

    }
}

 

汽车、坦克等移动

坦克的控制

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

public class TankMove : MonoBehaviour
{
    public float tankSpeed = 10f;
    public float turnSpeed = 150;

    private Rigidbody tanklRigidbody;
    private float tankMoveInputValue;
    private float tankTurnInputValue;

    public void Awake()
    {
        tanklRigidbody = GetComponent<Rigidbody>();
    }

    private void OnEnable()
    {
        tanklRigidbody.isKinematic = false;
        tankMoveInputValue = 0f;
        tankTurnInputValue = 0f;
    }

    private void OnDisable()
    {
        tanklRigidbody.isKinematic = true;
    }

    private void Update()
    {
        tankMoveInputValue = Input.GetAxis("Vertical");
        tankTurnInputValue = Input.GetAxis("Horizontal");
    }

    private void FixedUpdate()
    {
        Move();
        Turn();
    }

    private void Move()
    {
        Vector3 move = transform.forward * tankMoveInputValue * tankSpeed * Time.deltaTime;
        tanklRigidbody.MovePosition(tanklRigidbody.position + move);
    }

    private void Turn()
    {
        float turn = tankTurnInputValue * turnSpeed * Time.deltaTime;
        Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);
        tanklRigidbody.MoveRotation(tanklRigidbody.rotation * turnRotation);
    }

}

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值