Different sensitivity on different mobile devices Unity 3D - unity3d

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);

Related

Unity Keyboard Movement Easing?

I am trying to replicate the editor's camera for runtime use, and all is working as expected, but I am trying to Ease the movement and can't get it working right. Here is my code for the movement. It works correctly but isn't Eased out like I want.
Vector3 move = Vector3.zero;
if (Input.GetKey(KeyCode.W))
move += Vector3.forward * currentSpeed;
if (Input.GetKey(KeyCode.S))
move -= Vector3.forward * currentSpeed;
if (Input.GetKey(KeyCode.D))
move += Vector3.right * currentSpeed;
if (Input.GetKey(KeyCode.A))
move -= Vector3.right * currentSpeed;
if (Input.GetKey(KeyCode.E))
move += Vector3.up * currentSpeed;
if (Input.GetKey(KeyCode.Q))
move -= Vector3.up * currentSpeed;
transform.Translate(move);
I have tried a destination Vector3, setting that to move, then lerping the position to destination. This does Ease it as expected, but then the directions are all broken. Left moves forward, right moves back, and so on.
I have tried moving to fixedUpdate but still no luck.
Any help would be greatly appreciated!
Try to use CharacterController. It is component from Unity. It is easy to use and smooth.
public CharacterController controler;
public void Start(){
controller = GetComponent<CharacterController>();}
public void Move(){
controller.Move(moveDirection * Time.deltaTime);}
On youtube you can find tutorials from CharacterController.
After much experimentation, easing/smoothing can be achieved by doing the following in an update loop:
float x, y, z;
x = Input.GetAxis("Horizontal") * currentSpeed * Time.deltaTime;
z = Input.GetAxis("Vertical") * currentSpeed * Time.deltaTime;
y = 0;
transform.Translate(x,y,z);

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.

Move object with finger on x-axis 3d

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.

Unity3D SpaceShooter Mouse movement while upside down

I'm making a 3D space game where you fly a ship. its first person from the cockpit.
All my movements work like a charm (W/S = speed up/down, Q/R = rotate left/right) and the mouse moves the ship around.
But if I use my rotate buttons, the mouse stays "stuck". Easiest way to explain it is: when I fly normal, moving the mouse up pans the camera up, and down - pans down.
But if I fly upside down the mouse doesnt seem to notice it and pans the wrong way (inverted). Like it doenst know my rotation. On the side its even weirder. The mouse keeps going up on the initial rotation not on the new one I dont know how to solve it.
What I basicly want to do is also turn the mouse orientation when pressing Q or E
This is my rotate/mouse script:
if (Input.GetKey("q"))
{
orientationPanel.transform.Rotate(-Vector3.left * 100f * Time.deltaTime);
cam.transform.Rotate(Vector3.forward * 100f * Time.deltaTime);
transform.Rotate(Vector3.forward * 100f * Time.deltaTime);
}
if (Input.GetKey("e"))
{
orientationPanel.transform.Rotate(Vector3.left * 100f * Time.deltaTime);
cam.transform.Rotate(-Vector3.forward * 100f * Time.deltaTime);
transform.Rotate(Vector3.forward * 100f * Time.deltaTime);
}
if (Input.GetMouseButtonDown(0) && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Fire();
}
float mouseX = Input.GetAxis("Mouse X");
float mouseY = -Input.GetAxis("Mouse Y");
rotY += mouseX * mouseSensitivity * Time.deltaTime;
rotX += mouseY * mouseSensitivity * Time.deltaTime;
rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);
rotZ = orientationPanel.transform.rotation.z;
Quaternion localRotation = Quaternion.Euler(rotX, rotY, rotZ);
transform.rotation = localRotation;
You need to rotate your ship always in local space. If you move mouse to right - always move nose of the ship to right.
Rotate(axis, angle, Space.Self);
For example
Rotate(Vector3.up, rotY, Space.Self);
Rotate(Vector3.right, rotX, Space.Self);
This will also fix rotation with Q/E, because with
Quaternion localRotation = Quaternion.Euler(rotX, rotY, rotZ);
transform.rotation = localRotation;
you basically override changes made with
transform.Rotate(Vector3.forward * 100f * Time.deltaTime);
transform.Rotate(Vector3.forward * 100f * Time.deltaTime);

How change the Y position of moving forward spaceship in Unity 3D

i have start a project in unity 3d.I want to make a spaceship that moving forward,but when i pressed the ArrowUp then i want to change its y postion to
( currentpos+ 1.5 ) but i want this smoothly.
this is my code
transform.position += transform.forward * Time.deltaTime * 10f;
if (Input.GetKey (KeyCode.UpArrow))
transform.position = new Vector3 (transform.position.x, 5f,
transform.position.z);
through the above code the Y position of object can b changed but it work so fast and i want to make it smooth.
so please help me.
I think the best solution to your problem is to use Mathf.SmoothDamp.
Example:
private float targetY = 0f;
private float verticalVelocity = 0f;
private const float smoothTime = 1f;
private void Update()
{
transform.position += transform.forward * Time.deltaTime * 10f;
if (Input.GetKey(KeyCode.UpArrow))
{
targetY = 5f;
}
float y = Mathf.SmoothDamp(transform.position.y, targetY, ref verticalVelocity, smoothTime);
transform.position = new Vector3 (transform.position.x, y, transform.position.z);
}
This example will smoothly change the y coordinate to 5 over the course of 1 second (you can change the smoothTime constant for a different time).
Based in your own code the easiest way for you to work it out could be something like this
//this sets the X position
transform.position += transform.forward * Time.deltaTime * 10f;
//if the button is pressed then modify Y
if (Input.GetKey (KeyCode.UpArrow))
transform.position += new Vector3 (0, 5f * Time.deltaTime * y_speed,0);
y_speed could be a public float y_speed = 1.0f in your script so you could modify it from the inspector to get the effect you want to achieve.
Hope it helps!
Assuming your spaceship is a rigidbody, you should take a look at Rigidbody.AddForce
https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
By working with forces, you can get a smooth movement in all directions very easily, and tweak it within the Rigidbody's parameters (like mass) without fiddling in the script again. It's part of the Unity physics model.
If you only want to move in y-direction, input a vector like (0,1,0) but you can also input the Transform.forward vector of your spaceship's Gameobject. That way, it will always move the direction it is facing in.