How to prevent the player to move beyond certain x-position value
Hey,
I'm pretty new to Unity and I'm currently working on a 3D asteroid evasion game. The player is piloting a ship through an asteroid field. The ship moves constantly forward and the player can only navigate up, down, left and right. As I don't want to make an infinite asteroid field, I want my player to stay inside the field and stop him from moving out of it. The field goes from -10 to 10 on the X and -10 to 10 on the Y. I want to track the player position and if he moves past the -10 or 10 on the X position, his x-position should be set to 10 (or -10). Same on the Y-position. Any idea on how to do that in C#?
Thanks and best, Mat
Answer by pixelbash_de · Feb 19, 2018 at 02:16 PM
Ok I've got it to work now.
this is the final, working code:
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public Rigidbody player;
public float forwardForce = 2000f;
public float sidewaysForce = 500f;
void SetTransformX(float n)
{
player.transform.position = new Vector3(n, player.transform.position.y, player.transform.position.z);
}
void SetTransformY(float n)
{
player.transform.position = new Vector3(player.transform.position.x, n, player.transform.position.z);
}
// Update is called once per frame
void FixedUpdate () {
player.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("d"))
{
player.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a"))
{
player.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("w"))
{
player.AddForce(0, sidewaysForce * Time.deltaTime, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("s"))
{
player.AddForce(0, -sidewaysForce * Time.deltaTime, 0, ForceMode.VelocityChange);
}
if (player.transform.position.x >= 10) { SetTransformX(10.0f); player.velocity = new Vector3(0, player.velocity.y, player.velocity.z); }
if (player.transform.position.x <= -10) { SetTransformX(-10.0f); player.velocity = new Vector3(0, player.velocity.z, player.velocity.z); }
if (player.transform.position.y <= 0) { SetTransformY(0.0f); player.velocity = new Vector3(player.velocity.x, 0, player.velocity.z); }
if (player.transform.position.y >= 10) { SetTransformY(10.0f); player.velocity = new Vector3(player.velocity.x, 0, player.velocity.z); }
}
}
--- old response ---
Ok, I might need to clarify my struggle a bit. I understand the code you guys posted and that is also what I had in mind, but I don't know how to do it with the code I currently have.
This is my current player movement code:
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public Rigidbody player;
public float forwardForce = 2000f;
public float sidewaysForce = 500f;
// Update is called once per frame
void FixedUpdate () {
player.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("d"))
{
player.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
player.transform.Rotate(0, 0, 20 * Time.deltaTime);
}
if (Input.GetKey("a"))
{
player.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
player.transform.Rotate(0, 0, -20 * Time.deltaTime);
}
if (Input.GetKey("w"))
{
player.AddForce(0, sidewaysForce * Time.deltaTime, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("s"))
{
player.AddForce(0, -sidewaysForce * Time.deltaTime, 0, ForceMode.VelocityChange);
}
}
}
If I use this simple code-snippet:
if (player.transform.position.x >= 10) { player.transform.position.x = 10; }
Visual Studio gives me the following error:
player.transform.position.x is not a variable and therefore cannot be changed
so this seems not to work... :-/
dont use player.transform.position.x just use transform.position.x
player is your rigidbody you assigned on top, but transform.position is the position of the gameobject you attached the script to.
Using only "transform.position.x" results in the same error: transform.position.x is not a variable and therefore cannot be changed
Try this then: if (transform.position.x >= 10) { //keep the original y and z coordinates while change the x only transform.position = new Vector3(10, transform.position.y, transform.position.z; } else if(transform.position.x <= -10) { transform.position = new Vector3(-10, transform.position.y, transform.position.z) }
do the same for the y axis
Yes, this works... I now need to remove the force that I give to the player (player.AddForce) when he reaches X 10/-10, as this is still applied to the ship and therefore it feels like the ship is stuck in this "invisible wall" as the player needs to apply a negative force into the other direction before the ship will move in that direction...
Try using inside the piece of code where you reset the transform.position: rigidbody.velocity = Vector3.zero and also rigidbody.angularVelocity = Vector3.zero
Answer by Pandaa2610 · Feb 18, 2018 at 02:02 PM
You answered your own question,Just write:
If (transform.position.x < 10) { transform.position.x = 10 }
Four times for x,y axis (Positive and negative value)
Answer by yokenstein · Feb 18, 2018 at 02:08 PM
You can do this 2 ways: 1) use colliders and place them on the boundaries of your screen (make sure they're not isTrigger). This will be easier and a more preferable way to do it 2) in code, you could set constraints of your spaceship:
if(transform.position.x > 10)
{
transform.position.x = 10;
}
else if(transform.position.x < -10)
{
transform.position.x = -10;
}
if(transform.position.y > 10)
{
transform.position.y = 10;
}
else if(transform.position.y < -10)
{
transform.position.y = -10;
}
Your answer
Follow this Question
Related Questions
Change player gravity by pressing a key? 1 Answer
Quick question with a Camera Following script 1 Answer
Need help with this code? thanks. 2 Answers
Trying to Generate Different Random Values for Position of Game Object Instances [C#] 1 Answer
Transfering script to other game objects via code (Unity 2D) 0 Answers