Hey.
I have been looking on google for tutorials/scripts, but couldn’t find anything. Anyway i am having a problem. I have a top down controller which is very basic right now. You just get the horizontal and vertical axis and AddForce in that direction. So like i said very very basic. It can also rotate to the current moving direction.
However, right now it is suitable for a humanoid-like movement. But i would like a more vehicle style approach. So that the player can not make sharp turns, but instead would turn slowly which is determined by a turnSpeed variable.
Well i followed a tutorial: Creating a Mobile Optimized Vehicle in Unity - Unity Game Engine - YouTube ← this one! Which is great for a 3rd person view camera. But i have a Top-Down view, and i would like that the forward direction is always pointed up (if that makes sense) and that the right direction is always pointed right, not the right of the vehicle’s transform.
So eventually you would have the vehicle moving towards the joystick angle.
Does anyone know how to do this? Or has a link of a tutorial that is covering this?
My script(for the moment) is:
using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
public class Player : MonoBehaviour {
public float speed = 5;
public float turnSpeed = 5;
private Rigidbody rb;
private Vector3 v;
private Transform myTransform;
Vector3 lookPos;
void Start()
{
rb = gameObject.GetComponent<Rigidbody>();
myTransform = transform;
}
void Update()
{
}
void FixedUpdate()
{
float horizontal = CrossPlatformInputManager.GetAxis("Horizontal");
float vertical = CrossPlatformInputManager.GetAxis("Vertical");
float r_horizontal = CrossPlatformInputManager.GetAxis("r_horizontal");
float r_vertical = CrossPlatformInputManager.GetAxis("r_vertical");
Moving(horizontal, vertical);
//Rotation(r_horizontal, r_vertical);
}
void Moving(float horizontal, float vertical)
{
Vector3 movement = new Vector3(horizontal, 0, vertical);
float angle = Mathf.Atan2(horizontal, vertical) * Mathf.Rad2Deg;
rb.AddForce(movement * speed / Time.deltaTime);
myTransform.rotation = Quaternion.Euler(new Vector3(0, angle, 0));
}
void Rotation(float horizontal, float vertical)
{
float angle = Mathf.Atan2(horizontal, vertical) * Mathf.Rad2Deg;
myTransform.rotation = Quaternion.Euler(new Vector3(0, angle, 0));
}
}
Anyway thanks in advance, i am really stuck here. So any help is appreciated!
EDIT!!!
Here is an example picture of what i am trying to achieve.
The blue dot is the player, the green dot is the joystick(the black arrow is the movement direction)
The light blue line with the greyish arrow in it is the actual movement.
The red arrow is the player’s current facing direction.
1 is what i am getting right now and 2 is what i want.
So if the player is facing up, and the player presses right, it will turn according to the turningSpeed to the right. While making a turn, the player transform/rigidbody also rotates to the “targetRotation”.
Cheers,
Darryl