- Home /
Move object Horizontally : 2D
I am trying to move my player left and right on button click. But unfortunately it does not work.
Script i have written is here:
if (Input.GetMouseButtonDown(0))
{
var hit : RaycastHit;
var ray : Ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray,hit))
{
if(hit.collider.gameObject.GetComponent("ButtonId"))
{
buttonPressed = hit.collider.gameObject.GetComponent("ButtonId");
if(buttonPressed.id == "RightMove")
{
amttomove = Input.GetAxisRaw("Horizontal") * 2 * Time.deltaTime ;
transform.Translate(Vector3.right * amttomove);
}
}
}
}
But this code is not moving my player..
when i write below code out of Input.GetMouseButtonDown(0), it works..
amttomove = Input.GetAxisRaw("Horizontal") * 2 * Time.deltaTime ;
transform.Translate(Vector3.right * amttomove);
Where is the problem>?? pleaze any one help me to solve my problem..
Answer by Shankar · Apr 09, 2013 at 10:43 AM
var upRange : float = 1.0;
var downRange : float = 1.0;
var waveOffset: float = 0.0;
internal var yStartPos: float;
internal var upPos : float;
internal var downPos : float;
var speed : float = 0.2;
===========================
yStartPos = transform.position.y;
===========================
upPos = yStartPos +upRange;
downPos = yStartPos - downRange;
var weight : float = Mathf.Cos((Time.time + waveOffset) * speed * 2 * Mathf.PI) * 0.5 + 0.5;
transform.position.y = upPos * weight + downPos * (1-weight);
===========================
var leftRange : float =0.0;
var rightRange : float =0.0;
var offset = 0.0;
var waveOffset: float = 0.0;
internal var xStartPos : float;
internal var leftPos : float;
internal var rightPos : float;
var speed : float = 0.2;
============================
xStartPos = transform.position.x;
============================
function FixedUpdate () {
leftPos = xStartPos - leftRange;
rightPos = xStartPos + rightRange;
var weight = Mathf.Cos((Time.time + waveOffset) * speed * 2 * Mathf.PI) * 0.5 + 0.5;
transform.position.x = leftPos * (1-weight) + rightPos * weight - offset;
}
Hey this for vertical direction change. please convert this into horizontal. then u get get that. or else just change axis in transform.
this is from
http://www.lynda.com/Unity-3D-tutorials/Unity-3D-35-Essential-Training/96677-2.html
write in proper manner..i dont understand your code..
see this. this is only for vertical only. u can get horizontal in the above code. sorry for my answering format.
Can u give me some more clarification on this question. i understand that u have button in the scene. if u click that button the object goes to right. that is ok. what about left? can i take two buttons and then, can i write the code? if yes tell me i will try to write and post.
Answer by IronFurball · Apr 09, 2013 at 11:27 AM
The problem is that you're using GetMouseButtonDown. GetMouseButtonDown only triggers on the moment that you're mouse clicks, which means that your player only moves for 1 frame, and then stops until you click again. If you start spam clicking the button you will probably notice that he slowly starts to move.
if you want him to move until you release the button, you will have to use GetMouseButton, so without the "Down" at the end.
Is your raycast hitting ? You can check by writing this inside your raycaast check:
Debug.Log("The raycast hit something! yay :D");
If that doesn't show up in the console then its not hitting.
i have tested.. my input.GetButtonDown called only once.. So what do i do?
where did you put the Debug.Log, in the Get$$anonymous$$ouseButtonDown ? $$anonymous$$ake sure to put it in the
if (Physics.Raycast(ray,hit))
{}
so that you're absolutely sure your raycast is hitting something.
yes.. if(buttonPressed.id == "Right$$anonymous$$ove") { }
I have kept in this condition.. bt still not working
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
A Question about sprites 3 Answers
2D Mouse Aiming 2 Answers
Reusing same 2D Environment in different levels 0 Answers
Need help with screen resolution and character size 1 Answer