Drag with speed in Unity game not equal depending on resolution - c#

I'm doing a game in Unity, it is a space shooter and I had made a script for moving my spaceship. This game is developed for Android devices, and I'm moving the ship with the Touch. It is something like dragging the ship, to the position you want. Also one must note, that I'm not considering if you touch in the center of the ship in order to drag it.
However I'm having problems, depending on the device where I'm working, the ship is not following correctly the drag, for instance if I'm on a tablet, if I move with the finger, let's say 3 inches, my ship is only moving one. However, on my mobile device it is working fine.
What I'm doing wrong? I attach you the code for the movement:
void MovePlayer (Vector3 movement)
{
GetComponent<Rigidbody> ().velocity = movement;
GetComponent<Rigidbody> ().position = new Vector3
(Mathf.Clamp (GetComponent<Rigidbody> ().position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (GetComponent<Rigidbody> ().position.z, boundary.zMin, boundary.zMax));
GetComponent<Rigidbody> ().rotation = Quaternion.Euler
(0.0f,
0.0f,
GetComponent<Rigidbody> ().velocity.x * -tilt);
}
void FixedUpdate ()
{
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) {
// Get movement of the finger since last frame
Vector2 touchDeltaPosition = Input.GetTouch (0).deltaPosition;
float moveHorizontal = touchDeltaPosition.x;
float moveVertical = touchDeltaPosition.y;
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical) * speedMouse;
MovePlayer (movement);
}
}
Lot of thanks in advance and best regards

Touch position and deltaPosition are in pixels, so you can't assume that results will be consistent across varying resolutions.
There are a few ways you can mitigate that, depending on your design needs...
You could express the touch deltaPosition as a fraction of the screen resolution:
float screenSize = Mathf.Max(Screen.width, Screen.height);
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
Vector2 touchDeltaPercentage = touchDeltaPosition / screenSize;
//touchDeltaPercentage values will now range from 0 to 1
//you can use that as you see fit
You could use the main camera to transform screen coordinates into world positions, then check the difference between those:
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
Vector3 screenCenter = new Vector3(Screen.width * 0.5f, Screen.height * 0.5f, 1f);
Vector3 screenTouch = screenCenter + new Vector3(touchDeltaPosition.x, touchDeltaPosition.y, 0f);
Vector3 worldCenterPosition = Camera.main.ScreenToWorldPoint(screenCenter);
Vector3 worldTouchPosition = Camera.main.ScreenToWorldPoint(screenTouch);
Vector3 worldDeltaPosition = worldTouchPosition - worldCenterPosition;
//worldDeltaPosition now expresses the touchDelta in world-space units
//you can use that as you see fit
The point, with either method, is that you want some units that aren't tied to screen resolution.
Similar to the above, you could also use the main camera to raycast against terrain, or a large plane, or some other collider(s) in the scene.

The problem here is you are using a speedMouse variable. Speed should have absolutely nothing to do with this, and would be the exact reason for your problem. You should be moving objects based solely on position of touch/mouse. not with speed. because speed is the only thing that will differ your output from cross resolutions.
Think about it this way, if your speed is 10 pixels/second you will move a lot slower on a denser screen, however if you are only updating position based on location on the screen, speeds will never differ.

Related

Trying to make the player rotate perpendicular to the model's meshes. Unity

