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 nasby321 · Dec 02, 2014 at 08:37 PM · 2dtouchscreentabletfantasy

I need help setting up touch controls.

I need help making touch controls for my 2D sidescroller.

I have tried several youtube and text tutorials, but they don't show you everything you need. They are also in javascript. I use C#.

alt textalt text

Attached I have arrow textures that I want for movement, but I don't know how to get them to appear on screen and to follow the character/scene when moving. The attacking can be activated by clicking anywhere on the tablet or phone.

 using UnityEngine;
 using System.Collections;
 using SpriteFactory;
  
 public class player : MonoBehaviour {
     
     public int maxXPosition = 50;
     private Vector3 initialPos;
     private Quaternion initialRotation;
     
   
 
      public int points = 0;
     public float speed = 9.0f;
     public float jumpSpeed = 300.0f;
     public float gravity = 10.0f;
     public bool grounded = true;
     float height;
     int layerMask;
     public int numberOfPointsToWin = 30;
     public int die = -1;
     
     public bool charPressed = false;
     
  
  
     private Transform thisTransform;
     private Sprite sprite;
     private bool flipped;
     
     
     IEnumerator Death() {
    yield return new WaitForSeconds(0.1f);
         PointCounter.lives -= 1;
     transform.rotation = initialRotation;
     transform.position = initialPos;
         
         
         
     }
     
     
  
     // Use this for initialization
     void Awake () {
        thisTransform = transform;
        sprite = (Sprite)GetComponent(typeof(Sprite));
         height = collider.bounds.extents.y;
    layerMask = LayerMask.NameToLayer("Player");
  
     }
     
      void Start () {
         initialPos = transform.position;
         initialRotation = transform.rotation;
         
         
     }
  
     // Update is called once per frame
     void Update () {
         
     
         
        Vector3 moveVector = new Vector3(0,0,0);
        bool moving = false;
         
         moveVector.y -= gravity * Time.deltaTime;
         
     
         
          if(!Physics.Raycast(thisTransform.position, Vector3.down, height,layerMask)){
       sprite.Play("jump");
    }
         
         if(Input.GetKeyDown(KeyCode.Space)){
              Jump();
 }
     
 if(Input.GetKey(KeyCode.A)){
          moveVector.x -= 1.0f;
          moving = true;
 }
  if(Input.GetKey(KeyCode.D)){
          moveVector.x += 1.0f;
          moving = true;
  }
  if(Input.GetKey(KeyCode.F)){
          Attack();
        } else {
          if(!IsAttacking()) {
           if(moving) {
               Move(moveVector);
           } else {
               Stand();
          }
        }
        
 
     }
         
         
         
        }
     
 
 
  private void Stand() {
        sprite.Stop();
     }
     
 
     
   private void Move(Vector3 moveVector) {
        FlipSprite(moveVector.x);
        moveVector *= speed * Time.deltaTime;
        thisTransform.position = thisTransform.position + moveVector;
        sprite.Play("walk");
  
  
  
  
     }
  
     private void FlipSprite(float x) {
        if(x == 0.0f) return;
  
        float xDir = Mathf.Sign(x);
        if(!flipped && xDir < 0.0f) {
          flipped = true;
          sprite.SetFlippedState(true, false);
        } else if(flipped&& xDir > 0.0f) {
          flipped = false;
              sprite.SetFlippedState(false, false);
        }
     }
  
     private void Attack() {
        if(IsAttacking()) return;
  
        sprite.Play("Sword");
     }
  
     private bool IsAttacking() {
        if(sprite.IsAnimationPlaying("Sword")){ return false;}
        return false;
 }
     
         void Jump(){
         if(grounded == true)
         {
         rigidbody.AddForce(Vector3.up * jumpSpeed);
             grounded = false;
     }
         
     }
     
     void Gravity(){
         if(grounded == true)
         {
         rigidbody.AddForce(Vector3.up * jumpSpeed);
             grounded = false;
     }
         
     }
     
     void OnCollisionStay(Collision col){
         if(col.gameObject.tag == "floor"){
             grounded = true;
         
     }
     }
     
     void OnTriggerEnter(Collider other) {
         if(other.gameObject.name == "Enemy"){
              
             sprite.Play("hurtt");
                 if(sprite.IsAnimationPlaying ("hurtt")){
                 
                 StartCoroutine(Death());
             
         
     
 
         
     }
   }
 
     }
     
     void OnTriggerExit(Collider other) {
         
     }
     
 
 }


right2.png (18.4 kB)
left2.png (18.4 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

[2D] Click/touch handler for both Mobile and PC 0 Answers

[SOLVED]2D lookAt is off, any ideas? 1 Answer

2D rotating dial 1 Answer

Find distance within screen! 2 Answers

Trying to make my 2D Character Controller stick to a wall. 0 Answers


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