Move object with finger on x-axis 3d - unity3d

I have a question: How can I moce a object on the x-axis with my finger on th smartphone? In the internet I can only find solutions for 2d but non for 3d projects.
I have tried this code, but it is really jerky:
void FixedUpdate()
{
if (Input.touchCount == 1) {
touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Moved) {
transform.position = new Vector3(
transform.position.x + touch.deltaPosition.x * speedModifier,
transform.position.y,
transform.position.z
);
}
}
transform.position = new Vector3(
transform.position.x,
transform.position.y,
transform.position.z + (speed * Time.fixedDeltaTime)
);
}
The real question is, how can I make this code, that it works smoothly, because it is really unsmoothly at the moment?

If you want to move the player where you are pressing you can use this code that is simpler than yours:
if (Input.touchCount > 0)
{
touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Moved)
{
transform.position = new Vector3(
transform.position.x,
transform.position.y,
transform.position.z + touch.deltaPosition.x * speed);
}
}
It is very customizable, in this case, for example, I am moving the player along the z-axis , like in your example, but it depends on the position of the finger from left to right.
You can easily change the last part of the code in the way you prefer, for example:
transform.position = new Vector3(
transform.position.x + touch.deltaPosition.x * speed,
transform.position.y + touch.deltaPosition.y * speed,
transform.position.z);
In this way, it will go up and down and left and right, based on the point you press and it could be useful for a game in 2d view. In 3D keep in mind that if you press up you want to move on the z axis most of the time so my first code is working well because I am changing z position on the x touch.

Related

3rd Person RigidBody Movement Script Using CineMachine

I am trying to create a 3rd person movement script using Cinemachine as camera, I followed Brackeys "THIRD PERSON MOVEMENT in Unity" YouTube tutorial. I then Changed the base of it from character controller to rigidbody and the movement works perfectly fine. However my code sets the velocity of the rigidbody's y axis to 0 when I move the player which fights the gravity making the player jitter slowly to the ground when I move. The Character however does drop to the ground when the player stops moving. All I need is for the script to ignore the y axis and simply listen to unity's gravity.
void Update()
{
if (!photonView.isMine)
{
Destroy(GetComponentInChildren<Camera>().gameObject);
Destroy(GetComponentInChildren<Cinemachine.CinemachineFreeLook>().gameObject);
return;
}
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
isGrounded = Physics.CheckSphere(new Vector3(transform.position.x, transform.position.y - 1, transform.position.z), 0.01f, layerMask);
Vector3 inputVector = new Vector3(horizontal, 0f, vertical).normalized;
if (inputVector.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(inputVector.x, inputVector.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
rb.velocity = moveDir.normalized * speed;
}
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(Vector3.up * jumpForce);
}
}
The player jitters because, in your movement section, you set the y velocity to 0, since Vector3.forward returns new Vector3(0, 0, 1) and you only rotate the vector around the y axis. Instead of this, consider doing:
Vector3 moveDir = new Vector3(transform.forward.x, rb.velocity.y, transform.forward.z);
This will preserve the velocity as it was, removing the jittering.
Note: transform.forward automatically gets the forward vector for the player.

Different sensitivity on different mobile devices Unity 3D

I have a script that moves my character along the X, the moment I move my finger. I need the character not to immediately teleport to the finger, but at a certain speed. I have it set by this script.
if (Input.touchCount > 0)
{
touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Moved)
{
Vector3 newPos = new Vector3(transform.position.x + touch.deltaPosition.x * speed, transform.position.y, transform.position.z);
transform.position = Vector3.Lerp(transform.position, newPos, Time.deltaTime * smooth);
}
}
How do I fix this? I need to be able to use speed and smooth to set the speed I need, but that it does not affect the screen resolution.
transform.position = Vector3.MoveTowards(transform.position, newPos, Time.deltaTime * smooth);

Prevent Diagonal movements

I'm developing a 3D tactical RPG game and I would like to know how I could prevent diagonal movements when the NPC moves? I want him to detect where the human player is in the grid and then go after him.
I can take their grid position but it didn't work. The NPC makes diagonal movements and starts to "sink" in the grid.I've tried something like this, target position is a vector 3 and I call the function in Update(), this script is attached to the NPC:
public void MoveNPC()
{
HumanPlayer = GameObject.FindGameObjectWithTag("Player");
ScriptPlayer PlayerPosition = HumanPlayer.GetComponent<ScriptPlayer>();
NPC NPCPosition = this.GetComponent<NPC>();
if ((PlayerPosition.GridPosition.x - NPCPosition.GridPositonNPC.x) + PlayerPosition.GridPosition.y - NPCPosition.GridPositonNPC.y) >= movMax &&
(PlayerPosition.GridPosition.x -NPCPosition.GridPositonNPC.x) + (PlayerPosition.GridPosition.y - NPCPosition.GridPositonNPC.y) <= movMax)
{
if((PlayerPosition.GridPosition.x - NPCPosition.GridPositonNPC.x) - (PlayerPosition.GridPosition.y - NPCPosition.GridPositonNPC.y) >= movMax &&
(PlayerPosition.GridPosition.x - NPCPosition.GridPositonNPC.x) - (PlayerPosition.GridPosition.y - NPCPosition.GridPositonNPC.y) <= movMax){
targetPosition = HumanPlayer.transform.position;
targetPosition.y = 1.4f;
}
}
if(this.transform.position != targetPosition)
{
this.transform.position = Vector3.MoveTowards(transform.position, targetPosition, velocity * Time.deltaTime);
}
}
How could I solve it?

