0
votes

I am developing a 2D game level, but making it in a 3D environment using Unity3d and c# coding. Now i can not get the exact position for my character although it says that it is, (the vector 3 of my wanted position and my character position matches) but not in the screen,...

this is my code:

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;
    }
}
1
Post an image! It could be something to do with your sprite pivot point not being in the same position as the other.Savlon

1 Answers

1
votes

This problem has to do with your animation speed I believe. I have ran into similar issues where my GameObject was at the desired location, but the model was getting offset from the animation by just simply running. I can't really help you unless I can see what you're seeing. My guess would be to set the anim speed very high to see what happens to your character model. Change the character to just a cube, and see if it performs as desired. This will tell you if its an animation problem or your code.

But i do have one last off topic suggestion.

GetComponent ().velocity.magnitude == 0

You have this in an if statement that will be called every frame. GetComponent is slow, i recommend saving this rigidbody to a variable instead of getting it every frame.