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.
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)
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!
Your answer
Follow this Question
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