• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by eluong · Jan 19, 2014 at 09:05 PM · 4.3

How to move 2D Gameobjects using GUITextures?

Hi guys,

I'm trying to implement simple buttons to move my 2D character around the screen, and make him able to use any of the available functions in my PlayerControl script. I have my keyboard controls all set and done, but I don't know how to implement some touch features. I have some GUITexture GameObjects set up, however, I don't know how to hook up the code so that the touch buttons may communicate with my PlayerControl script.

How may I achieve this?

This is what my PlayerControl script looks like:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerControl : MonoBehaviour
 {
         public float moveSpeed = 4f;
         public float jumpSpeed = 200f;
         public bool grounded, interact = false;
         public Transform lineStart, lineEnd, groundedEnd, destination;
         float jumpTime, jumpDelay = .5f;
         bool jumped;
         Animator anim;
         RaycastHit2D interaction;
 
         void Start ()
         {
                 anim = GetComponent<Animator> ();
         }
 
         // Update is called once per frame
         void Update ()
         {
                 Movement ();
                 Raycasting ();
         }
 
         // Movement script
         void Movement ()
         {
 
                 anim.SetFloat ("speed", Mathf.Abs (Input.GetAxis ("Horizontal")));
                 anim.SetFloat ("falling", Mathf.Abs (rigidbody2D.velocity.y));
 
                 //move right
                 if (Input.GetAxisRaw ("Horizontal") > 0) {
                         transform.Translate (Vector2.right * moveSpeed * Time.deltaTime);
                         transform.eulerAngles = new Vector2 (0, 0); //rotate character
                 }
 
                 //move left
                 if (Input.GetAxisRaw ("Horizontal") < 0) {
                         transform.Translate (Vector2.right * moveSpeed * Time.deltaTime);
                         transform.eulerAngles = new Vector2 (0, 180); //rotate character
                 }
                 
                 //jump if grounded
                 if (Input.GetKeyDown (KeyCode.Space) && grounded == true) {
                         rigidbody2D.AddForce (Vector2.up * jumpSpeed);
                         jumpTime = jumpDelay;
                         anim.SetTrigger ("jump");
                         jumped = true;
                 }
 
                 jumpTime -= Time.deltaTime;
                 if (jumpTime <= 0 && grounded && jumped) {
                         anim.SetTrigger ("landing");
                         jumped = false;
                         anim.SetTrigger ("ground");
                 }
 
                 if (Input.GetKeyDown (KeyCode.E) && interact == true) {
                         transform.position = destination.position;
                 }
         }
 
         //raycasting for interacts and jumping
         void Raycasting ()
         {
                 Debug.DrawLine (lineStart.position, lineEnd.position, Color.green);
                 Debug.DrawLine (this.transform.position, groundedEnd.position, Color.green);
 
                 grounded = Physics2D.Linecast (this.transform.position, groundedEnd.position, 1 << LayerMask.NameToLayer ("Ground"));
 
                 if (Physics2D.Linecast (lineStart.position, lineEnd.position, 1 << LayerMask.NameToLayer ("Princess"))) {
                         interaction = Physics2D.Linecast (lineStart.position, lineEnd.position, 1 << LayerMask.NameToLayer ("Princess"));
                         interact = true;
                 } else {
                         interact = false; 
                 }
         }
 }
 
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

Answer by Benoit Dufresne · Jan 20, 2014 at 12:52 AM

At first glance your script is fine. Don't use GUITexture for things that move tho. They're made for static things like... GUIs. I don't think their position changes anything.

Use planes, and put your textures on those instead.

Comment
Add comment · Show 2 · 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 Eric5h5 · Jan 20, 2014 at 01:23 AM 0
Share

There's no issue with moving GUITextures. GUIs aren't necessarily static, anyway. Don't use planes, though; there's no reason when 1) the quad primitive exists and 2) sprites exist. Generally you would prefer to use sprites.

avatar image Benoit Dufresne · Jan 20, 2014 at 01:27 AM 0
Share

Hmmm I didn't realize Sprites were a thing! A quad is what I meant tho! Small planes > Big planes, oddly enough

avatar image
0

Answer by eluong · Jan 20, 2014 at 01:23 AM

Sorry guys, I figured it out.

I can show the fixes I did to make it work if anyone wants.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

using top down character controller on 4.3 2D view? 0 Answers

Xbox pad triggers not individually accessible? 2 Answers

GuiTexture + spritesheet unity 4.3 1 Answer

Git issues with 4.3 2 Answers

Loop Time through Script 2D 4.3 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