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 Bentley100 · Mar 29, 2021 at 06:41 AM · androidmovement script

How to change movement script for computer for android.

Hello, i have this script but it is made for a computer not android. i am trying to make this game for android i am stuck on the movement. if you can make it into an android script i already have a joystick pack press here to see. Here is the script,

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 [RequireComponent(typeof(CharacterController))]
 
 public class Movement : MonoBehaviour
 {
     public float walkingSpeed = 7.5f;
     public float runningSpeed = 11.5f;
     public float jumpSpeed = 8.0f;
     public float gravity = 20.0f;
     public VariableJoystick variableJoystick;
     public float lookSpeed = 2.0f;
     public float lookXLimit = 45.0f;
 
     CharacterController characterController;
     Vector3 moveDirection = Vector3.zero;
     float rotationX = 0;
 
     [HideInInspector]
     public bool canMove = true;
 
     void Start()
     {
         characterController = GetComponent<CharacterController>();
 
         // Lock cursor
         
     }
 
     void Update()
     {
         // We are grounded, so recalculate move direction based on axes
         Vector3 forward = transform.TransformDirection(Vector3.forward);
         Vector3 right = transform.TransformDirection(Vector3.right);
         // Press Left Shift to run
         bool isRunning = Input.GetKey(KeyCode.LeftShift);
         float curSpeedX = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Vertical") : 0;
         float curSpeedY = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Horizontal") : 0;
         float movementDirectionY = moveDirection.y;
         moveDirection = (forward * curSpeedX) + (right * curSpeedY);
 
         if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
         {
             moveDirection.y = jumpSpeed;
         }
         else
         {
             moveDirection.y = movementDirectionY;
         }
 
         // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
         // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
         // as an acceleration (ms^-2)
         if (!characterController.isGrounded)
         {
             moveDirection.y -= gravity * Time.deltaTime;
         }
 
         // Move the controller
         characterController.Move(moveDirection * Time.deltaTime);
 
         // Player and Camera rotation
         if (canMove)
         {
             rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
             rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
             
             transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
         }
     }
 
     }


Comment
Add comment · Show 7
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 Bentley100 · Mar 30, 2021 at 08:10 PM 0
Share

Thank you so much but do you know how to change the rotation bit to be a finger instead of a mouse

avatar image Llama_w_2Ls Bentley100 · Mar 31, 2021 at 06:03 AM 1
Share

Yes I have done something similar but a bit complicated. You'll need to use Input.GetTouch() to get the positions of multiple touches on the screen simultaneously. I would use Input.GetTouch(1) for now, so the second touch on the screen is the mouse movement, and the first is the joystick. Anyway, here's the full script (i'm not entirely sure if it works):

 public class LookAround : MonoBehaviour
 { 
     Vector2 AnchorPoint;
     Vector3 PreviousCameraRotation;
 
     public float LookSensitivity;
 
     private void Start()
     {
         AnchorPoint = new Vector2(Screen.width / 2, Screen.height / 2);
         PreviousCameraRotation = transform.rotation.eulerAngles;
     }
 
     void Update()
     {
         CameraLook();
     }
 
     private void CameraLook()
     {
         if (Input.touchCount < 1)
             return;
 
         Vector2 touchPos = Input.GetTouch(1).position;
 
         int ScreenWidth = Screen.currentResolution.width;
         int ScreenHeight = Screen.currentResolution.height;
 
         float xRotation = (touchPos.y - AnchorPoint.y) / ScreenHeight * LookSensitivity;
         float yRotation = (touchPos.x - AnchorPoint.x) / ScreenWidth * LookSensitivity;
 
         transform.rotation = Quaternion.Euler
         (
             -xRotation + PreviousCameraRotation.x,
             yRotation + PreviousCameraRotation.y,
             transform.rotation.z
         );
     }
 }

@Bentley100

avatar image Bentley100 Llama_w_2Ls · Apr 03, 2021 at 06:59 AM 0
Share

Hello i just tested it and it does not work if you have any other scripts like this could you please say. Thanks @Llama_w_2Ls

Show more comments
avatar image Bentley100 · Mar 31, 2021 at 08:09 AM 0
Share

thank you @Llama_w_2Ls

avatar image Bentley100 · Mar 31, 2021 at 10:05 PM 0
Share

One more thing, can i make the jump key into a button instead of space @Llama_w_2Ls

avatar image Llama_w_2Ls Bentley100 · Apr 02, 2021 at 02:36 PM 0
Share

You would need a bool called IsJumping, and a public function that is assigned to the button's on click event. For example:

 bool IsJumping = false;
 
 //....
 
 void Update()
 {
      if (IsJumping)
      {
           //...
 
           IsJumping = false;
      }
 }
 
 // This is assigned to the button's on click event in the inspector
 public void Jump()
 {
      IsJumping = true;
 }

@Bentley100 please accept the answer as correct if it helped enough. Thanks

1 Reply

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

Answer by Llama_w_2Ls · Mar 29, 2021 at 06:57 PM

The link doesn't work, but if you're talking about the free Fenerax joystick pack on the asset store Fenerax joystick package

Then you can replace Input.GetAxis("Horizontal") with joystick.horizontal, for example. Here's some general code:

 public VariableJoystick joystick;
 
 void Update()
 {
      // Same as Input.GetAxis() for horizontal/vertical asx
      float x = joystick.horizontal;
      float z = joystick.vertical;
 
      // Movement code...
 }

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

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

Everything is choppy... How can I make object movement more fluid? 3 Answers

2D Vector Movement,Vectorel Movement with Buttons 2 Answers

Android based ARCore example ObjectManipulation using Joystick Controller 0 Answers

Android APK acts different than in Editor 1 Answer

Unity Android movement 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