I'm currently working on a school game project where i need to have the player climb walls and pick up stuff. I am currently stuck in figuring out how to make the player rotate perpendicular to where the model's faces are while he/she moves towards a corner of a wall as shown below.
Something like this:
I searched for some methods to achieve this but i haven't found anything concrete so far.
Edit: I tried Reasurria's method and it kinda works.
if (Physics.Raycast (transform.position, -transform.up, out hit) && hit.collider.GetComponent<WallModifier> ()) {
transform.rotation = Quaternion.LookRotation (hit.normal, Vector3.right);
//Physics.gravity = hit.normal * -10.0f;
}
My player does indeed rotate correctly pependicular to the slope/wall but then my camera code completly breaks when trying to actually climb the wall. This shows how it looks like in first person and this shows how it looks like in the scene view.
While very funny, it is not obviously desired to have such effect.
Here is the camera code:
void Update () {
if (Input.GetKeyDown (KeyCode.Escape) || toggle && Input.GetMouseButtonDown (0)) {
toggle = !toggle;
}
if (!toggle && Application.isFocused) {
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
var md = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y"));
md = Vector2.Scale (md, new Vector2 (sensitivity * smoothing, sensitivity * smoothing));
smoothV.x = Mathf.Lerp (smoothV.x, md.x, 1f / smoothing);
smoothV.y = Mathf.Lerp (smoothV.y, md.y, 1f / smoothing);
mouseLook += smoothV;
mouseLook.y = Mathf.Clamp (mouseLook.y, minClamp, maxClamp);
transform.localRotation = Quaternion.AngleAxis (-mouseLook.y, (Vector3.right + transform.right).normalized);
character.transform.localRotation = Quaternion.AngleAxis (mouseLook.x, character.transform.up.normalized);
if (rotateModel && antModel)
antModel.transform.localRotation = Quaternion.AngleAxis (-mouseLook.y, (Vector3.right + transform.right).normalized) * Quaternion.Euler (0, 90, 0);
} else {
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
Application.runInBackground = false;
}
}
This code was taken by this person.
Do you mean perpendicular? In other words, rotate along x axis with -90 degree.
transform.Rotate(new Vector3(-90, 0, 0));
If I understand correctly, you may want to use Raycast.normal. So you would raycast from your player position with a direction equal to your player's local Vector3.down (player.transform.up * -1.0f).
You can use Quaternion.LookAt(surfaceNormal) to align your players 'up' vector with the normal of the surface under the player. This may involve some 90 degree offset to make the two 'up' vectors match.
Depending on your goal you may also want to set Physics.gravity to surfaceNormal * -10.0f or implement some custom gravity solution if the new surface will function as ground.
You can add empty GameObject with attached collider, than you can use OnTriggerEnter to rotate your Player. Add flag and on every odd enter rotate in one side and on every even enter rotate to opposite side as shingo said
Example

Unity 3D - rotating gameobject without rotating axis

