Hi, I´m trying to set limits for player movement, but since I´m not using transform.positions I was unable to do it. Anyone knows how it can be done? This is the code I´m working with.
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public Vector2 speed = new Vector2(50, 50);
private Vector2 movement;
public int dir = -1;
private float cmovforce = -5;
void Update() {
bool shoot = Input.GetButtonDown ("Fire1");
//Ship Mov
if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
{
dir = 1;
}
else if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
{
dir = 1;
}
if (transform.position.x <= -8)
{
dir = 0;
}
Vector3 temp = transform.localScale;
temp.x = dir;
transform.localScale = temp;
float cmov = cmovforce * dir;
if (shoot) {
WeaponScriptPlayer weapon = GetComponent<WeaponScriptPlayer> ();
if (weapon != null) {
weapon.Attack (false);
}
}
{
float inputX = Input.GetAxis("Horizontal");
float inputY = Input.GetAxis("Vertical");
// 4 - Movement per direction
movement = new Vector2((speed.x * inputX) + cmov, speed.y * inputY);
/*(
speed.x * inputX,
speed.y * inputY);*/
}
}
void FixedUpdate()
{
GetComponent<Rigidbody2D>().velocity = movement;
}
void OnCollisionEnter2D(Collision2D collision)
{
bool damagePlayer = false;
// Collision with enemy
AtAtScript enemy = collision.gameObject.GetComponent<AtAtScript>();
if (enemy != null)
/*{
// Kill the enemy
HealthScript enemyHealth = enemy.GetComponent<HealthScript>();
if (enemyHealth != null) enemyHealth.Damage(enemyHealth.hp);
damagePlayer = true;
}*/
// Damage the player
if (damagePlayer)
{
HealthScript playerHealth = this.GetComponent<HealthScript>();
if (playerHealth != null) playerHealth.Damage(20);
}
}
void OnDestroy()
{
transform.parent.gameObject.AddComponent<GameOverScript>();
}
}