【发布时间】:2019-05-01 15:00:52
【问题描述】:
我有一个使用免费角色控制器 2D 资产脚本和我自己制作的有效玩家移动脚本,它使用键盘的 A 和 D 键来左右移动。
我希望此代码适用于触摸屏手机。基本上,你按屏幕左侧向左移动,向右移动。
我还是 C# 新手,可以使用帮助。
这是我当前的玩家移动脚本。
提前致谢!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[Range(0, .3f)] [SerializeField] private float m_MovementSmoothing = .05f;
private Rigidbody2D m_Rigidbody2D;
private Vector3 m_Velocity = Vector3.zero;
public float runSpeed = 40f;
float horizontalMove = 0f;
private void Awake()
{
m_Rigidbody2D = GetComponent<Rigidbody2D>();
}
public void Move(float move)
{
// Move the character by finding the target velocity
Vector3 targetVelocity = new Vector2(move * 10f,
m_Rigidbody2D.velocity.y);
// And then smoothing it out and applying it to the character
m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity,
targetVelocity, ref m_Velocity, m_MovementSmoothing);
}
// Update is called once per frame
void Update()
{
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
}
void FixedUpdate()
{
// Move our character
Move(horizontalMove * Time.fixedDeltaTime);
}
}
【问题讨论】: