- Home /
Top-Down Movement in Unity
I am attempting to make a top-down 2D character with a circle collider on the XZ plane.
The problems/features I need to deal with are:
I want the character to always face the mouse, to rotate to look at the mouse.
I want the character to move dependent of where he looks, so pressing W, for instance, always moves you the direction that you are facing.
I want to eliminate the "ice-skating" effect. On joystick, my character moves flawlessly, but with keyboard, the character slides around like an ice-skater. It's wierd.
On the plus side, collision works...beyond that, I'm lost.
This is what I have so far: using UnityEngine; using System.Collections;
public class DungeonPlayerBehaviorScript : MonoBehaviour {
private const float VELOCITY = .075f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
CharacterController controller = GetComponent<CharacterController>();
controller.Move(new Vector3(Input.GetAxis ("Horizontal") * VELOCITY, 0, Input.GetAxis ("Vertical") * VELOCITY));
}
}
Answer by chummscrubber · Jul 29, 2013 at 04:09 AM
1) Set your characters forward vector to rotate towards the x,y co-ords that the mouse pointer is located using Input.mousePosition (so you would do a vector rotation to face the mouse pointer on the x,y axis only).
2) Use Vector3.Forward Time.DeltaTime movespeed to move forward from where your character is currently facing.
Your answer
Follow this Question
Related Questions
How do I make a 2D object face the direction it is moving in top down space shooter 1 Answer
[2D] CharacterController rotation 0 Answers
Top down 2d movement without being affected by rotation 2 Answers
how to make an object move towards it's rotation a set amount as a child in 2d 0 Answers
Character Controller slides sideways when it hits objects are angles different from 90 degrees 1 Answer