Hope my title summarises my problem. I have a rocket on a 2d game that only moves horizontally across the screen. I want it to rotate towards the players finger (the direction of movement), but cannot find a way to rotate the object without rotating the whole axis it moves on. I simply need it to seem like it has turned, but it should keep moving along the x. How can I go about this?
void Start () {
//scoreT = GetComponent<TextMeshProUGUI> ();
gameSpeed = 1;
score = 0;
Rigidbody2D rb2d = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update () {
float MoveHorizontal = Input.GetAxis ("Horizontal");
Vector2 Movement = new Vector2 (MoveHorizontal, 0.0f);
rb2d.rotation = Quaternion.Euler (0.0f, 0, 0f, rb2d.velocity.x * -tilt);
transform.Translate (MoveHorizontal * speed, 0, 0);
One thing you can do is to modify rigidbody.rotation of your rocket rocket to make it tilt, when it moves, to one direction or to another. For example:
float tilt - 0.3f;
//In case you prefer the rotation in another axis you just need to modify the position of the rigidbody.velocity.x * -tilt
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
Since you didn't add any code I am not sure how you are moving your rocket, so I will post a generic code you will need to adapt depending on your own project:
public class PlayerController : MonoBehaviour
{
public float speed;
//The tild factor
public float tilt;
//The limit within the spaceship can move
public Boundary boundary;
void FixedUpdate ()
{
//You will need to capture the screen touchs of the user for the inputs
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
//Applying the movement to the GameObject
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;
//To ensure the GameObject doesnt move outside of the game boundary
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);
//Here is where you apply the rotation
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}
}
As an aside, you are doing a space 2D game you may be interested in checking this tutorial:
https://unity3d.com/learn/tutorials/s/space-shooter-tutorial
What you want is to move your object in global/world space. As it seems the movement of your rocket is currently happening within local space. So when you rotate the rocket it's local coordinates are rotated as well. The world space coordinates are fixed and will never rotate when you change your rocket.
Here is another explanation at that.
You can also have a look at Transform.localPosition and Transform.position and see how your rocket behaves when using one or the other.
You could make the sprite/renderer a child of the GameObject that is your rocket.
Then you can freely rotate the sprite/renderer around without changing the rotation you move the parent GameObject.
This is a hacky solution but it achieves the desired result.
You must move your object relative to the world and rotate locally.
So use Transform.position to move your object and Transform.LocalRotation to rotate it.
Or you can put the part that must rotate as a children of the object that translate, and rotate the children.

Player rotation and camera rotation

i want the player to look into the direction, the camera is looking.
The camera follows the player (3rd person game style).
I've tried it with
transform.localRotation = new Quaternion(transform.localRotation.x,
cameraMain.transform.localRotation.y,
transform.localRotation.z,
transform.localRotation.w);
but it doesn't work.
Sometimes the player starts rotating the other direction.
the following code will make the object (specified in the parameter) face in the direction of where the main camera is looking:
public void lookInDirectionOfCamera(Transform object) {
RayCastHit hit;
if (Physics.raycast(cameraMain.transform.position, cameraMain.transform.forward, out hit)) {
object.forward = hit.point - object.position;
}else { //if the raycast didnt hit anything, make the object face 100 units in front of the camera
Vector3 point = Camera.main.transform.position + Camera.main.transform.forward * 100;
object.forward = point - object.position;
}
}
This will make the player face the point that is forward to the camera. If you just want them to have the same rotation in the y-axis don't use Quaternions!
Instead, you can just do it with Euler angles:
transform.eulerAngles = new Vector3(transform.eulerAngles.x,
cameraMain.transform.eulerAngles.y,
transform.eulerAngles.y);
The reason not to use transform.localRotation is because that is a Quaternion. The y component in a Quaternion is not the same as the y-axis in a Euler angle (what you are used to seeing), Quaternions are very confusing so you should almost never set individual values in them. If you want to edit them only use the built-in methods.
Get the direction the camera is looking with cameraMain.transform.forward, make a copy with a y value of zero, then use Quaternion.SetLookRotation to look in that direction with the global up direction.:
Vector3 cameraDirection = cameraMain.transform.forward;
Vector3 characterLookDirection = new Vector3(cameraDirection.x,
0f,
cameraDirection.z);
Quaternion newRotation = new Quaternion();
newRotation.SetLookRotation(characterLookDirection, Vector3.up);
transform.rotation = newRotation;

Translation on a X,Z plane using variable axis relative to camera orientation

I have a board on the world X,Z axis.
I have a camera that can rotate to look downward or upward and rotate left to right using the world Y axis.
Here's the camera rotation code:
float angle = (mouseX_Current - mouseX_ActionStart) * camRotationSpeedMod;
Camera.main.transform.Rotate(0.0f, angle, 0.0f, Space.World);
angle = (mouseY_Current - mouseY_ActionStart) * camRotationSpeedMod;
Camera.main.transform.Rotate(angle, 0.0f, 0.0f, Space.Self);
Using mouse button down and a mouse position input(drag&Drop) I translate the camera in the world. It receive a 2D vector that contain the X and Y value of the mouse(drag&drop) operation. Here's the code of the method I use :
private void CameraOnPlaneTranslation(Vector2 myVector)
{
float cameraYPos = Camera.main.transform.position.y;
Camera.main.transform.Translate(new Vector3(myVector[0], 0.0f, myVector[1]));
Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, cameraYPos, Camera.main.transform.position.z);
}
Problem : When my camera is not oriented directly toward the Z Axis(0.0f, Z, 0.0f) the translation vector I compute is incorrect. I need to change the Y value of the vector so the camera don't move on the world Y axis.
Thanks
I am not familiar with Unity, but supposing it is similar to other 3D systems on these points, I will attempt an answer and hopefully not embarrass myself.
It looks like you /translate/ the camera position, then immediately /set/ the camera position, thus overwriting the work done by translate. If this is the case, you should try setting camera.main.transform.position once (in the line after you create the Camera). Then only apply myVector by the Translate call (as you have it). Thus you should eliminate the third line of the method Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, cameraYPos, Camera.main.transform.position.z);
After trying this, if it still does not do what you expect, see if it needs you to Update the view or Flush or ApplyTransform . . . anything that will make it understand you are done altering the transform and you are ready for it to process again with the updated transform.
I've solved this by adding another GameObject that is affected only by the camera right/left rotations(world up vector) and the camera translations.
In the Awake method
cameraOrientation = new GameObject("cameraOrientation");
cameraOrientation.transform.position = Camera.main.transform.position;
cameraOrientation.transform.localEulerAngles = new Vector3(0.0f, Camera.main.transform.localEulerAngles.y, 0.0f);
In the rotation method
float angle = (mouseX_Current - mouseX_ActionStart) * camRotationSpeedMod;
Camera.main.transform.Rotate(0.0f, angle, 0.0f, Space.World);
cameraOrientation.transform.Rotate(0.0f, angle, 0.0f, Space.World);
angle = (mouseY_Current - mouseY_ActionStart) * camRotationSpeedMod;
Camera.main.transform.Rotate(angle, 0.0f, 0.0f, Space.Self);
And finally the translation method
cameraOrientation.transform.Translate(new Vector3(myVector[0], 0.0f, myVector[1]));
Camera.main.transform.position = cameraOrientation.transform.position;

