Hi all, so i was following this video[below] and the problem is that he is using C# all time and im confused as im using Javascript to control my ball. I want my ball to be moved by this joystick but im getting error that i need to put ‘’ ; ‘’ at the end of line but there is ‘’ ; ‘’ so im a bit confused
Here is my code
var joy : Joystick;
var forward = 20;
function FixedUpdate()
{
if (IsGrounded())
{
var joy = GameObject.FindGameObjectWithTag("joystick").GetComponent(Joystick);
Vector2 moveVec = new Vector2(joy.GetAxis("Horizontal"), joy.GetAxis("Vertical")) * forward;
GetComponent.<Rigidbody>().AddForce(moveVec);
}}
and im getting error that on the line with Vector 2 ‘’‘;’ expected. Insert a semicolon at the end.‘’
i would really appreciate if someone could help.
Also how would i be able to make it move forward at var forward speed and make new variables for right and left movement so it moves at different speed in different directions
Looks like you are using C# creation of a variable on that line instead of Unityscript. You need to do “var moveVec: Vector2” instead of “Vector2 moveVec”.
Also, I don’t think you need to use the “new” keyword in Unityscript, but it may not matter. You could try removing it.
i have changed it and it works but i got another error saying ‘’
‘GetAxis’ is not a member of ‘UnityStandardAssets.CrossPlatformInput.Joystick’.
‘’
Im using this code
using System;
using UnityEngine;
using UnityEngine.EventSystems;
namespace UnityStandardAssets.CrossPlatformInput
{
public class Joystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
{
public enum AxisOption
{
// Options for which axes to use
Both, // Use both
OnlyHorizontal, // Only horizontal
OnlyVertical // Only vertical
}
public int MovementRange = 100;
public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input
Vector3 m_StartPos;
bool m_UseX; // Toggle for using the x axis
bool m_UseY; // Toggle for using the Y axis
CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input
CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input
void Start()//was OnEnable
{
m_StartPos = transform.position;
CreateVirtualAxes();
}
void UpdateVirtualAxes(Vector3 value)
{
var delta = m_StartPos - value;
delta.y = -delta.y;
delta /= MovementRange;
if (m_UseX)
{
m_HorizontalVirtualAxis.Update(-delta.x);
}
if (m_UseY)
{
m_VerticalVirtualAxis.Update(delta.y);
}
}
void CreateVirtualAxes()
{
// set axes to use
m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
// create new axes based on axes to use
if (m_UseX)
{
m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
}
if (m_UseY)
{
m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
}
}
public void OnDrag(PointerEventData data)
{
Vector3 newPos = Vector3.zero;
if (m_UseX)
{
int delta = (int)(data.position.x - m_StartPos.x);
//delta = Mathf.Clamp(delta, - MovementRange, MovementRange); //changed
newPos.x = delta;
}
if (m_UseY)
{
int delta = (int)(data.position.y - m_StartPos.y);
//delta = Mathf.Clamp(delta, -MovementRange, MovementRange); //changed
newPos.y = delta;
}
transform.position = Vector3.ClampMagnitude(new Vector3(newPos.x, newPos.y, newPos.z),MovementRange) + m_StartPos;
UpdateVirtualAxes(transform.position);
}
public void OnPointerUp(PointerEventData data)
{
transform.position = m_StartPos;
UpdateVirtualAxes(m_StartPos);
}
public void OnPointerDown(PointerEventData data) { }
void OnDisable()
{
// remove the joysticks from the cross platform input
if (m_UseX)
{
m_HorizontalVirtualAxis.Remove();
}
if (m_UseY)
{
m_VerticalVirtualAxis.Remove();
}
}
}
}
Well you have several things going on there conceptually. The first one is that it’s correct, “GetAxis” is not a function that is contained within your “Joystick” class. I believe it’s part of the Input Unity class. So generally you’d do “Input.GetAxis(“Horizontal”)”. But since you’re trying to do a customized version of the Input, it makes it a little more difficult.
It looks like in your script “m_HorizontalVirtualAxis” and “m_VerticalVirtualAxis” are the variables that actually grab the Input (I could be wrong though). So I’d try this, try replacing that Vector2 with this:
Vector2(joy.m_HorizontalVirtualAxis.GetAxis("Horizontal"), joy.m_VerticalVirtualAxis.GetAxis("Vertical")) * forward;
or try
Vector2(joy.m_HorizontalVirtualAxis("Horizontal"), joy.m_VerticalVirtualAxis("Vertical")) * forward;
You might also have to make sure those variables in your Joystick class are public. Even then though I’m not super sure on that. I don’t have much experience with Inputs, especially Joysticks. So sorry if that doesn’t work.
im getting this error ‘‘It is not possible to invoke an expression of type ‘UnityStandardAssets.CrossPlatformInput.CrossPlatformInputManager.VirtualAxis’.’’
i checked variables in joystick and they are public
Hi never mind i think i will just make buttons instead of joystick because that seems complicated, Thanks for helping !
No problem, glad to help a little. It’s up to you though. I think other people might be able to help you if you still want to go with the joystick route though – it’s your call though.
Good luck.
would the built in GetAxis(“Horizontal”) and GetAxis(“Vertical”) not work for joysticks? I thought it did… Not sure if thats what your looking for, but I presumed it was as easy as that. I could be wrong, still learning
and just be careful with variables. All you have to remember is that you have to declare it a variable by var, then the variable name, then define the variable… e.g
var variableName : String = "whatever string here";
EDIT: Scratch that, I see your wanting to create a virtual joystick for touch devices. Not an actual joystick. 
Hi there, thanks for helping i decided to make buttons instead because joystick also will affect my movement in game [it wont be really accurate] So i was following this video and [as always it never can be easy and simple always some problem] so here is link with exact time where i have problem
https://youtu.be/ qvJCjy8vIrY?t=22m46s
just remove space after be/ because this forum make the link embeded automatically
i did all the same [except i made javascript for touchcontrols] and when he clicks Nofunction he gets his script there and choose function but when i click im not getting my script whats wrong :?
EDIT: Seems like attaching script to all of the buttons not just the empty UI touch controls but each of buttons leftarrow right etc works and shows function