Unity3D - 2D object rotation based on touch moved (diff between touches) - vector

I´m newbie in Unity. I want rotate my 2D object based on user touch moved (moved finger on the screen). I have this code:
void Update ()
{
if (Input.touches.Length > 0) {
t = Input.GetTouch (0);
if (t.phase == TouchPhase.Moved) {
Vector3 movePos = new Vector3 (t.position.x, t.position.y, 0);
var objectPos = Camera.main.WorldToScreenPoint (transform.position);
var dir = movePos - objectPos;
transform.rotation = Quaternion.Euler (new Vector3 (0f, 0f, Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg));
}
}
}
This code rotate the object based on user touch but when I touch screen again in another position and do touch move, it will rotate the whole object to the actual touch and then it will do correct object rotation based on touch move.
And I dont´t want rotate the whole object based on touch position but rotate the object only based on touch move. Do you understand me? Can you help me? How should I rewrite my code?

If I understand you, try to use this code below:
private float turnSpeed = 5f;
private Vector2 movement;
void Update()
{
Vector2 currentPosition = transform.position;
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Moved)
{
Vector2 moveTowards = Camera.main.ScreenToWorldPoint(touch.position);
movement = moveTowards - currentPosition;
movement.Normalize();
}
}
float targetAngle = Mathf.Atan2(movement.y, movement.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 0, targetAngle), turnSpeed * Time.deltaTime);
}
Let me know if is what you want. Also, there is a complete sample here: https://github.com/joaokucera/unity-2d-object-rotation

Look into using deltaPosition instead of position on your touch. That should get you in the right direction.
var movedVector = t.deltaPosition;
Edit:
Here is a possible integration with your existing code. I don't have Unity on this PC so this is entirely untested. The main idea is you are getting a change in the finger position between frames. You then scale that change by move speed, and of course, the change in time between frame renders (delta time).
How the object rotates relative to that information is up to you. I just inserted the logic into your existing code.
float moveSpeed = 2.0f;
void Update ()
{
if (Input.touches.Length > 0) {
t = Input.GetTouch (0);
if (t.phase == TouchPhase.Moved) {
var delta = t.deltaPosition * moveSpeed * Time.deltaTime;
transform.rotation = Quaternion.Euler (new Vector3 (0f, 0f, Mathf.Atan2 (delta .y, delta.x) * Mathf.Rad2Deg));
}
}
}

Related

Unity 3D script player shoot bad direction

I'm using Unity 3d version 4 because I have a MacOS X and I'm making a FPS 3D
I have a script to make the player shoot but this script only shoots up and does not consider where I point the mouse
Below the code in CS
Could someone please correct my script because I have been blocked for 3 days and I have not found any solution on the Internet. Thanks
using UnityEngine;
using System.Collections;
public class ShootDemo : MonoBehaviour {
public Rigidbody projectile;
public float speed = 100;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
Rigidbody instantiatedProjectile = Instantiate(projectile,
transform.position,
transform.rotation)
as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
}
}
}
The way you set the velocity is not right, you should get the mouse position like this:
Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation);
var input = Input.mousePosition;
input.z = 10;
Vector2 touchPos = Camera.main.ScreenToWorldPoint(input);
Vector2 dir = new Vector2(transform.position.x, transform.position.y) - touchPos;
dir.Normalize();
instantiatedProjectile.velocity = dir * speed;
I found this script in JavaSript working but the bullet speed is too slow and then the bullet stays in the scene for 2 seconds instead of making it disappear completely. How can I then add strength to the bullet?
var Effect : Transform;
var TheDamage = 100;
function Update ()
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast (ray, hit, 100))
{
var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(particleClone.gameObject, 2);
hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
}
}
}

Realistic BOAT/SHIP movement and rotation (2d)

