Rotate a GameObject with Touch - unity3d

I have this code below and works in Keyboard, but does not works with touch.
Quaternion rot = transform.rotation; float z = rot.eulerAngles.z;
z-= Input.GetAxis("Horizontal") * rotSpeed * Time.deltaTime;
rot = Quaternion.Euler( 0, 0, z );
transform.rotation = rot;
I need that code above works on touch, how to do this?

Maybe you should read the Api for input of Unity Scripting http://docs.unity3d.com/ScriptReference/Input.html
you could use the getTouch method to rotate the object
here the documentation
http://docs.unity3d.com/ScriptReference/Input.GetTouch.html
you can use the delta for draging or the Position for rotating the Object
so it would be like this not sure its working it should work with dragging
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
z-=touchDeltaPosition.x * rotSpeed * Time.deltaTime;
rot = Quaternion.Euler( 0, 0, z );
}

It should be nearly the same as above but instead of the x axis which is the horizontal one we are using the y axis for the vertical one this code is also with dragging
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
Vector3 pos = transform.position;
Vector3 velocity = new Vector3(0, touchDeltaPoition.y * maxSpeed * Time.deltaTime, 0);
pos += rot * velocity;
}

Related

How can I match my animation with transform in Unity3d?

I have implemented a player movement and jump in my game. I also attached a run and jump animation to animator. However, the jump animation does not perfectly match with the player vertical movement. How can I match them perfectly?
Thanks in advance,
void Control()
{
if (charController.isGrounded)
{
float h = Joystick.GetHorizontalAxis("MyJoystick");
float v = Joystick.GetVerticalAxis("MyJoystick");
moveDirection = new Vector3(h, 0.0f, v);
moveDirection *= (speed * sprint);
anim.SetFloat("WalkSpeed", moveDirection.magnitude);
if (moveDirection.magnitude > 0.5)
{
anim.SetFloat("WalkSpeed", moveDirection.magnitude / speed);
anim.SetFloat("Walk", 1.0f);
transform.forward = moveDirection;
}
else if (moveDirection.magnitude > 0 && moveDirection.magnitude < 0.5)
{
anim.SetFloat("WalkSpeed", moveDirection.magnitude / (speed * 0.5f));
anim.SetFloat("Walk", 0.5f);
transform.forward = moveDirection;
}
else if (moveDirection.magnitude == 0)
{
anim.SetFloat("Walk", 0f);
}
if (Input.GetKeyDown("space"))
{
moveDirection.y = jumpSpeed;
anim.SetTrigger("Jump");
}
}
moveDirection.y -= gravity * Time.deltaTime;
charController.Move(moveDirection * Time.deltaTime);
}
There is an option in the Animator component to do what you want.
Here is a link that describes it in more details.

Lerp to target position, but only within a radius

I am making a 3D game but my player can only move on the X and Y axis. I have a player with an attached camera following my mouse, but I only want it to follow up to a max radius distance from Vector3.zero, even if my mouse is beyond those bounds.
I have tried repositioning the player to the max distance on radius every frame it tries to follow the mouse outside its bounds, but this causes camera jitters even in LateUpdate.
void LateUpdate()
{
if (Input.GetMouseButtonDown(0)) {
firstTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
playerPos = transform.position;
}
if (Input.GetMouseButton(0)) {
Ray currentTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
Vector2 direction = currentTouchPos.origin - firstTouchPos.origin;
float distance = Vector3.Distance(transform.position, Vector3.zero);
if (distance >= radius) {
targetPosition = direction.normalized * radius;
} else {
targetPosition = playerPos + direction * touchSensitivity;
}
}
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * followSpeed);
}
I'm thinking there must be a way to clamp the positioning of the player to a radius so that I don't have to "leash" him back by repositioning him through code every frame.
You should try using Vector3.ClampMagnitude().
The solution was to use Clamp Magnitude.
void LateUpdate()
{
if (Input.GetMouseButtonDown(0)) {
firstTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
playerPos = transform.position;
}
if (Input.GetMouseButton(0)) {
// targetPosition will follow finger movements
Ray currentTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
Vector2 direction = currentTouchPos.origin - firstTouchPos.origin;
targetPosition = playerPos + direction * touchSensitivity;
}
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * followSpeed);
transform.position = Vector3.ClampMagnitude(transform.position, radius);
}

Moving player in Subway Surf like game using left/right swipe

