- Home /
NEED HELP SETTING BOUNDERYS !!
Hi, I am very VERY new to c# programming, I almost know nothing at all. I am creating a game called 'breakout' And i have been able to manage to get the paddle moving left and right but i cant seem to set a boundry. I have watched many youtube videos and searching the web has turned up nothing. If someone out there can point me in the right direction or even create the code for me that would be awesome ! Here is my code
/// <summary>
/// Paddle.cs ALWAYS AT 0 ON Z AXIS ALWAYS ON Y POSTION -6.5
///
/// </summary>
using UnityEngine;
using System.Collections;
public class Paddle : MonoBehaviour {
public static float zPosition = 0;
public float yPosition = -6.5;
public float speed = 9;
public float xBoundryL = -8.3;
public float xBoundryR = 8.7;
private Transform _t;
void Awake() {
_t = transform;
}
//start
void Start () {
_t.position = new Vector3( 0, yPosition, zPosition);
}
//update is once per frame
void Update () {
//player input
if (Input.GetAxis ("Paddle") != 0) {
//moving paddle according to Xaxis
_t.position = new Vector3( _t.position.x + Input.GetAxis( "Paddle" ) * speed * Time.deltaTime, yPosition, zPosition );
//place boundry code here (got not clue lol)
_t.position.x = new Vector3(Mathf.Clamp (xBoundryL, xBoundryR, 0));
}
}
}
You can't modify a struct(vector3) in c# this way:
_t.position.x = new Vector3($$anonymous$$athf.Clamp (xBoundryL, xBoundryR, 0));
Plus the constructor of Vector3 expects more than a float.
//_t.position.x = new Vector3($$anonymous$$athf.Clamp (xBoundryL, xBoundryR, 0));
Vector3 pos = _t.position;
pos.x = $$anonymous$$athf.Clamp (Time.time, xBoundryL, xBoundryR);
_t.position = pos;
That is proper.
Thanks Landern, But now the paddle flickers towards the positive point. (8.7) Here is my code:
/// <summary>
/// Paddle.cs ALWAYS AT 0 ON Z AXIS ALWAYS ON Y POSTION -6.5
///
/// </summary>
using UnityEngine;
using System.Collections;
public class Paddle : $$anonymous$$onoBehaviour {
public static float zPosition = 0;
public float yPosition = -6.5f;
public float speed = 9f;
public float xBoundryL = -8.3f;
public float xBoundryR = 8.7f;
private Transform _t;
void Awake() {
_t = transform;
}
//start
void Start () {
_t.position = new Vector3( 0, yPosition, zPosition);
}
//update is once per frame
void Update () {
//player input
if (Input.GetAxis ("Paddle") != 0) {
Vector3 pos = _t.position;
pos.x = $$anonymous$$athf.Clamp ( Time.time, -8.3f, 8.7f );
_t.position = pos;
//moving paddle according to Xaxis
_t.position = new Vector3( _t.position.x + Input.GetAxis( "Paddle" ) * speed * Time.deltaTime, yPosition, zPosition );
//place boundry code here (got not clue lol)
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Little problem with Mathf.Clamp as used in Space Shooter Tutorial 8 Answers