- Home /
Question by
FantaLaTone · Mar 17, 2021 at 09:45 AM ·
3dmobiledragtopdown
how i can make smooth rotation for mobile game?
hi i'm currently working on a new mobile game. it's 3d top down view game. i add a simple player controller and a drag rotation system too. but when i tried it first it worked correctly but when tried to change rotation with clicking again it turned suddenly so it's not good for my game i want much more smoother thing. how i can code smoother rotation? this player controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarController : MonoBehaviour
{
public Rigidbody rb;
public Vector3 mouseStart;
public Vector3 mouseDrag;
public Vector3 dir;
void Start()
{
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
mouseStart = Input.mousePosition;
}
else if (Input.GetMouseButton(0))
{
mouseDrag = Input.mousePosition;
dir = mouseDrag - mouseStart;
dir = new Vector3(dir.x, 0, dir.y);
transform.LookAt(transform.position + dir.normalized);
rb.velocity = transform.forward;
}
else if (Input.GetMouseButtonUp(0))
{
mouseStart = Vector3.zero;
mouseDrag = Vector3.zero;
}
}
}
Comment
Answer by FantaLaTone · Mar 17, 2021 at 10:28 AM
ok everybody, i find out myself how to do that. use this block after "dir = new Vector3(dir.x, 0, dir.y);" ta da! now your code is work perfectly
if (mouseStart != mouseDrag)
{
Vector3 targetDirection = (dir - transform.position).normalized;
targetRotation = Quaternion.LookRotation(targetDirection);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime * speed);
transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
}