【问题标题】:Vector3 MoveTowards get the exact positionVector3 MoveTowards 获取准确位置
【发布时间】:2015-03-25 16:07:46
【问题描述】:

我正在开发一个 2D 游戏关卡,但使用 Unity3d 和 c# 编码在 3D 环境中制作它。现在我无法获得我的角色的确切位置,虽然它说它是(我想要的位置的向量 3 和我的角色位置匹配)但不在屏幕中,...

这是我的代码:

using UnityEngine;
using System.Collections;

public class WalkToPoint : MonoBehaviour
{
    public Vector3 tar;
    public float speed;
    private Vector3 mousePos;
    private Vector3 relativePos;
    Camera main;
    Animator anim;
    bool isWalking = false;
    Vector3 target;
    float x, y, z;
    [HideInInspector]
    public bool
        facingRight = true;
    void Start ()
    {
//      y = transform.position.y;
//      z = transform.position.z;
//      tar.z = z;
        main = Camera.main;
        anim = GetComponent<Animator> ();
        target = transform.position;
    }

    // Update is called once per frame
    void Update ()
    {
        if (Time.time > 2 && GetComponent<Rigidbody2D> ().velocity.magnitude == 0) {

            y = transform.position.y;
            z = transform.position.z;
            if (Input.GetMouseButton (0)) {
                mousePos = Input.mousePosition;
                mousePos.z = -53f;
                mousePos.y = 0f;
                target = main.ScreenToWorldPoint (mousePos);
                target.x *= -1;
                target.y = y;
                target.z = z;

                print (target);
                relativePos = target - transform.position;

                anim.SetFloat ("Speed", speed);
                isWalking = true;
            }

            if (isWalking) {
                /*x = transform.position.x;
            x = Vector3.Lerp (transform.position, tar, Time.deltaTime * speed).x;
            transform.position = new Vector3 (x, y, z);*/

                transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
                if (Mathf.Abs (target.x - transform.localPosition.x) <= 0.12f) {
                    isWalking = false;


                    anim.SetFloat ("Speed", 0f);
                }
            }
        }
    }
    void FixedUpdate ()
    {
        if (relativePos.x > 0 && facingRight)
            Flip ();
        else if (relativePos.x < 0 && !facingRight)
            Flip ();
    }

    void Flip ()
    {
        facingRight = !facingRight;
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;
    }
}

【问题讨论】:

  • 张贴图片!这可能与您的精灵枢轴点与另一个不在同一位置有关。

标签: c# unity3d


【解决方案1】:

我相信这个问题与你的动画速度有关。我遇到了类似的问题,我的游戏对象位于所需的位置,但模型只是通过简单地运行而从动画中偏移。除非我能看到你所看到的,否则我无法真正帮助你。我的猜测是将动画速度设置得非常高,以查看您的角色模型会发生什么。将角色更改为只是一个立方体,并查看它是否按预期执行。这将告诉您是动画问题还是您的代码问题。

但我确实有最后一个离题建议。

GetComponent().velocity.magnitude == 0

你在每帧都会调用的 if 语句中有这个。 GetComponent 很慢,我建议将此刚体保存到一个变量中,而不是每帧都获取它。

【讨论】:

  • 谢谢,我按照你说的做了,但我试图将我的相机投影从透视更改为正交并且它有效,问题是我不能将它作为投影我需要在我的透视投影中执行此操作...... :(
  • 谢谢,这只是画布位置增强的 raycas 问题。
猜你喜欢
  • 1970-01-01
  • 2019-07-21
  • 2017-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-05
  • 1970-01-01
相关资源
最近更新 更多