Hi, im new to unity so please make your answer simple, thanks. I have a sprite who is my player , I want to make Button sprites on the screen so when a person clicks them with a mouse he moves, for some reason i when i press the Button sprites the player sprite only moves right and not left.
Here is the code for the player
using UnityEngine;
using System.Collections;
public class player : MonoBehaviour {
public Vector3 initvel;
// Update is called once per frame
void Update () {
this.rigidbody2D.velocity = initvel ;
}
}
Here is the left button sprite
using UnityEngine;
using System.Collections;
public class screen_touch_left : MonoBehaviour {
void Update () {
player.FindObjectOfType<player>().initvel.x = 0 ;
if (Input.GetMouseButtonDown(0) ) {
Vector3 mouseWorldPostion = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 mousePos2D = new Vector2(mouseWorldPostion.x, mouseWorldPostion.y);
Vector2 direction = Vector2.zero;
//ray cast postion and direction.
RaycastHit2D hit = Physics2D.Raycast(mousePos2D,direction);
if(hit != null && hit.collider != null){
//we clicked on something that has a collider
if(hit.collider.rigidbody2D != null){
player.FindObjectOfType<player>().initvel.x = -15 ;
}
}
}
}
}
Here is the right button sprite
using UnityEngine;
using System.Collections;
public class screen_touch_left : MonoBehaviour {
void Update () {
player.FindObjectOfType<player>().initvel.x = 0 ;
if (Input.GetMouseButtonDown(0) ) {
Vector3 mouseWorldPostion = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 mousePos2D = new Vector2(mouseWorldPostion.x, mouseWorldPostion.y);
Vector2 direction = Vector2.zero;
//ray cast postion and direction.
RaycastHit2D hit = Physics2D.Raycast(mousePos2D,direction);
if(hit != null && hit.collider != null){
//we clicked on something that has a collider
if(hit.collider.rigidbody2D != null){
player.FindObjectOfType<player>().initvel.x = 15 ;
}
}
}
}
}