Is there a way to see if the Y axis is going up
Im making a mobile game for the first time and making movement. I want to see if there is some code that can see if the Y axis of the mouse position is going up or down. And if it is going up it moves the character up. I also want to do the same for X axis so is this possible?
Answer by mirage3d · Sep 21, 2019 at 07:11 AM
If what you need is to move the character, then you can use this code inside of the Update()
function in a - preferably movement or controller - script attached to your character:
private float speed = 4f;
private Vector3 velocity = Vector3.zero;
private RigidBody rigidBody;
void Update() {
// ...
Vector3 forward = transform.forward * Input.GetAxis("Vertical");
Vector3 right = transform.right * Input.GetAxis("Horizontal");
velocity = (forward + right).normalized * speed;
// do the actual movement in FixedUpdate()
}
void FixedUpdate() {
transform.position += velocity * Time.fixedDeltaTime;
// or you can set the velocity on the RigidBody, and let Physics do the movement:
// rigidbody.velocity = velocity;
}
I don't understand what that code is. I know how to make pc movement. Im looking for mobile movement. I don't want any plungins though.
Sorry, my bad. Didn't understand your question. Saw "mouse" so I immediately thought "PC".
$$anonymous$$aybe you're asking about touches?
// Vector2 result. handling first touch
var movementDelta = Input.GetTouch(0).deltaPosition;
var upPixels = movementDelta.x; // negative if down
var rightPixels = movementDelta.y; // negative if left
more info: Unity Scripting API - Input.GetTouch
its fine. but i don't understand your code. i want to make it so that i can move my hand anywhere on the screen and the character moves in that direction and follows. can you give me any code to help with this and give me some explaining so i know what im doing. reading this i sound rude but im sorry i don't mean it that way.
no problem.
I think for me to give you full code might be an overkill here. I found a video that talks about handling touches in mobile games from Brackeys. He uses a system from the asset store. If it suits you, you could probably use the same. Otherwise you can easily replace the joystick code he uses with the Input.GetTouch[0].position
or Input.GetTouch[0].deltaPosition
like above.
https://www.youtube.com/watch?v=bp2PiFC9sSs
The idea is that you ask the Input
manager for the touch events. These are returned as Touch
structures that contain position
and deltaPosition
values among other things. you would do this in the Update()
method, and then preform the actual move in the FixedUpdate()
method since it makes more sense to move/transform your character during the physics update stage of the run loop. If you do that, then you need a variable to se in Update()
and use that variable to move your object's transform.position
in FixedUpdate()
.
Hope that helps.
Your answer
Follow this Question
Related Questions
Make character move up on Y axis as apposed to move forward in Z space when mobile device is tilted 2 Answers
Way to detect movement on mobile devices 0 Answers
How do I adjust code so gyro translates player to center position when device is being held in hands 1 Answer
the player input is jittery seems like glitching 0 Answers
movement control for IOS 0 Answers