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 unity_EJruBHs4DIgk4Q · Jan 07, 2019 at 05:54 AM · transformarraymovement scripttransform.position

My code is not cycling through an array as expected

have another issues with my code

I'm trying to get the 'player' to continue on after he jumps (see code) but what he does is return back to the 'jumpPosition' transform without continuing on to the next point

Please help

Here is the Code I have used:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Events;
 
 public class MovementController : MonoBehaviour
 {
 
     // public GameObject playerToMove;           // not sure why I need this 
 
     public float moveSpeed;                     // move speed of the player going from player postion to current point, possible to use somewhere else
 
     private Transform currentPoint;             // used to determine where the next point the player has to move by cycling through 'points' array
     public Transform jumpPoint;                 // used as a location trigger to tell the player when to jump -- will be attempting to make into an array
     public Transform crouchPoint;               // used as a location trigger to tell the player when to crounch -- will be attempting to make into an array
     public Transform[] points;                  // an array of location for the 'currentPoint' to cycle through 
 
     public float maxPause = 100;                // used to determine the length of time between when the player arrives at the 'currentPoint' and when to leave said point; default = 100
     public float reducedPause = 2;                // used to set 'maxPause' to a smaller number so that player won't keep jumping/crouching
 
     public TestCharacterController2D controller; // acceses the TestCharacterController2D script (I didnt write this script but plan to modiify) used for basic move, jump, and crouch funtions 
 
     public Animator animator;                   // my attempt to find the player's animator
 
     public bool isRight;                        // used to to determine which way the character is facing -- I think this can be accesed through the 'controller' variable (TestCharacterController2D script)
 
     private bool jump;                          // to tell the 'controller' when to jump
     private bool crouch;                        // to tell the 'controller' when to crouch
     private bool pause = false;                 // used to determine when the player arrives at the 'currentPoint' and the 'maxPause' countdown begins
 
     public int pointsSelection;                 // used to cycle the 'points' array when maxPause cycle is over and player is at current point
 
     //   public float jumpHeight = 100f;             // not sure why used
 
 
     void Start()                                         // looking into 'onAwake' maybe? (or others)
     {
         currentPoint = points[pointsSelection];         // sets currentPoint to default location ('pointSelection' is 'publc' so can be modified in Unity
 
 
         isRight = true;                                 // player starts facing right -- as per character animations
 
     }
 
     void Update() // not sure if should have more in 'FixedUpdate' or others (maybe?)
     {
         jump = false;
 
         if (Vector2.Distance(transform.position, currentPoint.position) < 0.05f)
         // checks to see if player is at 'currentPoint' 
         {
             pause = true; // starts the pause sequenece
             Debug.Log("Pause = " + pause);
 
             if (pause) // when the movement is pause do the the following
             {
                 moveSpeed = 0;
 
                 animator.SetFloat("Speed", 0); // player stops moving -- works!
 
                 if (maxPause <= 100) // checks to see if still paused
                 {
 
                     Debug.Log("this is maxPause: " + maxPause);
 
                     if (maxPause < 0) // found 'maxPause' was going to far below zero
                         maxPause = 0;
 
                     maxPause--; // reduce pause amount (working way out of loop)
                  
                 }
 
                
 
                 if (maxPause == 0) // when 'maxPause' timer has finished
                 {
                     pointsSelection++; // move to next point
                     maxPause = 100; // reset 'maxPause' timer
                     pause = false; // resume 'transform.position == currentPoint.position' process
                 }
 
             }
 
 
             if (pointsSelection == points.Length) // makes sure 'pointsSelection' doesn't go out of bounds
             {
                 Debug.Log("at end of array");
                 pointsSelection = 0; // start the player's movement process over again
             }
 
         }
         else // not sure if requried
         {
             Debug.Log("pause = false");
             Debug.Log("this is the moveSpeed " + moveSpeed);
             Debug.Log("pointsSelection: " + pointsSelection);
         }
 
         if (Vector2.Distance(transform.position, jumpPoint.position) < 0.05f && jumpPoint == currentPoint) // conditions for the jump action (automatic) -- I fell the whole thing needs to be more elaborate ** WORK IN PROGRESS **
         {
             jump = true;
         }
         else
             jump = false;
 
         currentPoint = points[pointsSelection]; // moved to line 130 -- not sure if better here
 
 
     }
 
     void FixedUpdate()
     {
 
         if (isRight && transform.position.x > currentPoint.position.x) // flipping the character -- I'm pretty sure I can use TestCharacterController2D to do this for me, this is comparing the player's 'transform' 
         {
             moveSpeed = -0.25f; // tells controller to head in the left direction
             isRight = false;   // no longer facing right 
         }
         if (!isRight && transform.position.x < currentPoint.position.x) // reverse of above
         {
             moveSpeed = 0.25f; // tells controller to head in the right direction
             isRight = true; // no longer facing left
         }
 
         if (moveSpeed > 0 || moveSpeed < 0)
             animator.SetFloat("Speed", 1); // player starts PlayerRun animation -- works!
 
 
         // Move our character
         controller.Move(moveSpeed, crouch, jump); // draws from the TestCharacterController2D script
 
     }
 
    
     public void OnLanding()
     {
         animator.SetBool("Jumped", false);
     }
 
 }
 

Here's what unity is doing: (see gif)

https://1drv.ms/u/s!AkHltNliis8BhM0cewU4SfBdn2fInA

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
Best Answer

Answer by Neo_Genesis10 · Jan 07, 2019 at 03:52 PM

I can see one particular issue which could be the cause. You have a line of code which subtracts 1 from the maxPause value. This immediately follows the error checking which checks if it's less than zero. This should be the other way around. This means that the following line which checks if the value is equal to zero may not actually occur.

You can mitigate this entirely of course by swapping the line if (maxPause <= 100) to if (maxPause > 0). Unless you have a different need for values above 100, this simple change would prevent it dropping below zero in the first place.

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 unity_EJruBHs4DIgk4Q · Jan 08, 2019 at 01:26 AM 0
Share

It works!!!! ..... i think I have a little more debugging to do but thanks for the tip!!

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

208 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

Related Questions

how to make the player move in only 2 directions? 0 Answers

Moving spawned enemies randomly towards several pre-defined positions 1 Answer

fill public Transform[] on Awake? 1 Answer

transform.Translate is too smooth 3 Answers

I am trying to say if the Y of my player reaches below 0 change the players position to 0,1,0 it wont work thought and I tried using Vector3 but it says I cant use Vector3 like a method Please help! 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