Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 SolidSlish · Jun 26, 2020 at 01:26 PM · controlsswipeslidedown

How do I fix my Swiping Down Errors?

So essentially, when I swipe down more then once while sliding, my character will repeat the sound effect and the particles will go down further. Clearly my script needs revising. How do I make it so when swiping and in the sliding animation, my character can't slide more then once? I dont have this problem for jumping/swiping up.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMotor : MonoBehaviour
 {
     private const float LANE_DISTANCE = 2.0f;
     private const float TURN_SPEED = 0.05f;
 
     //
     private bool isRunning = false;
     public static bool invincible = false;
     public static bool shield = false;
 
 
     // Animation
     private Animator anim;
 
     // Movement
     private CharacterController controller;
     private float jumpForce = 7.0f;
     private float gravity = 12.0f;
     private float verticalVelocity;
     private int desiredLane = 1; // 0 = Left, 1 = Middle, 2 = Right
 
     // Speed Modifier
     private float originalSpeed = 8.5f;
     private float speed = 7.0f;
     public GameObject Player;
     public ParticleSystem MagnetParticles;
     public ParticleSystem MultiplyParticles;
     public float speedPowerupTime;
     public float PowerupDuration = 5;
     Vector3 temp = new Vector3(0f, -1, 0);
 
     private void Start()
     {
         speed = originalSpeed;
         controller = GetComponent<CharacterController>();
         anim = GetComponent<Animator>();
     }
 
     private void Update()
     {
         if (!isRunning)
             return;
 
         // Gather the inputs on which lane we should be in
         if (MobileInput.Instance.SwipeLeft)
             MoveLane(false);
         if (MobileInput.Instance.SwipeRight)
             MoveLane(true);
 
         // calculate where we should be in the future
         Vector3 targetPosition = transform.position.z * Vector3.forward;
         if (desiredLane == 0)
             targetPosition += Vector3.left * LANE_DISTANCE;
         else if (desiredLane == 2)
             targetPosition += Vector3.right * LANE_DISTANCE;
 
         // Lets calculate our move delta
         Vector3 moveVector = Vector3.zero;
         moveVector.x = (targetPosition - transform.position).normalized.x * speed;
 
         bool isGrounded = IsGrounded();
         anim.SetBool("Grounded", isGrounded);
 
         // Calculate Y
         if (isGrounded) // if Grounded
         {
             verticalVelocity = -0.1f;
 
             if (MobileInput.Instance.SwipeUp)
             {
                 // Jump
                 FindObjectOfType<AudioManager>().Play("Jump");
                 anim.SetTrigger("Jump");
                 verticalVelocity = jumpForce;
             }
             else if (MobileInput.Instance.SwipeDown)
             {
                 // Slide
                 FindObjectOfType<AudioManager>().Play("Slide");
                 StartSliding();
                 Invoke("StopSliding", 1.0f);
             }
         }
         else
         {
             verticalVelocity -= (gravity * Time.deltaTime);
 
             // Fast Falling Mechanic
             if (MobileInput.Instance.SwipeDown)
             {
                 verticalVelocity = -jumpForce;
             }
         }
 
         moveVector.y = verticalVelocity;
         moveVector.z = speed;
 
         controller.Move(moveVector * Time.deltaTime);
 
         // Rotate the Character to Where She is Going
         Vector3 dir = controller.velocity;
         if (dir != Vector3.zero)
         {
             dir.y = 0;
             transform.forward = Vector3.Lerp(transform.forward, dir, 0.05f);
         }
 
     }
 
     public void SetSpeed(float modifier)
     {
         speed = 5 + modifier;
     }
 
 
     private void StartSliding()
     {
         anim.SetBool("Sliding", true);
         controller.height /= 2;
         controller.center = new Vector3(controller.center.x, controller.center.y / 2, controller.center.z);
         MagnetParticles.transform.position += temp;
         MultiplyParticles.transform.position += temp;
     }
 
     private void StopSliding()
     {
         anim.SetBool("Sliding", false);
         MagnetParticles.transform.position -= temp;
         MultiplyParticles.transform.position -= temp;
         controller.height *= 2;
         controller.center = new Vector3(controller.center.x, controller.center.y * 2, controller.center.z);
     }
 
     private void MoveLane(bool goingRight)
     {
         desiredLane += (goingRight) ? 1 : -1;
         desiredLane = Mathf.Clamp(desiredLane, 0, 2);
     }
 
     private bool IsGrounded()
     {
         Ray groundRay = new Ray(new Vector3(controller.bounds.center.x, (controller.bounds.center.y - controller.bounds.extents.y) + 0.2f, controller.bounds.center.z), Vector3.down);
         return Physics.Raycast(groundRay, 0.2f + 0.1f);
     }
 
     public void StartRunning()
     {
         isRunning = true;
         anim.SetTrigger("StartRunning");
     }
 
     private void Crash()
     {
         Player.GetComponent<CharacterController>().enabled = false;
         isRunning = false;
         GameManager.Instance.OnDeath();
         anim.SetTrigger("Death");
         speed = -1.0f;
     }
 
     private void OnControllerColliderHit(ControllerColliderHit hit)
     {
         if (hit.gameObject.tag == "Obstacle")
         {
             if (!invincible)
             {
                 if (!shield)
                 {
                     Crash();
                 }
                 else
                 {
                     shield = false;
                     FindObjectOfType<PowerUpSpawner>().shield.GetComponent<Shield>().Disable();
                 }
             }
             hit.collider.enabled = 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ShadoX · Jun 26, 2020 at 03:10 PM

You probably just want to check if the Player is already sliding or not, before running everything inside StartSliding()

I'm guessing that the Sliding bool is supposed to be false when the Player isn't sliding, so maybe this?

   private void StartSliding()
      {
         if(anim.GetBool("Sliding")){
             return;
         }
          anim.SetBool("Sliding", true);
          controller.height /= 2;
          controller.center = new Vector3(controller.center.x, controller.center.y / 2, controller.center.z);
          MagnetParticles.transform.position += temp;
          MultiplyParticles.transform.position += temp;
      }
Comment
Add comment · Show 1 · 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 SolidSlish · Jun 26, 2020 at 03:54 PM 0
Share

Didn't work, whenever I swipe down twice, the player kind of sinks into the ground, and the double sound effect problem still occurs. Ironically, it also seems to make my power up particle effect go up ins$$anonymous$$d of down. Thanks for trying though, but I'm just so stuck.

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

130 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

Related Questions

Swipe controls error "Cannot implicitly convert type 'UnityEngine.Touch' to 'Touch' " 1 Answer

Swipe & gesture controls for android 1 Answer

swipe movement control 1 Answer

Problem with 3D mobile diagonal swipe controls 0 Answers

Move down while on air (swipe down with key and touch while on air, jumping. Cancel jump and instant go down) 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