I am developing endless runner game like subway surfer in Unity.
I want to move my player, smoothly on swipe left or right.
How can do it?
Here is my code:
using UnityEngine;
using System.Collections;
public class SwipeScript3 : MonoBehaviour {
private Touch initialTouch = new Touch();
private float distance = 0;
private bool hasSwiped = false;
//Quaternion targetx = Quaternion.Euler(0, -3f, 0);
//Quaternion targety = Quaternion.Euler(0, 3f, 0);
void FixedUpdate()
{
foreach(Touch t in Input.touches)
{
if (t.phase == TouchPhase.Began)
{
initialTouch = t;
}
else if (t.phase == TouchPhase.Moved && !hasSwiped)
{
float deltaX = initialTouch.position.x - t.position.x;
float deltaY = initialTouch.position.y - t.position.y;
distance = Mathf.Sqrt((deltaX * deltaX) + (deltaY * deltaY));
bool swipedSideways = Mathf.Abs(deltaX) > Mathf.Abs(deltaY);
if (distance > 50f)
{
if (swipedSideways && deltaX > 0) //swiped left
{
//transform.rotation = Quaternion.Slerp (transform.rotation, targetx, Time.deltaTime * 0.8f);
this.transform.Rotate(new Vector3(0, -3f, 0)*Time.deltaTime);
//transform.position = Vector3.Lerp (transform.position,new Vector3(transform.position.x+5f,transform.position.y,transform.position.z),Time.deltaTime*2f );
transform.position = Vector3.Lerp (transform.position, new Vector3 (transform.position.x - 5f, transform.position.y, transform.position.z), Time.deltaTime * 5f);
}
else if (swipedSideways && deltaX <= 0) //swiped right
{
//transform.rotation = Quaternion.Slerp (transform.rotation, targety, Time.deltaTime * 0.8f);
this.transform.Rotate(new Vector3(0, 3f, 0)*Time.deltaTime);
//transform.position = Vector3.Lerp (transform.position, new Vector3 (transform.position.x - 5f, transform.position.y, transform.position.z), Time.deltaTime * f);
transform.position = Vector3.Lerp (transform.position,new Vector3(transform.position.x+5f,transform.position.y,transform.position.z),Time.deltaTime*5f );
}
else if (!swipedSideways && deltaY > 0) //swiped down
{
//this.transform.Rotate(new Vector3(0, 2f, 0));
transform.position = Vector3.Lerp (transform.position,new Vector3(transform.position.x,transform.position.y,transform.position.z-5f),Time.deltaTime*2f );
}
else if (!swipedSideways && deltaY <= 0) //swiped up
{
this.GetComponent<Rigidbody>().velocity = new Vector3(this.GetComponent<Rigidbody>().velocity.x, 0, this.GetComponent<Rigidbody>().velocity.z);
this.GetComponent<Rigidbody>().AddForce(new Vector3(0, 400f, 0));
Debug.Log ("Swiped Up");
}
hasSwiped = true;
}
}
else if (t.phase == TouchPhase.Ended)
{
initialTouch = new Touch();
hasSwiped = false;
}
}
}
}
You could use Vector3.Slerp(Vector3 StartPosition, Vector3 DestinationPosition, float Number)
Number is between 0 and 1 and it indicates where will be the position of your object between StartPosition and DestinationPosition.
Lets say Number = 0.0f;: your object will be at StartPosition.
If Number = 0.5f;: your object will be between StartPosition and DestinationPosition.
You need to increase the Number value from 0 to 1 when swipe action is performed.
The faster you increase the "Number" value, the faster your object will move towards Destination.
You should set you StartPosition once when the swipe action begins, not give your transform.position repeatedly in your Vector3.Slerp() function.
You can find an example here in Unity Docs.
Hope this helps! Cheers!

Unity 5: How to rotate an object in 3d space relative to camera axes

I'm trying to write a very simple 3d model viewer that allows the user to click and drag on the x and y axes to rotate an object. The problem I am facing with my included code sample is that, when I rotate something, say, about the y axis, and then try to rotate about the x axis, I find that the object is rotated about the object's x axis instead of the x-axis from the perspective of the camera.
I'm effectively trying to simulate rotating something along the z-axis, albeit through two motions.
public Transform obj;
private Vector3 screenPoint;
private Vector3 offset;
//public float minX = 270.0f;
//public float maxX = 360.0f;
//public float minY = -90.0f;
//public float maxY = 90.0f;
public float sensX = 100.0f;
public float sensY = 100.0f;
float rotationY = 0.0f;
float rotationX = 0.0f;
float posX = 0.0f;
float posY = 0.0f;
void Update() {
if (Input.GetMouseButton(0)) {
rotationX += Input.GetAxis("Mouse X") * sensX * Time.deltaTime;
//rotationX = Mathf.Clamp(rotationX, minX, maxX);
rotationY += Input.GetAxis("Mouse Y") * sensY * Time.deltaTime;
//rotationY = Mathf.Clamp(rotationY, minY, maxY);
Quaternion q = Quaternion.Euler(rotationY, -rotationX, 0);
transform.rotation = q;
}
if (Input.GetMouseButton(1)) {
posX += Input.GetAxis("Mouse X") * 25.0f * Time.deltaTime;
posY += Input.GetAxis("Mouse Y") * 25.0f * Time.deltaTime;
transform.position = new Vector3(posX, posY, 0);
}
}
If you are looking to rotate around the z-axis, you could try the transform.RotateAround function. This will allow you to specify a point (as a Vector3), a rotation axis (again as a Vector3), and a degree to which to rotate. This function can modify both the position and the rotation elements of your transform.

Rotate a GameObject to player position?

I am creating a 2D game and I have a prefab enemy, this enemy is a cannon. I want rotate this cannon when player change position.
The cannon should always rotate to the player position
I'm trying this.
// Update is called once per frame
void Update () {
float distance = Vector2.Distance(player.position, transform.position);
if(distance < 10){
Vector2 dir = player.position - transform.position;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
Quaternion qto = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, qto, 5f * Time.deltaTime);
}
}
see the result: https://www.youtube.com/watch?v=REeSNKWLvIQ
The cannon isn't rotate to the player position.
How can I solve this problem ?
Your Code is Fine Just Make a Small Change And You Are Good to Go :
void Update () {
float distance = Vector2.Distance(player.position, transform.position);
if(distance < 10){
Vector2 dir = player.position - transform.position;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
Quaternion qto = Quaternion.AngleAxis(angle, Vector3.forward);
Quaternion qto2 = Quaternion.Euler (qto.eulerAngles.x,
qto.eulerAngles.y,
qto.eulerAngles.z + 90);
transform.rotation = Quaternion.Slerp(transform.rotation, qto2, 5f * Time.deltaTime);
}
}
I'm Just Add qto2 and Slerp to that.
You can try this code:
float turnspeed=1.0f;
//set a turning speed
void Update ()
{
dir = player.position - transform.position;
dir.Normalize();
transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(dir), turnSpeed *Time.deltaTime);
}