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 Ak0rn · Apr 23, 2019 at 09:22 PM · scripting problemscripting beginnerscriptingbasics

Make an object stationary until player is colliding with it, then move along path.

I've been trying to make a script for an elevator. So far I've got the movement part done but I can't figure out how to stop the script from starting before the player is on the elevator object.

This is what I've got so far :

public class LiftScript : MonoBehaviour

{ private Vector3 posA;

 private Vector3 posB;

 [SerializeField]
 private float speed;

 [SerializeField]
 private Transform startPosition;

 [SerializeField]
 private Transform endPosition;


 void Start()
 {


     posA = startPosition.localPosition;
     posB = endPosition.localPosition;
 }

 void Update()
 {
     Move();
 }

 private void Move()
 {
     startPosition.localPosition = Vector3.MoveTowards(startPosition.localPosition, posB, speed * Time.deltaTime);
 }

}

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 KevRev · Apr 23, 2019 at 11:04 PM

I'd probably have a collider, smaller than the lift, in the centre, and when the player collides with it, start the movement. This code should help point you in the right direction:

   void OnCollisionEnter()
      {
          Moving=true;
      }
 
   void OnCollisionExit()
     {
         Moving=false;
     }
 
    void Update ()
     {
         if (Moving) Move();
     }


Comment
Add comment · Show 2 · 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 Ak0rn · Apr 25, 2019 at 06:21 PM 0
Share

Took me a $$anonymous$$ute to fill in the blanks but I ended up figuring out a way of doing it. Not sure if it's the way you were thinking of doing it but what I came up with is below. I also put in a set parent so that the player wouldn't slide off.

public class LiftScript : $$anonymous$$onoBehaviour

// This Script creates a platform that moves from its original position to a customisable secondary location and back again. The player will follow this platform if touching it { private Vector3 posA;

 private Vector3 posB;

 private Vector3 nextPos;

 [SerializeField]
 private float speed;

 [SerializeField]
 private Transform startPosition;

 [SerializeField]
 private Transform endPosition;

 private bool moving;

 // Use this for initialization
 void Start()
 {
     posA = startPosition.localPosition;
     posB = endPosition.localPosition;
     nextPos = posB;
 }

 private void ChangeDestination()
 {
     nextPos = nextPos != posA ? posA : posB;
 }

 private void OnCollisionEnter(Collision collision)
 {
     if (collision.gameObject.tag == "Player")
     {
         moving = true;
         collision.collider.transform.SetParent(transform);
     }
 }

 private void OnCollisionExit(Collision collision)
 {
     if (collision.gameObject.tag == "Player")
     {
         collision.collider.transform.SetParent(null);
     }
 }

 private void FixedUpdate()
 {
     if (moving)
     {
         startPosition.localPosition = Vector3.$$anonymous$$oveTowards(startPosition.localPosition, nextPos, speed * Time.deltaTime);
     }
 }

}

avatar image KevRev Ak0rn · Apr 25, 2019 at 07:00 PM 0
Share

Perfect! Well done :)
$$anonymous$$aybe also add a line something like the following:

 private void FixedUpdate()
  {
      if (moving && startPosition!=nextPos)
      {
          startPosition.localPosition = Vector3.$$anonymous$$oveTowards(startPosition.localPosition, nextPos, speed * Time.deltaTime);
      }
 else {
 moving=false;
 ChangeDestination();
 }
  }

What this should do, is once it reaches nextPos, it stops moving and swaps the destination. Then, on the next collision, it'll move again to its initial position.

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

252 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

Related Questions

Accessing script from another object 1 Answer

Disable and enable emission property of a material via C# script? 0 Answers

Making a game where objects need to be shot off of a surface. level complete when all objects are shot off. difficulty in scripting for level completion. 0 Answers

How can I temporary increase the size of an object? 2 Answers

how to change the value of my slider(in the inspector) thanks 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