代码的虚拟轴

在这里插入图片描述

虚拟轴只是俩张图片,下边的代码是挂在中间的小图片上的,不用插件:

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

public class VirtualDemo : MonoBehaviour,IDragHandler,IEndDragHandler  {
    Vector3 v3;
    RectTransform rect;
    Vector3 world;
    Vector2 screen;
    Vector2 middle;
    float radius;
    public Vector3 movev3;
    public void OnDrag(PointerEventData eventData)
    {
        middle = eventData.position - screen;
        movev3 = middle.normalized.y * Vector3.up;
        movev3 += middle.normalized.x * Vector3.right;
        if(middle .magnitude >radius )
        {
            middle = middle.normalized * radius;
        }
        rect.anchoredPosition = middle;
    }
    public void OnEndDrag(PointerEventData eventData)
    {
        movev3 = Vector3.zero;
        transform.position = v3;
    }
	void Start () {
        rect = GetComponent<RectTransform>();
        v3 = transform.position;
        radius =    transform.parent.GetComponent<RectTransform>().rect.width / 2;
        world = rect.position;
        screen = RectTransformUtility.WorldToScreenPoint(null, world);
	}
    public Vector3 gethandl()
    {
        return movev3;
    }
}

现在是2D的把y轴v3乘以的换成forward,就是3D的,下边的挂在要移动的物体上的代码:

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

public class VirtualMove : MonoBehaviour {
    public GameObject obj;
    VirtualDemo virtualDemo;

    void Start () {
        virtualDemo = obj.GetComponent<VirtualDemo>();
	}
	void Update () {
        transform.Translate(virtualDemo.movev3 * Time.deltaTime*100);
	}
}

最后的100是物体移动速度,可以自己调,以上的就是全部代码,要用的自己往项目里添加吧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要控制虚拟值的增长速度,可以通过以下几种方法实现: 1. 在Unity的Input Manager中创建一个新的虚拟,并将其绑定到水平移动的按键或手柄摇杆。然后,在角色控制器的脚本中使用`Input.GetAxisRaw()`方法获取虚拟的原始值,而不是`Input.GetAxis()`方法获取虚拟的平滑值。这样可以避免虚拟值平滑增长的效果,而是直接根据按键或手柄摇杆的输入值来改变虚拟的值。 ```csharp public float moveSpeed = 5f; // 移动速度 public float moveAcceleration = 10f; // 移动加速度 public float moveDeceleration = 10f; // 移动减速度 private float moveInput; private Rigidbody2D rb; void Start() { rb = GetComponent<Rigidbody2D>(); } void FixedUpdate() { // 获取虚拟的原始值 moveInput = Input.GetAxisRaw("Horizontal"); // 根据虚拟值进行移动 if (moveInput != 0) { // 根据虚拟值和移动加速度计算速度增量 float velocityIncrement = moveInput * moveAcceleration * Time.fixedDeltaTime; // 将速度增量限制在移动速度范围内 float newVelocityX = Mathf.Clamp(rb.velocity.x + velocityIncrement, -moveSpeed, moveSpeed); // 将刚体的速度设置为新的速度 rb.velocity = new Vector2(newVelocityX, rb.velocity.y); } else { // 根据移动减速度逐渐减慢速度 float velocityDecrement = moveDeceleration * Time.fixedDeltaTime; float newVelocityX = Mathf.MoveTowards(rb.velocity.x, 0f, velocityDecrement); rb.velocity = new Vector2(newVelocityX, rb.velocity.y); } } ``` 以上代码会根据虚拟的原始值来计算速度增量,然后将速度增量限制在移动速度范围内,并将刚体的速度设置为新的速度。如果虚拟的原始值为零,则根据移动减速度逐渐减慢速度,从而实现缓慢停止的效果。 2. 在角色控制器的脚本中添加一个移动速度的插值变量,用于控制虚拟值的增长速度。当按下移动键时,将插值变量从零逐渐增加到移动速度,从而实现缓慢加速的效果。当松开移动键时,将插值变量从当前速度逐渐减少到零,从而实现缓慢停止的效果。 ```csharp public float moveSpeed = 5f; // 移动速度 public float moveAcceleration = 10f; // 移动加速度 public float moveDeceleration = 10f; // 移动减速度 private float moveInput; private float moveSpeedLerp = 0f; private Rigidbody2D rb; void Start() { rb = GetComponent<Rigidbody2D>(); } void FixedUpdate() { // 获取虚拟的原始值 moveInput = Input.GetAxisRaw("Horizontal"); // 根据虚拟值进行移动 if (moveInput != 0) { // 根据插值变量和移动速度计算速度增量 float velocityIncrement = moveSpeedLerp * moveSpeed * Time.fixedDeltaTime; // 将速度增量限制在移动速度范围内 float newVelocityX = Mathf.Clamp(rb.velocity.x + velocityIncrement, -moveSpeed, moveSpeed); // 将刚体的速度设置为新的速度 rb.velocity = new Vector2(newVelocityX, rb.velocity.y); // 逐渐增加插值变量 moveSpeedLerp = Mathf.MoveTowards(moveSpeedLerp, 1f, moveAcceleration * Time.fixedDeltaTime); } else { // 根据插值变量和移动速度计算速度减量 float velocityDecrement = moveSpeedLerp * moveSpeed * moveDeceleration * Time.fixedDeltaTime; // 将速度减量限制在移动速度范围内 float newVelocityX = Mathf.Clamp(rb.velocity.x - velocityDecrement, -moveSpeed, moveSpeed); // 将刚体的速度设置为新的速度 rb.velocity = new Vector2(newVelocityX, rb.velocity.y); // 逐渐减少插值变量 moveSpeedLerp = Mathf.MoveTowards(moveSpeedLerp, 0f, moveDeceleration * Time.fixedDeltaTime); } } ``` 以上代码会根据插值变量和移动速度计算速度增量或减量,并将速度增量或减量限制在移动速度范围内,并将刚体的速度设置为新的速度。如果虚拟的原始值为零,则根据移动减速度逐渐减少插值变量,从而实现缓慢停止的效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值