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
1
Question by Iceblitzyt · Nov 09, 2014 at 12:56 AM · c#platformmoving

Moving Platform Help (c#)

Hey boys and girls,

So I made this simple script after not using unity for 9 months and I really.. REALLY RUSTY with everything. I know I've made a script like this before but I can't fix it. It's a simple script, I want the object to move between 2 waypoints (A & B). this is the error I get:

 NullReferenceException: Object reference not set to an instance of an object
 MovingPlatforms.Update () (at Assets/Game scripts/Enviroment Scripts/Platforms/MovingPlatforms.cs:13)


and this is my code:

 using UnityEngine;
 using System.Collections;
 
 public class MovingPlatforms : MonoBehaviour 
 {
     public Transform[] Waypoints;
     public bool move = false;
     public float speed = 5;
     
 
     void Update () 
     {
         if(transform.position.y < Waypoints[1].transform.position.y && move == false)
         {
             this.transform.position = Vector3.Slerp(transform.position, Waypoints[1].transform.position, speed * Time.deltaTime);
             move = true;
         }
         else if(transform.position.y > Waypoints[2].transform.position.y && move == true)
         {
             this.transform.position = Vector3.Slerp(transform.position, Waypoints[2].transform.position, speed * Time.deltaTime);
             move = false;
         }
     
     }
 }
 




Any help would be greatly appreciated.

Comment
Add comment · Show 3
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 dmg0600 · Nov 09, 2014 at 01:24 AM 0
Share

Are you sure that error is given by this code and not by a previous version of the script?

The only error I see it can give is IndexOutOfRange if the array does not have enough Transforms to access Waypoints[1] and Waypoints[2].

Are you sure you want to access Waypoints[1] and Waypoints[2] ins$$anonymous$$d of Waypoints[0] and Waypoints[1]? 0 and 1 are the two first elements of the array.

All the other variables you are using are O$$anonymous$$ as move and speed are declared and given a value and every $$anonymous$$onoBehaviour have a transform. The problem must be at the array.

avatar image Iceblitzyt · Nov 09, 2014 at 01:38 AM 0
Share

Fixed thank you!!

avatar image Iceblitzyt · Nov 09, 2014 at 01:40 AM 0
Share

Well the issues gone but it wont move :(

3 Replies

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

Answer by Iceblitzyt · Nov 09, 2014 at 06:34 PM

I figured out what my issues were and I made the code like 100% easier and more usable for multiple waypoints. I'm going to include it as a learning resource incase someone stumbles accross this in the future:

 using UnityEngine;
 using System.Collections;
 
 public class MovingPlatforms : MonoBehaviour 
 {
     public Transform[] Waypoints;
     public float speed = 2;
 
     public int CurrentPoint = 0;
 
     void Update () 
     {
         if(transform.position.y != Waypoints[CurrentPoint].transform.position.y)
         {
             transform.position = Vector3.MoveTowards(transform.position, Waypoints[CurrentPoint].transform.position, speed * Time.deltaTime);
         }
 
         if(transform.position.y == Waypoints[CurrentPoint].transform.position.y)
         {
             CurrentPoint +=1;
         }
         if( CurrentPoint >= Waypoints.Length)
         {
             CurrentPoint = 0; 
         }
     }
 }
 
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
2

Answer by CalvinAMi · Jun 03, 2015 at 06:55 AM

I tweaked your script (and am currently successfully using it... thank you). Each waypoint can now transition between position, rotation, and scale to include the entire Transform. Great job on the simple script!

Here's my version:

I am using "void FixedUpdate()", but you can also use the normal "void Update()"

 using UnityEngine;
 using System.Collections;
 
 public class MovingPlatform : MonoBehaviour 
 {
     public Transform[] Waypoints;
     public float moveSpeed = 3;
     public float rotateSpeed = 0.5f;
     public float scaleSpeed = 0.5f;
     public int CurrentPoint = 0;
     
     void FixedUpdate () 
     {
         if(transform.position != Waypoints[CurrentPoint].transform.position)
         {
             transform.position = Vector3.MoveTowards(transform.position, Waypoints[CurrentPoint].transform.position, moveSpeed * Time.deltaTime);
             transform.rotation = Quaternion.Lerp (transform.rotation, Waypoints[CurrentPoint].transform.rotation, rotateSpeed * Time.deltaTime);
             transform.localScale = Vector3.Lerp (transform.localScale, Waypoints[CurrentPoint].transform.localScale, scaleSpeed * Time.deltaTime);
         }
         
         if(transform.position == Waypoints[CurrentPoint].transform.position)
         {
             CurrentPoint +=1;
         }
         if( CurrentPoint >= Waypoints.Length)
         {
             CurrentPoint = 0; 
         }
     }
 }
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 money4honey · Oct 13, 2016 at 10:54 AM

Hi, that's my script for simple moving platform, wrote it yesterday.

 using UnityEngine;
 using System.Collections;
 
 public class MovingPlatform : MonoBehaviour 
 {
     public float speed = 0.5f, reductionFactor = 1;
     public bool moveOnTouch = false;
 
     Transform toPoint, playerParent;
     Vector3 fromPos, toPos;
     bool forward = true, touch = false;
     float distFrom, distTo, moveSpeed, lowSpeedDist;
 
     void Awake() {
         toPoint = transform.Find ("toPoint");
         fromPos = transform.position;
         toPos = toPoint.transform.position;
         lowSpeedDist = Mathf.Abs (fromPos.x - toPos.x) * 0.15f;
         toPoint.parent = null;
     }
 
     void FixedUpdate() {
         if (!moveOnTouch) {
             Move ();
         } else if (touch) {
             Move ();
         }
     }
 
     void Move() {
         distFrom = Mathf.Abs(transform.position.x - fromPos.x);
         distTo = Mathf.Abs(transform.position.x - toPos.x);
         reductionFactor = (reductionFactor > 0 ? reductionFactor : 1);
         moveSpeed = (distFrom < lowSpeedDist || distTo < lowSpeedDist ? speed / reductionFactor : speed);
 
         transform.position = Vector3.MoveTowards (transform.position, 
             (checkPos () ? toPos : fromPos),
             moveSpeed * Time.deltaTime);
     }
 
     bool checkPos () {
         bool result = true;
 
         if (forward && transform.position.x != toPos.x) {
             result = true;
         }
         else {
             forward = false;
         }
 
         if (!forward && transform.position.x != fromPos.x) {
             result = false;
         }
         else {
             forward = true;
         }
 
         return result;
     }
 
     void OnTriggerEnter2D(Collider2D other) {
         if (other.gameObject.CompareTag ("Player")) {
             touch = true;
             playerParent = other.transform.parent;
             other.transform.parent = transform;
         }
     }
 
     void OnTriggerExit2D(Collider2D other) {
         if (other.gameObject.CompareTag ("Player")) {
             touch = false;
             other.transform.parent = playerParent;
         }
     }
 }

alt text


d54965e4ec.jpg (239.8 kB)
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

31 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

Related Questions

Multiple Cars not working 1 Answer

moving platforms? 0 Answers

Distribute terrain in zones 3 Answers

Rigidbody on a platform 2 Answers

Multiplayer - Other players shaking back and fourth on moving platform 2 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