Skip to content

Instantly share code, notes, and snippets.

@burkaslarry
Last active October 31, 2019 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burkaslarry/636ddad4fd94eb3132e9babfa3e687a9 to your computer and use it in GitHub Desktop.
Save burkaslarry/636ddad4fd94eb3132e9babfa3e687a9 to your computer and use it in GitHub Desktop.
Unity : Moving object by drag : constant speed, deceleration, and acceleration and deceleration
using UnityEngine;
using System.Collections;
public class CarControllerTouch : MonoBehaviour
{
float speed = 10f;
Vector3 destination;
float rotSpeed = 10;
public float smoothTime = 0.3F;
private Vector3 velocity = Vector3.zero;
void Start()
{
destination = transform.position;
}
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Ray ray = Camera.main.ScreenPointToRay(touch.position);
Debug.DrawRay(ray.origin, ray.direction * 100, Color.green);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
destination = hit.point;
}
}
destination.y = transform.position.y;
//transform.position= Vector3.MoveTowards(transform.position, destination, speed*Time.deltaTime);
//transform.position = Vector3.Lerp(transform.position, destination, speed / 5 * Time.deltaTime);
transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, smoothTime);
//transform.LookAt (destination);
Vector3 lookVector = destination - transform.position;
if (lookVector != Vector3.zero)
{
Quaternion targetRot = Quaternion.LookRotation(lookVector);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, rotSpeed * Time.deltaTime);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment