- Home /
Question by
Iso2D · Jan 27, 2018 at 03:13 AM ·
c#movementvisual studiopacman
Pacman Movement help
I'm making this pacman clone for a school project but I ran into a problem and pacman wont move. Is there something wrong with my script?
using UnityEngine;
using System.Collections;
public class PacmanMove : MonoBehaviour
{
public float speed = 0.4f;
Vector2 dest = Vector2.zero;
void Start()
{
dest = transform.position;
}
void FixedUpdate()
{
// Move closer to Destination
Vector2 p = Vector2.MoveTowards(transform.position, dest, speed);
GetComponent<Rigidbody2D>().MovePosition(p);
// Check for Input if not moving
if ((Vector2)transform.position == dest)
{
if (Input.GetKey(KeyCode.W) && Valid(Vector2.up)) dest = (Vector2)transform.position + Vector2.up;
if (Input.GetKey(KeyCode.D) && Valid(Vector2.right)) dest = (Vector2)transform.position + Vector2.right;
if (Input.GetKey(KeyCode.S) && Valid(-Vector2.up)) dest = (Vector2)transform.position - Vector2.up;
if (Input.GetKey(KeyCode.A) && Valid(-Vector2.right)) dest = (Vector2)transform.position - Vector2.right;
}
// Animation Parameters
Vector2 dir = dest - (Vector2)transform.position;
GetComponent<Animator>().SetFloat("DirX", dir.x);
GetComponent<Animator>().SetFloat("DirY", dir.y);
}
bool Valid(Vector2 dir)
{
// Cast Line from 'next to Pac-Man' to 'Pac-Man'
Vector2 pos = transform.position;
RaycastHit2D hit = Physics2D.Linecast(pos + dir, pos);
return (hit.collider == GetComponent<Collider2D>());
}
}
Comment
Answer by Honorsoft · Jan 27, 2018 at 07:52 AM
On the object that has this script (which should be the same object that has the Animator), make sure the "Apply Root Motion" box is check-marked.
See if that works, I am more familiar with 3D onjects in Unity.
test01.jpg
(20.6 kB)
Your answer
![](https://koobas.hobune.stream/wayback/20220612151357im_/https://answers.unity.com/themes/thub/images/avi.jpg)