Unity C# Quaternion Rotation Correcting orientation

IHi I've been working on a camera rotation method.
I'm trying to rotate a camera using the mouse inputs.
I need the horizontal rotation to be parallel to a x,y plane(my board) and the vertical rotation to be limited to certain angle(something like 40 degree to -40).
I use quaternions to rotate the camera.
Here my code so far
float angle = (mouseX_Current - mouseX_ActionStart) * 0.25f;
Quaternion horizontalRotationQuat = Quaternion.AngleAxis(angle, Vector3.up);
angle = (mouseY_Current - mouseY_ActionStart) * 0.25f;
Quaternion verticalRotationQuat = Quaternion.AngleAxis(angle, Vector3.right);
Camera.main.transform.rotation *= (horizontalRotationQuat * verticalRotationQuat);
My problem is that by adding those mouse related rotations to my current camera orientation quaternion the camera is no more parallel to the plane(x,y) which it s looking at.
I' ve been trying to create a correction quaternion to add to the camera quaternion after adding those mouse related rotations but I can't seem to find the right one.
Quaternion currentOrientationQuat = Camera.main.transform.rotation;
Quaternion corretionQuat = new Quaternion(0.0f, 0.0f, - currentOrientationQuat.z, currentOrientationQuat.w);
Camera.main.transform.rotation *= corretionQuat;
If someone could help me out on this I would be very grateful.
Thanks.
Sorry for the English, not my primary language.
Solved this using another method
float angle = (mouseX_Current - mouseX_ActionStart) * 0.25f;
Camera.main.transform.Rotate(0.0f, 0.0f, angle, Space.World);
angle = (mouseY_Current - mouseY_ActionStart) * 0.25f;
Camera.main.transform.Rotate(angle, 0.0f, 0.0f, Space.Self);
Feeling dumb after seeing how easy it was to do it.
Did'nt even needed the quaternions for this one.

Categories