Я развиваю уровень 2D игры, но делаю его в 3D-среде с использованием кодировок Unity3d и C#. Теперь я не могу получить точную позицию для своего персонажа, хотя он говорит, что это (вектор 3 моей желаемой позиции и позиция моего персонажа совпадают), но не на экране, ...Vector3 MoveTowards получить точную позицию
это мой код:
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;
}
}
Опубликовать изображение! Это может быть связано с тем, что ваш опорный пункт спрайта не находится в том же положении, что и другой. – Savlon