I want to move my boat to the position where i clicked BUT with a realistic movement and rotation :
(http://i.imgur.com/Pk8DOYP.gif)
Here is my code (attached to my boat gameobject) :
Basically, when i click somewhere, it move the boat until it reach the point that i clicked in the first place (i simplified my code for you)
using UnityEngine;
using System.Collections;
public class BoatMovement : MonoBehaviour {
private Vector3 targetPosition;
private float speed = 10f;
private bool isMoving;
void Update(){
if (!isMoving && Input.GetMouseButton (0)) {
targetPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
isMoving = true;
}
if (isMoving) {
moveToPosition ();
}
}
void moveToPosition() {
transform.position = Vector3.MoveTowards (transform.position, new Vector3(targetPosition.x, targetPosition.y, 0f), speed * Time.deltaTime);
if (transform.position.x == targetPosition.x && transform.position.y == targetPosition.y) {
isMoving = false;
}
}
}
After some research and tries, i didn't find a way to do what i want.
Thanks for your help
There's two parts to this problem, which should be kept completely separate from each other and at no time does the equation of one interfere with the equation of the other.
The first is the forward movement of the ship, which can be achieved in two ways:
If you are using a rigidbody
void Propel()
{
float speed = 150f;
RigidBody rb = GetComponent<RigidBody>();
Transform transform = GetComponent<Transform>();
rb.AddForce(transform.forward * speed * Time.deltaTime); // <--- I always forget if its better to use transform.forward or Vector3.forward. Try both
}
If no rigidbody
void Propel()
{
float speed = 150f;
Transform transform = GetComponent<Transform>();
transform.Translate(transform.forward * speed * Time.deltaTime, Space.World);
}
Now the second would be the turning of the ship, which also can be achieved in two ways:
With rigidbody
IEnumerator TurnShip(Vector3 endAngle)
{
float threshold = Single.Epsilon;
float turnSpeed = 150f;
RigidBody rb = GetComponent<RigidBody>();
while (Vecotr3.Angle(transform.forward, endAngle) > threshold)
{
rb.AddTorque(transform.up * turnSpeed * Time.deltaTime);
yield return null;
}
}
Without rigidbody
IEnumerator TurnShip(Vector3 endAngle)
{
float threshold = Single.Epsilon;
float turnSpeed = 150f;
float step = turnSpeed * Time.deltaTime;
while (Vector3.Angle(transform.forward, endAngle) > threshold)
{
newDir = Vector3.RotateTowards(transform.forward, endAngle, step);
transform.rotation = Quaternion.LookRotation(newDir);
yield return null;
}
}
Then of course, the IEnumerators are called like so:
StartCoroutine(TurnShip(new Vector3(12f, 1f, 23f));
Couple things to note:
This is pseudo code, i haven't tested any of it so just know that making it work is up to you, i'm only providing you with the correct path to follow.
All the variables in the beginnings of the methods are meant to be global variables, so declare them where you please.
I've used a Transform called target here, just replace it with the Vector from your mouse click.
public Transform target;
private void Update()
{
transform.position += transform.forward * Time.deltaTime;
Vector3 targetDir = target.position - transform.position;
float step = Time.deltaTime / Mathf.PI;
Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0f);
transform.rotation = Quaternion.LookRotation(newDir);
}
The key to this is the RotateTowards function and calculating a new vector to look towards on each frame based on the current position.
If your boats have some kind of rotation speed stat then modify the float called step to suit. If they have a speed stat then modify the transform.position += transform.forward * Time.deltaTime with some multiplier.
I guess you'll want to put some conditions in as to when to stop the movement and rotation, perhaps using a coroutine.
Declare
private Vector3 targetPosition;
private float targetDistance;
Then at the start of your move, do :
targetPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
targetDistance = Vector3.Distance(targetPosition, transform.position);
While moving call this : (in your update loop for example)
turnSpeed = 0.062f * targetDistance;
moveSpeed = 35f * targetDistance;
//Important /!\ : you need to add Linear drag on your rigidbody or it will keep adding
RigidBody.AddForce(transform.up * moveSpeed * Time.deltaTime);
var newRotation = Quaternion.LookRotation (transform.position - targetPosition, Vector3.forward);
newRotation.x = 0f;
newRotation.y = 0f;
transform.rotation = Quaternion.Slerp (transform.rotation, newRotation, Time.deltaTime * turnSpeed);
Finally, add a linear drag value to your rigidbody (1 would be ok).
Thanks to maksymiuk who helped me and took a lot of his time trying to figure out a solution.

Unity Quaternion reset base orientation

I'm having a problem with the rotation of a game object.
I'm able to rotate the object in the direction/angle of another game object.
But after the first rotation the game object i'm rotating based on the delta rotation of the source game object It starts rotating in a different direction because it has been rotated once already I guess.
So is there a way so that after the first rotation I can reset the rotation so It moves again in the angle/direction the other object is rotating?
This is my current code below, i'm not a math expert so I might be doing things wrong ..
So the basic thing I want is that the second game object rotates in the same direction the base game object rotates but I don't want the rotation values to be equal, the rotation values doesn't have to be the same, it only has to rotate in the same angle/direction as the base game object rotates.
TiltAgainstObject is the source game object.
The script is attached to the second game object.
Any help is welcome :).
public class Tilt : MonoBehaviour {
public GameObject TiltAgainstObject;
private float rotX;
private float rotY;
private float rotZ;
private float rotW;
private float deltaX;
private float deltaY;
private float deltaZ;
private float deltaW;
// Use this for initialization
void Start () {
rotX = TiltAgainstObject.transform.rotation.x;
rotY = TiltAgainstObject.transform.rotation.y;
rotZ = TiltAgainstObject.transform.rotation.z;
rotW = TiltAgainstObject.transform.rotation.w;
}
// Update is called once per frame
void Update () {
if(OVRInput.Get(OVRInput.Button.One))
{
deltaX = TiltAgainstObject.transform.rotation.x - rotX;
deltaY = TiltAgainstObject.transform.rotation.y - rotY;
deltaZ = TiltAgainstObject.transform.rotation.z - rotZ;
deltaW = TiltAgainstObject.transform.rotation.w - rotW;
gameObject.transform.localRotation = new Quaternion(gameObject.transform.rotation.x + deltaX,
gameObject.transform.rotation.y + deltaY,
gameObject.transform.rotation.z + deltaZ,
gameObject.transform.rotation.w + deltaW);
}
rotX = TiltAgainstObject.transform.rotation.x;
rotY = TiltAgainstObject.transform.rotation.y;
rotZ = TiltAgainstObject.transform.rotation.z;
rotW = TiltAgainstObject.transform.rotation.w;
}
}
The question seems a bit confusing, bur if you want the second gameobject (where your script is) to rotate with the same direction of the first object you should do something like this:
public class Tilt : MonoBehaviour {
public GameObject TiltAgainstObject;
private float rotX;
private float rotY;
private float rotZ;
private float rotW;
private float deltaX;
private float deltaY;
private float deltaZ;
private float deltaW;
// Use this for initialization
void Start () {
rotX = TiltAgainstObject.transform.rotation.x;
rotY = TiltAgainstObject.transform.rotation.y;
rotZ = TiltAgainstObject.transform.rotation.z;
rotW = TiltAgainstObject.transform.rotation.w;
deltaX = 0;
deltaY = 0;
deltaZ = 0;
deltaW = 0;
}
// Update is called once per frame
void Update () {
rotX = TiltAgainstObject.transform.rotation.x;
rotY = TiltAgainstObject.transform.rotation.y;
rotZ = TiltAgainstObject.transform.rotation.z;
rotW = TiltAgainstObject.transform.rotation.w;
this.gameObject.transform.localRotation = new Quaternion(rotX + deltaX,
rotY + deltaY,
rotZ + deltaZ,
rotW + deltaW);
}
This way the second object will rotate in the same direction of the first object, if you want to change the speed of rotation just modify the deltas.
If you want the rotation to occur only when you press the button.One just pust it inside the if statement instead:
if(OVRInput.Get(OVRInput.Button.One))
{
this.gameObject.transform.localRotation = new Quaternion(rotX + deltaX,
rotY + deltaY,
rotZ + deltaZ,
rotW + deltaW);
}
I think the problem is that you're assigning the world coordinate rotation to your local coordinate localRotation, a typo maybe? Besides that though, I'm not sure why you're editing the components of the Quaternion directly, generally it's better to just add them together and use Unity's functions since dealing with 4 dimensions is often error-prone. You might try something like this:
public class Tilt : MonoBehaviour {
public GameObject TiltAgainstObject;
private Quaternion prevRotation;
private Quaternion currentRotation;
void Start()
{
prevRotation = TiltAgainstObject.transform.rotation;
}
void Update()
{
currentRotation = TiltAgainstObject.transform.rotation;
if (OVRInput.Get(OVRInput.Button.One))
gameObject.transform.rotation *= Quaternion.RotateTowards(prevRotation, currentRotation, 1000f);
prevRotation = currentRotation;
}

Game Maker - How do I create homing effect for a projectile?

I'm trying to make a homing projectile for my bullet hell game and I'd need to be able to calculate the angle between the target and projectile relatively to the projectile's angle (0 degrees would be the direction the projectile is pointing). Right now the angle calculation is absolute done using point_direction, but the problem is when the target is at the 4th sector the projectile starts steering the wrong way. Another issue is that if the projectile does a 180 degree turn while chasing the target (or moves down if fired by enemy) the steering direction will get inverted. I have also tried mp_potential_ functions but their pathfinding is too "agressive".
This is what my current code looks like:
if(instance_exists(obj_fighter1)) {
var target;
target = instance_nearest(x, y, obj_fighter1);
if(target != noone) {
var angle_to_target;
angle_to_target = point_direction(x,y,target.x,target.y);
if(angle_to_target < direction) {
direction -= 2;
}
if(angle_to_target > direction) {
direction += 2;
}
}
}
Hopefully this information is enough and is understandable.
Okay, a common Game Maker question. The routine I use is below. Looking at it, it could do with a bit of refactoring, but it does work.
var wantDir;
var currDir;
var directiondiff;
var maxTurn;
// want - this is your target direction \\
wantDir = argument0;
// max turn - this is the max number of degrees to turn \\
maxTurn = argument1;
// current - this is your current direction \\
currDir = direction;
if (wantDir >= (currDir + 180))
{
currDir += 360;
}
else
{
if (wantDir < (currDir - 180))
{
wantDir += 360;
}
}
directiondiff = wantDir - currDir;
if (directiondiff < -maxTurn)
{
directiondiff = -maxTurn
}
if (directiondiff > maxTurn)
{
directiondiff = maxTurn
}
return directiondiff
So you'd call this, and it'll return you a value that you can add to your missile's angle. So if you call it scr_get_angle, your code might then look like this:
if(instance_exists(obj_fighter1)) {
var target;
target = instance_nearest(x, y, obj_fighter1);
if(target != noone) {
var angle_to_target;
angle_to_target = point_direction(x,y,target.x,target.y);
direction += scr_get_angle(angle_to_target, 2);
}
}

Unity - Making Camera Lock on To Enemy and stay behind player?

I am attempting to create a Camera that moves with the Player but locks onto an enemy when the player clicks the lock on button. The behaviour is almost working as I want it, the camera locks onto the target. And when the player stand in-front of the target it works fine. However as soon as the player runs past the target, the camera behaves strangely. It still looks at the Enemy, however it does not stay behind the player. Here is the code that dictates the behaviour:
if(MouseLock.MouseLocked && !lockedOn){ // MOUSE CONTROL:
Data.Azimuth += Input.GetAxis("Mouse X") * OrbitSpeed.x;
Data.Zenith += Input.GetAxis("Mouse Y") * OrbitSpeed.y;
} else if(lockedOn) { // LOCKON BEHAVIOUR:
FindClosestEnemy();
}
if (Target != null) {
lookAt += Target.transform.position;
base.Update ();
gameObject.transform.position += lookAt;
if(!lockedOn){
gameObject.transform.LookAt (lookAt);
} else if(enemyTarget != null) {
Vector3 pos1 = Target.transform.position ;
Vector3 pos2 = enemyTarget.transform.position ;
Vector3 dir = (pos2 - pos1).normalized ;
Vector3 perpDir = Vector3.Cross(dir, Vector3.right) ;
Vector3 midPoint = (pos1 + pos2) / 2f;
gameObject.transform.LookAt (midPoint);
}
}
And the Code for Finding the nearest Enemy:
void FindClosestEnemy () {
int numEnemies = 0;
var hitColliders = Physics.OverlapSphere(transform.position, lockOnRange);
foreach (var hit in hitColliders) {
if (!hit || hit.gameObject == this.gameObject || hit.gameObject.tag == this.gameObject.tag){
continue;
}
if(hit.tag != "Enemy") // IF NOT AN ENEMY: DONT LOCK ON
continue;
var relativePoint = Camera.main.transform.InverseTransformPoint(hit.transform.position);
if(relativePoint.z < 0){
continue;
}
numEnemies += 1;
if(enemyTarget == null){
print ("TARGET FOUND");
enemyTarget = hit;
}
}
if(numEnemies < 1){
lockedOn = false;
enemyTarget = null;
}
}
As I said, teh behaviour almost works as expected, however I need the camera to stay behind the player whilst locked on and it must face the enemy/midPoint between the enemy and player. How can this be done? Thank you for your time.
To clarify your intent: you want to lock the position relative to the target (player), whilst setting the camera rotation to look at either the target or a secondary target (enemy)? And your current code performs the rotation correctly but the positioning is buggy?
The easiest way to fix the camera relative to another object is to parent it in the scene. In your case you could add the camera as a child under the Player game object.
If you would rather not do this then look at your positioning code again:
lookAt += Target.transform.position;
base.Update ();
gameObject.transform.position += lookAt;
I don't know where lookAt comes from originally but to me this looks all wrong. Something called lookAt should have nothing to do with position and I doubt you want to += anything in the positioning code given that you want a fixed relative position. Try this instead:
public float followDistance; // class instance variable = distance back from target
public float followHeight; // class instance variable = camera height
...
if (Target != null) {
Vector3 newPos = target.position + (-Target.transform.forward * followDistance);
newPos.y += followHeight;
transform.position = newPos;
}
This should fix the positioning. Set the followDistance and followHeight to whatever you desire. Assuming your rotation code works this should fix the problem.

Resources