Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by reef99 · Mar 12, 2016 at 03:44 AM · controllertouch controlsshooterstickdual

Help with mobile touch dual stick controls. Rotate or Snap character to the right stick. (Top Down)

Hi all,

This might be a noob-of-the-year worthy question, so sorry, but I have wasted so much time already on this... Basically I found a script which I managed to implement into my game. It allows me to control my character by using 1 stick mobile input.

Sadly it makes the game rubbish as the game was originally dual stick (mouse and keys). I would really appreciate some help separating the bit of code I have into two sticks...

I have fiddled with it and searched all over and have only broken what works and have not achieved what I want so I am desperate now (I broke it so that the player turns right when I press up- but i think i will find a way to fix that myself).

Thank you for any input everyone (clearly I am not a programmer, but I am learning, just very slowly- always used PlayMaker in the past).

I would like the character to snap to face a direction (or rotate) according to the right stick, much like in geometry wars, and continue to move using the left stick.

Bit of Code I think I need to Split : (starts on line 106 in full script)

 Vector3 turnDir = new Vector3(CrossPlatformInputManager.GetAxisRaw("Horizontal"), 0f, CrossPlatformInputManager.GetAxisRaw("Vertical"));
          if(turnDir != Vector3.zero){
             Vector3 playerToMouse = (transform.position + turnDir) - transform.position;
             playerToMouse.y = 0;
             Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
             playerRigidBody.MoveRotation(NewRotation); 
         }

Entire Script:

 using UnityEngine;
 using System.Collections;
 using UnityStandardAssets.CrossPlatformInput;
 using UnityEngine.UI;
 
 public class GrunkBehaviorScript : MonoBehaviour {
 
     public float speed = 2.0f;
     public GameObject gameOverPanel;
     PlayerHealth playerHealth;
     public int score;
     public Text scoreText;
 
     Vector3 movement;
     Rigidbody playerRigidBody;
     bool isMoving = false;
     Animator anim;
     int floorMask;
     float camRayLength = 100.0f;
     public bool isEnabled = true;
     public bool gameOver = false;
 
     // Use this for initialization
     void Start () {
         gameOverPanel.SetActive (false);
         GameObject player = GameObject.FindGameObjectWithTag ("Player");
         playerHealth = player.GetComponent<PlayerHealth> ();
         score = 0;
         scoreText.text = "Score: 0";
     
     }
     
     // Update is called once per frame
     void Update () {
         if (playerHealth.currentHealth <= 0) {
             isEnabled = false;
         if (!gameOver) {
             Invoke ("DisplayGameOver", 1.0f);
             }
         }
 
         scoreText.text = "Score: " + score.ToString ();
 
     }
 
     void Awake() {
         playerRigidBody = GetComponent<Rigidbody> ();
         anim = GetComponent<Animator> ();
         floorMask = LayerMask.GetMask ("Floor");
     }
 
     void FixedUpdate (){
         if (isEnabled == false)
             return;
         // get button pressed by player
         float h = CrossPlatformInputManager.GetAxisRaw ("Horizontal");
         float v = CrossPlatformInputManager.GetAxisRaw ("Vertical");
 
         Move (h, v);
 
         if (h !=0 || v != 0) {
             isMoving = true;
         } else {
             isMoving = false;
         }
         Animating ();
         Turning ();
     }
 
 
     void Move( float h, float v){
         movement.Set (v, 0f, -h);
 
         movement = movement.normalized * Time.fixedDeltaTime * speed;
 
         playerRigidBody.MovePosition (transform.position + movement);
 
     }
 
 
     void Animating(){
         // if char is moving, then play walk anim, if not play idle anim
         if (isMoving == true) {
             //if true play animation
             anim.SetFloat ("speed", 1);
         } else {
             anim.SetFloat ("speed", 0);
         }
     }
 
     void Turning(){
         //Find Mouse Position and check if in range
         //if in range, rotate character towards mouse
 
         #if !MOBILE_INPUT
         Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
         RaycastHit floorHit;
         if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask)) {
             Vector3 playerToMouse = floorHit.point - transform.position;
             playerToMouse.y = 0.0f;
             Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
             playerRigidBody.MoveRotation(newRotation);
 
         }
         #else
         Vector3 turnDir = new Vector3(CrossPlatformInputManager.GetAxisRaw("Horizontal"), 0f, CrossPlatformInputManager.GetAxisRaw("Vertical"));
          if(turnDir != Vector3.zero){
             Vector3 playerToMouse = (transform.poition + turnDir) - transform.position;
             playerToMouse.y = 0;
             Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
             playerRigidBody.MoveRotation(NewRotation); 
         }
         #endif
     }
 
 
     public void DisableMovement(){
         isEnabled = false;
     }
 
     public void RestartGame(){
         Application.LoadLevel ("SceneWithSpidersAndStuff");
     }
 
     void DisplayGameOver(){
             gameOver = true;
         gameOverPanel.SetActive (true);
     }
 }

I think this bit of code works perfectly with the mouse to do what I want, but im not sure:

          Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
          RaycastHit floorHit;
          if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask)) {
              Vector3 playerToMouse = floorHit.point - transform.position;
              playerToMouse.y = 0.0f;
              Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
              playerRigidBody.MoveRotation(newRotation);



Thanks again to anyone reading.

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 reef99 · Mar 13, 2016 at 01:11 AM

Well I figured it out,

The short of it is:

I added another single joystick and called the axis' mouse x' and 'mouse y' then in the first bit of script I posted I changed the' horizontal' axis to 'mouse x' and 'vertical' to 'mouse y'.

Also I modified the script of the unity standard asset joystick. (Yeah, I know, me modifying scripts- im like a genius overnight). I changed a '+' to a '-' somewhere and now my character moves left and right correctly - but the funniest thing happened: I move my finger to right- character moves to right - but joystick sprite moves to left lol. I guess i better keep studying the script until I find where I change another '+' to a '-',

So essentially I think my problem is solved, here is my game running as I wish (except for the right stick flying left when I move it right lol): https://www.youtube.com/watch?v=JImpqcABzIU

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 omerselman · Apr 23, 2016 at 02:06 AM

can you post the modified version of the script?

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

50 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Single Stick Controller Prefab Dont work properly 1 Answer

How could I make a fps touch controller? 0 Answers

How to set up the right controller stick 0 Answers

Controller Stick Input "Snaps" 0 Answers

Touchscreen Controls Like GetAxis 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