Unity - following finger only works for one direction

I have made some code that should make my player move into the direction of the finger:
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
Vector2 pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 0));
if (pos.x < rb.position.x)
{
movehorizontal = -1;
}
if(pos.x > rb.position.x)
{
movehorizontal = 1;
}
if (pos.y < rb.position.z)
{
movevertical = -1;
}
if(pos.y > rb.position.z)
{
movevertical = 1;
}
}
Vector3 movement = new Vector3(movehorizontal, 0.00f, movevertical)*speed;
It's a 3D game, with a top-view, so my player starts at 0,0,0 and only moves along the x and z axis. My camera is positionated on 0,10,3. The following works on the x axis, so when my finger touches on the right side it goes to the right, if on the left to the left, but no matter where I touch it, it will only move to the front and not to the bottom of my screen.
I tried debugging, but the instructions werent working at the time.
screentoWorldPoint should be stored as a vector3. also since the camera is 10 units away from your plane the last parameter should be 10.
edit, that will only work for a cam pointing straight down. this code should work regardless of the camera angle.
Vector3 pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 1f));
Vector3 pointDelta = pos - Camera.main.transform.position;
float multiplier = -Camera.main.transform.position.y / pointDelta.y;
pos = Camera.main.transform.position + pointDelta * multiplier;
finally these lines should compare the z values to each other
if (pos.z < rb.position.z)
if(pos.z > rb.position.z)
make those changes and let us know if any other problems still exist

Unity goes beyond a value set as limit, when using scrollwheel

This is rather bizarre; and I have no idea how to actually fix it.
I use a set of limit (min and max), to know how much the scrollwheel can zoom in or out a camera. The problem is that the camera get stuck, because I specify that the zoom should happen only if the y position is between min and max.
Unity always overshoot when using the mouse scrollwheel; if I set 1.0f, and scroll; the final result end up being 0.92; and same goes if I set a max of 5.0f, and using the scrollwheel, it overshoot to 5.1.
This result in the camera getting stuck, because now the camera y value is set beyond the limits, so obviously it won't move.
How do you actually avoid that Unity will go beyond the actual limits?
This is what I use
float mouse = Input.GetAxis("Mouse ScrollWheel");
float zoom_speed = 1.0f;
if (transform.position.y <= 3f && transform.position.y >= 1f)
transform.Translate(0, -mouse * zoom_speed, mouse * zoom_speed, Space.World);
So, this code makes your transform travel between minZoomPos and maxZoomPos in world space. The currentZoom is the normalized value that determines how close is transform to minZoomPos and how var from maxZoomPos:
Vector3 minZoomPos = new Vector3(0, 1f, -3f); //transform's position when zoomed in all the way
Vector3 maxZoomPos = new Vector3(0, 3f, -1f); //position when zoomed out
float currentZoom = 0f; //this is normalized value >> 0 means it's zoomed all the way in, 1 means it's on all the way out
float zoom_speed = 1.0f; //if you keep these declarations in Update() function it will give you unnecessary overhead on garbage collection
void Update(){
currentZoom += Input.GetAxis("Mouse ScrollWheel") * zoom_speed;
currentZoom = Mathf.Clamp01(currentZoom);
//get point between minZoomPos and maxZoomPos depending on currentZoom
transform.position = Vector3.Lerp (minZoomPos, maxZoomPos, currentZoom);
}
Found a solution.
Basically if I go below or above the limit; I just set the limit by hand. Not elegant, but with 4 lines of code I get what I need.
I also did change how I handle the zoom; since I don't need to move on both Y and Z axis, I just lower the camera on Y axis, which works better for my scenario
float mouse = Input.GetAxis("Mouse ScrollWheel");
float zoom_speed = 1.0f;
if (transform.position.y <= 3f && transform.position.y >= 1f)
transform.Translate(0, -mouse * zoom_speed, 0);
if (transform.position.y < 1f)
transform.position = new Vector3(transform.position.x, 1f, transform.position.z)
if (transform.position.y > 3f)
transform.position = new Vector3(transform.position.x, 3f, transform.position.z)
With this, the value get tweaked as soon as it is changed, it happens so fast that the camera does not jitter or even move slightly. Pretty sure there are better ways to do so, but this seems to work for now.