- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
unity_HB7tUJeYO9dzNA · Jun 11, 2019 at 06:52 AM ·
transformlerpmovetowardssmoothingswapping
Smooth swapping my game object
In my game i want to move my player to x-direction using swapping (3-position -9,0,9 ), I wrote the programme but smoothing was not good i try with Vector3.lerp but my object move smoothly but when i give speed to player move zig-zag pattern (player move second lane little back and go initial position. I write down program plz help about this. using UnityEngine; using System.Collections;
public class playercontrol : MonoBehaviour {
Vector2 firstPressPos;
Vector2 secondPressPos;
Vector2 currentSwipe;
Vector3 spos,endpos;
public bool check = false ;
public float time = 0; private float movetime; private float moveduration=0.1f; void Update() { playera.transform .Translate ((Vector3.forward speed Time.deltaTime));
if(Input.GetMouseButtonDown(0))
{
Debug.Log ("left button");
//save began touch 2d point
firstPressPos = new Vector2(Input.mousePosition.x,Input.mousePosition.y);
}
if (Input.GetMouseButtonUp (0)) {
//save ended touch 2d point
secondPressPos = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
//create vector from the two points
currentSwipe = new Vector2 (secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
//normalize the 2d vector
currentSwipe.Normalize ();
// desiredPos = Vector3.Lerp (firstPressPos, secondPressPos, Time.deltaTime);
//swipe left
if (currentSwipe.x < 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f) {
Debug.Log ("left swipe");
if (transform.position.x > -9f) {
// StartCoroutine (move ("left"));
/* spos = transform.position;
Debug.Log ("spos" + spos);*/
transform.position = new Vector3 (transform.position.x - 9f, transform.position.y, transform.position.z);
//transform.Rotate = new Vector3 (0, 0, xAngle);
//desiredPos = Vector3.Lerp (transform.position ,desiredPos ,Time.deltaTime);
//endpos =new Vector3 (transform.position.x - 9f ,transform.position.y,transform.position.z);
/* endpos = new Vector3 (transform.position.x - 9.0f, transform.position.y,transform.position.z);
//endpos = transform.position;
Debug.Log ("endpos" + endpos);
check = true;
for (float i = 0.0f; i < 9f; i++) {
//transform.position.x = i;
Debug.Log ("i");
transform.position= new Vector3 (transform.position.x+i, transform.position.y, transform.position.z);
check = true; */
}
}
//swipe right
if (currentSwipe.x > 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f) {
Debug.Log ("right swipe");
if (transform.position.x < 9f) {
Debug.Log ("spos");
// spos = transform.position;
// Debug.Log ("spos" + spos);
// desiredPos= new Vector3 (transform.position.x +9f,transform.position.y,transform.position.z);
//desiredPos = Vector3.Lerp (transform.position ,desiredPos ,Time.deltaTime);
//endpos = new Vector3 (transform.position.x + 9f,transform.position.y,transform.position.z);
transform.position = new Vector3 (transform.position.x + 9f, transform.position.y, transform.position.z);
//endpos = transform.position;
//check = true;
}
}
}
}
}
Comment