Question by
genepic · May 18, 2016 at 07:24 AM ·
2d game2d rotation
Hi Friends, I am a beginner in Unity world also I am crawling child in C#.. I want to Flip My Character According to My Touch Position left or right.. Now I can flip character but not according the touch its just flip whenever i touch..
using UnityEngine; using System.Collections;
public class meramaidSwim : MonoBehaviour { private float lastTouchTime, currentTouchTime;
public float velocityVal;
public float torqueVal;
public float thresholdTime;
void Awake()
{
velocityVal = 7.0f;
torqueVal = 150.0f;
thresholdTime = 0.3f;
}
void Update()
{
if UNITY_ANDROID
MermaidSwimAndroid();
endif
if UNITY_EDITOR MermaidSwim(); #endif }
void MermaidSwim()
{
// for testing only in computer
Vector3 currentPos, touchedPos, distanceVec;
if (Input.GetMouseButtonUp(0))
{
currentPos = Camera.main.WorldToScreenPoint(transform.position);
touchedPos = Input.mousePosition;
distanceVec = (touchedPos - currentPos).normalized;
stopRotatingMermaidAndMoveIt(distanceVec, velocityVal);
}
}
void MermaidSwimAndroid()
{
Vector3 currentPos, touchedPos, distanceVec;
for (int i = 0; i < Input.touches.Length; i++)
{
Touch touch = Input.GetTouch(i);
currentPos = Camera.main.WorldToScreenPoint(transform.position);
touchedPos = touch.position;
distanceVec = (touchedPos - currentPos).normalized;
}
}
void stopRotatingMermaidAndMoveIt(Vector3 distanceVec, float velocity)
{
GetComponent<Rigidbody2D>().velocity = distanceVec * velocity;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
} strong text
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612081032im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help for a Dash Script 1 Answer
How to fix enemy's rotation (2D) 0 Answers
2D player enter/exit orbit 0 Answers
How can I make player shoot in 2d with a joystick when we move the joystick out of its circuler area 0 Answers
2D fixed cam aim problem 0 Answers