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 Blogan · Feb 03, 2019 at 04:22 AM · scripting problemscripting beginner

How do I ignore input when player object has reached a certain point?

I am a beginner programmer and I am trying to remake "pong" with very little help but I encountered a problem while trying to get the player to stop moving when it has reached a specific position. The object never stops moving and can go on forever. Here is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player1Movement : MonoBehaviour {
 
 public GameObject Player; *Player Object*
     public Transform p1; *Player's transform*
     public Transform endPos; *Empty game object where the player is supposed to stop (0, 4.25, 0)*
         
     // Use this for initialization
     void Start ()
     {
         transform.Translate(0f, 0f, 0f); *start position*
     }
     
     // Update is called once per frame
     void Update ()
     {
         transform.Translate(0f, Input.GetAxis("Player1Movement"), 0f); *input*
 
         if (p1 == endPos) *Not sure if this is correct*
         {
             **I DON'T KNOW WHAT TO PUT HERE**
         }
     }
 }

I just need to know what to put in the 'if' statement. Otherwise, everything else runs very smooth.

Comment
Add comment · Show 1
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 tormentoarmagedoom · Feb 03, 2019 at 12:44 PM 3
Share

Good day

for comments, inside a script, you must use //

 public GameObject Player;  //Player Object
 public Transform p1;  //Player's transform
 public Transform endPos; //Empty game object where the player is supposed to stop (0, 4.25, 0)


1 Reply

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

Answer by tormentoarmagedoom · Feb 03, 2019 at 12:53 PM

Good day.

I see so many errors...

First, for comment a line you must use //

Second, this is a function, not a variable change. If something have the () symbols, means its a function.

 transform.Translate(); 

This is the function Translate() wich means MOVE!

You pretend to set initial position? then do:

 transform.position = Vector3.zero;

Third, you want tio know when the player has reached endpos. So you must check for their position! You are now comparing its transform component, which has no sense..

you prentend to do this:

 if (p1.position == endPos.position) 

But, position is a float, so it have many decimals, and pretend that they will be exactly the same ...is hard. So you should calculate the distance between that 2 positiond with Vector3.Distance() function and check if is smaller than some value..

 if ((Vector3.Distance(p1.position, endPos.position) < 10)

And the Input.GetAxis you are using, you must define before that axis... Is better to just detect keys one by 1 to commence learning..


Look for Unity ansers about all of this, read read read, try try try.

It's nice you try to do things at the beggining of learning, but maybe you should see more tutoprials, basic tutorials about Unity, learn what is a variable, a component, a class, a method, a function, etc..

Good luck!

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

242 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

Related Questions

How do I assign values to variables in the Update function only ONCE? 1 Answer

Making a sword follow mouse movement 1 Answer

Flashlight flickering script? 0 Answers

NavMesh of an automatically generated maze 0 Answers

Displaying Text on Touch Event 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