Question by
SynGT · Mar 15, 2016 at 02:49 AM ·
input.touch
Assets/Scripts/jumpTouch.js(16,35): BCE0020: An instance of type 'UnityEngine.Touch' is required to access non static member 'position'.
I've been searching for a few hours on this. Essentially I want to apply an up speed if the player touches the top half of the screen & a down speed if the player touches the bottom half.
This is as far as I've gotten & I'm getting the error above. Adding in: var position: Vector2; function Start () { UnityEngine.Touch touch; touch.position = position; }
Just gives me an error code about a semi-colon missing. Any help woudl be appreciate it.
var jumpSpeed : float = .002;
var fallSpeed : float = -.004;
var position : Vector2;
function Start() {
}
function Update() {
if (Input.GetTouch(0).phase) {
if (Touch.position > (Screen.height/2)) {
GetComponent.<Rigidbody2D>().velocity.y = jumpSpeed;
} else {
GetComponent.<Rigidbody2D>().velocity.y = fallSpeed;
}
}
}
Comment
Best Answer
Answer by SynGT · Mar 18, 2016 at 01:50 AM
I did end up getting this to work. For future use:
#pragma strict
var jumpSpeed : float = .002;
var fallSpeed : float = -.004;
var position : Vector2;
function Start() {
}
function Update() {
if (Input.touchCount>0) {
var touch : Touch = Input.GetTouch(0);
if(touch.position.y < Screen.height/2) {
GetComponent.<Rigidbody2D>().velocity.y = jumpSpeed;
} else {
GetComponent.<Rigidbody2D>().velocity.y = fallSpeed;
}
}
}
Answer by Ali-hatem · Mar 15, 2016 at 11:28 AM
c# sorry :
float jumpSpeed = 0.002f;
float fallSpeed = -0.004f;
Rigidbody2D rgb;
void Start()
{
//geting rididbody component only once not each time you touch the screen.
rgb = GetComponent<Rigidbody2D> ();
}
void Update()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)//change the TouchPhase as you want.
{
if( Input.GetTouch(0).position.y > Screen.height/2)
{
rgb.velocity = new Vector2 (0,jumpSpeed);
}
else if(Input.GetTouch(0).position.y < Screen.height/2)
{
rgb.velocity = new Vector2 (0,fallSpeed);
}
}
}
Your answer