- Home /
Question by
JohnArthur · Jan 30, 2015 at 12:19 PM ·
c#rotationgameobjecttouch controlsswipe
Change the rotation of Z through swipe
I have this code:
using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour {
public Vector3 startPos, endPos, stayPos, dir;
public GameObject selectedProjectile;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 10))
{
selectedProjectile = hit.collider.gameObject;
}
startPos = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0))
{
Vector3 mousePos3D = Input.mousePosition;
mousePos3D.z = 10;
Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos3D);
worldPos.z = 0;
Debug.Log(worldPos);
Vector3 dir = (worldPos - selectedProjectile.transform.position).normalized;
dir.z = 0;
selectedProjectile.GetComponent<BasicProjectile>().direction = dir;
selectedProjectile.transform.rotation = Quaternion.Slerp(selectedProjectile.transform.rotation, Quaternion.LookRotation(selectedProjectile.transform.position - worldPos), 10 * Time.deltaTime);
selectedProjectile = null;
//endPos = Input.mousePosition;
//dir = (endPos - startPos).normalized;
//selectedProjectile.transform.LookAt(selectedProjectile.transform.position + dir);
//selectedProjectile.transform.rotation.z = endPos.z;
//Quaternion targetQ = Quaternion.Euler(endPos);
//Vector3 relativePos = endPos - transform.position;
//Quaternion rotation = Quaternion.LookRotation(relativePos);
//transform.rotation.z = Quaternion.RotateTowards(transform.rotation, endPos.z, 0);
}
if (Input.GetMouseButton(0))
{
//Quaternion targetQ = Quaternion.Euler(stayPos);
//stayPos = Input.mousePosition;
//selectedProjectile.transform.rotation = targetQ;
}
}
}
The selected projectile, which is a cube, is moving with:
this.gameObject.transform.Translate(direction * speed * Time.deltaTime);
I only want to change the Z axis of the selected projectile, but, the whole rotation changes.
Comment