Question by
tomarlokesh93 · Oct 11, 2017 at 07:56 AM ·
c#unity 5move
how to move a rigidbody ball or kick a ball from swipe gesture
help me for getting this and what should i do for kick a ball like soccer game
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class kick : MonoBehaviour {
// Use this for initialization
private Vector2 startpos;
private Vector2 endPos;
private Vector2 touchpos;
private Rigidbody rb;
private Touch touch;
void Start(){
rb = GetComponent<Rigidbody> ();
}
void FixedUpdate(){
float speed = Time.time;
float moveHorizontal;
float moveVertical;
if (Input.touches.Length > 0) {
touch = Input.touches [0];
}
if (touch.phase == TouchPhase.Began) {
startpos = touch.position;
endPos = touch.position;
moveHorizontal = 0.0f;
moveVertical = 0.0f;
}
if (touch.phase == TouchPhase.Ended) {
endPos = touch.position;
speed = Time.time - speed;
moveHorizontal = endPos.x - startpos.x;
moveVertical = endPos.y - startpos.y;
Vector3 movement = new Vector3 (moveHorizontal,moveVertical, 0.0f);
rb.MovePosition(rb.position + movement * Time.fixedDeltaTime);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Move the character automaticly with the road 0 Answers
Snake game, problem with tail 0 Answers
Hiding part of mesh 0 Answers
How to get build my game to the cloud without it failing? 0 Answers