Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by vovo801 · Feb 07, 2015 at 09:57 AM · 2dcontrollerplatformerclickquest

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...

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

GetButtonDown not always firing 1 Answer

sprite character rotation 0 Answers

How to switch animation controllers dynamically? 1 Answer

Are Character Controllers inefficient? 1 Answer

Moving using controller stick question 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges