Lock player movement only to x when moving on x, and lock movement only to y when moving on y
Hi,
I'm trying to make an iOS 3D game. I've just got into programming so I'm still not familiar with, well, alot.
I want my player to move only on x axis (up or down) when I swipe my finger across it, and only on y (up or down) when I swipe across y.
So far I've managed to lock movement to x only (least I think so as it works in test mode), but when I try to write the code for y axis movement, visual studio gives an error for ...GetAxis(0, ("Vertical")));
CS1503 Argument 1: cannot convert from 'int' to 'char'8.
Can anyone help please?
Also, player needs to move forward automatically, hence, transform.Translate(Vector3.forward Time.deltaTime speed, Space.World); . - this part works fine.
Thank you for your time in advance.
void Start ()
{
mybody = this.GetComponent < Rigidbody> ();
}
void FixedUpdate()
{
transform.Translate(Vector3.forward * Time.deltaTime * speed, Space.World);
if (CnInputManager.GetAxis("Horizontal") < 0)
{
moveDirection = new Vector2(CnInputManager.GetAxis("Horizontal"), 0);
moveDirection = transform.TransformDirection(-moveDirection);
moveDirection = -moveDirection * speed * moveForce * Time.deltaTime;
mybody.AddForce(moveForce * -moveDirection);
}
else if (CnInputManager.GetAxis("Horizontal") > 0)
{
moveDirection = new Vector2(CnInputManager.GetAxis("Horizontal"), 0);
moveDirection = transform.TransformDirection(-moveDirection);
moveDirection = -moveDirection * speed * moveForce * Time.deltaTime;
mybody.AddForce(moveForce * -moveDirection);
}
else if (CnInputManager.GetAxis("Vertical") < 0)
{
moveDirection = new Vector2(CnInputManager.GetAxis(0, ("Vertical")));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection = moveDirection * speed * moveForce * Time.deltaTime;
mybody.AddForce(moveForce * moveDirection);
}
else if (CnInputManager.GetAxis("Vertical") < 0)
{
moveDirection = new Vector2(CnInputManager.GetAxis(0, ("Vertical")));
moveDirection = transform.TransformDirection(-moveDirection);
moveDirection = -moveDirection * speed * moveForce * Time.deltaTime;
mybody.AddForce(moveForce * -moveDirection);
}
}
}
Answer by dkjunior · Oct 15, 2015 at 04:18 PM
There's a small syntactic mistake in moveDirection assignment in Vertical case, should be:
moveDirection = new Vector2(0, CnInputManager.GetAxis("Vertical"));
thank You it finally works, i spent hours on it and couldn't see it.
Only problem now is, it's very unrensponsive (reacts fine for x movements but only moves on y if i move my finger perfectly straight, . I know i should go with threshold to do it, but i can't figure it out. Could you help with this one too?
Couple more issues:
Your else clauses for vertical case are both "< 0". I assume one of the should be "> 0".
The code within 4 clauses is inconsistent - it is identical in both of the Horizontal clauses, and different in Vertical clauses (one inverses direction, another one - not).
In general, I feel it can all be combined into one block, see below (this is completely untested, but you get the idea):
if (CnInput$$anonymous$$anager.GetAxis("Horizontal") != 0 || CnInput$$anonymous$$anager.GetAxis("Vertical") != 0)
{
moveDirection = new Vector2(CnInput$$anonymous$$anager.GetAxis("Horizontal"), CnInput$$anonymous$$anager.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection = moveDirection * speed * moveForce * Time.deltaTime;
mybody.AddForce(moveForce * -moveDirection);
}
Answer by ArcticFrosty · Oct 16, 2015 at 07:04 AM
@dkjunior 1. i changed < after your original answer. 2. already tryed that way, it moves on x and y at the same time and i need movement locked only to axis being swiped.
Your answer
Follow this Question
Related Questions
Gyroscope Controls for FPS 0 Answers
Got unwanted motion blur on fast moving object (Only iOS) 0 Answers
Move an object by rotation 0 Answers
Rotate Object Axis On Swipe Cam Rotation - Android 1 Answer
How to rotate fluently/smoothly? 1 Answer