Denkaev
Denkaev

Reputation: 21

Teleport an object to specified coordinates

After death, pacman returns to the point of impact with the ghost and starts to rotate. How can I fix this?

     private void FixedUpdate()
     {
 
         Vector2 p = Vector2.MoveTowards(transform.position, dest, Speed);
         GetComponent<Rigidbody2D>().MovePosition(p);
         if ((Vector2)transform.position == dest)
         {
             if (Input.GetKey(KeyCode.UpArrow) && valid(Vector2.up))
                 dest = (Vector2)transform.position + Vector2.up;
             if (Input.GetKey(KeyCode.RightArrow) && valid(Vector2.right))
                 dest = (Vector2)transform.position + Vector2.right;
             if (Input.GetKey(KeyCode.DownArrow) && valid(-Vector2.up))
                 dest = (Vector2)transform.position - Vector2.up;
             if (Input.GetKey(KeyCode.LeftArrow) && valid(-Vector2.right))
                 dest = (Vector2)transform.position - Vector2.right;
         }
         Vector2 dir = dest - (Vector2)transform.position;
         GetComponent<Animator>().SetFloat("DirX", dir.x);
         GetComponent<Animator>().SetFloat("DirY", dir.y);
     }
     bool valid(Vector2 dir)
     {
         Vector2 pos = transform.position;
         RaycastHit2D hit = Physics2D.Linecast(pos + dir, pos);
         return (hit.collider == GetComponent<Collider2D>());
     }
     private void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.tag == "Ghost")
             transform.position = new Vector2(14f,11f);
     }

Need return to start position, after dead. enter image description here

Upvotes: 1

Views: 174

Answers (2)

Denkaev
Denkaev

Reputation: 21

The problem is in the first line :

   Vector2 p = Vector2.MoveTowards(transform.position, dest, Speed);

Decision:

transform.position = PacManStartPoint;
dest = transform.position;

How to change the sprite now ?

Upvotes: 1

derHugo
derHugo

Reputation: 90682

Whenever physics are involved (your re using a Rigidbody2D) you do not want to set anything via

transform.position = new Vector2(14f,11f);

just as you did within FixedUpdate rather always only set the value via the Rigidbody2D component:

GetComponent<Rigidbody2D>().position = new Vector2(14f,11f);

Then you also have an Animator. Since your movement seems to work in general I guess it's not the issue but still make sure that the animator doesn't hold any keyframe on the position in any animation.


In general you should store the reference once

// Already drag this in via the Inspector
[SerializeField] private Rigidbody2D _rigidbody;
[SerializeField] private Animator _animator;

// Also make it possible to adjust the start position via the Inspector
// will make your live easier when changing it ;)
[SerializeField] private Vector2 startPoint = new Vector2(14f,11f);

private void Awake ()
{
    // or get it once as fallback
    if(!_rigidbody) _rigidbody = GetComponent<Rigidbody2D>();
    if(!_animator) _animator = GetComponent<Animator>();

    _rigidbody.position = startPoint;
}

and than later re-use them e.g. in

_rigidbody.MovePosition(p);

and

_rigidbody.position = startPoint;

and the same for the _animator

Upvotes: 1

Related Questions