- Home /
2d character controller for platformer with touch or mouse input?
Hello!
I am new to Unity. I am just learning but I have an interesting idea of a game in my mind that requires this custom made character controller.
It is a 2d game. The game will basically look like a platformer. Character walking on platforms. In this game the character can not jump, nor can he fall of the egdes of the platforms, so it makes it a bit simpler. The platforms might be connected with each other with ladders or similar means of getiing from one platform to another. The main issue is with the input coming from touch interface, or mouse clicks on screen.
What is required is that after user clicks on the screen - character is moving towards the point in the scene where the click occured. He does it by walking on platforms and climbing ladders to get himself closest to the point of user click possible.
Has anyone done a similar controller before? Or maybe can help me out with some references to look at? I have been looking at Samples Beta in Asset store, and many other samples, but they are all mostly made for classic platformers, where the player can jump, crawl, collide with other elements in the scene, etc... Which is not the case here...
Answer by vovo801 · Feb 13, 2015 at 10:00 AM
I guess I will answer this one myself. The most helpful information I have found on character controllers was Unity Live training video about Robot character animation. But it was just classic platformer type of controls. Also i found this helpful thread: Point to click movement in a 2d game
// Define a variable vector for the position you will want the object to move to. Vector3 wantedPos;
// Get the position of the mouse click. Vector3 mousePos = Input.mousePosition;
// Transmogrify the mouse coordinates to the coordinate system of your game space. wantedPos = Camera.main.ScreenToWorldPoint (new Vector3 (mousePos.x, mousePos.y, 1f));
// Determine the vector from your desired (ending) position and the game objects current position. Vector3 relativePos = wantedPos - transform.position ;
// Move your game object using a rigid body force to get it moving in the right direction. transform.rigidbody2D.AddForce(relativePos.normalized * 40f);
Tried to merge these two approaches for my case and wrote this piece of code(it is a C# script to attach to the character):
using UnityEngine;
using System.Collections;
public class Indiana2 : MonoBehaviour {
public Vector3 target; // The point user picks for the character to follow
public bool facingRight = false; // Check where the character is facing
private int move=0; // Movement direction
private float maxSpeed = 5f; // Speed of character movement
private Animator anim; // Reference to the animator
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator> ();
}
void FixedUpdate()
{
// Obtaining target point user clicked on screen
if (Input.GetMouseButton (0))
{
// After user has clicked left mouse button recording mouse position
Vector3 mousePos = Input.mousePosition;
// Converting mouse position into coordinate system connected to the camera.
target = Camera.main.ScreenToWorldPoint (new Vector3 (mousePos.x, mousePos.y, 1f));
// Set movement direction depending on target point
if (target.x < transform.position.x)
move = -1;
else if (target.x> transform.position.x)
move = 1;
Debug.Log (move);
}
// Flip the character if nessessary
if (move ==1 && ! facingRight)
Flip ();
if (move == -1 && facingRight)
Flip();
// check if arrived to target point
if (Mathf.Abs (target.x - transform.position.x) < 0.2f)
{
move = 0;
}
anim.SetInteger ("Movement", Mathf.Abs(move));
// apply velocity to actually make character move
rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
}
void Flip()
{
Debug.Log ("In Flip");
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Answer by chintan_shroff · Feb 13, 2015 at 05:05 AM
You should post some of your code here if you want people to help you better. See if this helps. http://www.thegamecontriver.com/2014/10/move-to-mouse-click-touch-position-unity.html
Your answer
Follow this Question
Related Questions
GetButtonDown not always firing 1 Answer
sprite character rotation 0 Answers
How to switch animation controllers dynamically? 1 Answer