- Home /
character controller rotation problem (c#)
Hi, I'm new to Unity, and my problem is simple but I do not know how to solve it: I have a big scene with a 3rd person controller and need to move my character to certain key points (facing a determined area) through a menu (the problem is the rotation angle in the Y axis), and already partially achieved my goal with the following code:
using UnityEngine;
using System.Collections;
public class Zona{
public Zona(string nombre, Vector3 pos, Rect rect, float rotar)
{
Nombre = nombre; Pos = pos; Rect = rect; Rotar = rotar;
}
public string Nombre{get; set;}
public Vector3 Pos{get; set;}
public Rect Rect{get; set;}
public float Rotar{get; set;}
}
[RequireComponent(typeof(CharacterController))]
public class ubicarse : MonoBehaviour {
private Zona[] zonas = new Zona[]
{
new Zona("Parking",new Vector3(31.2251f, 9.12212f, -87.28203f),
new Rect(10,30,180,20), 186.0f),
new Zona("Main Office",new Vector3(66.87338f, 9.12212f, -103.3831f),
new Rect(10,50,180,20), 233.0f)
//new Zona...
};
private Rect Ir_A = new Rect(10,10,180,20);
bool verMenu = false;
IEnumerator MoverA(Vector3 destino, float rotacion){
float t = 0.0f, rate = 1.0f / 0.5f;
while (t < 1.0f)
{
t += Time.deltaTime * rate;
transform.position = Vector3.Lerp(transform.position, destino, t);
yield return null;
}
transform.Rotate(0f,rotacion,0f); // <<---------- PROBLEM HERE!!!!
transform.position = destino;
}
void OnGUI(){
if(GUI.Button(Ir_A,"Ir a...")) verMenu = !verMenu;
if(verMenu)
{
for(int i=0; i<zonas.Length;i++)
{
if(GUI.Button(zonas[i].Rect,zonas[i].Nombre)){
StartCoroutine(MoverA(zonas[i].Pos, zonas[i].Rotar));
return;
}
}
}
}
}
sounds silly but I have 3 days trying and what I have achieved is only to make vibrate the character and return to its original rotation (I've tried transform.rotation.y, character.move, quaternion.slerp and many more)
By the way, if there is any more optimal code to do what i'm doing, i greatly appreciate it. Someone please help me...
Your answer
Follow this Question
Related Questions
Character Model direction 1 Answer
how can i let my player move converse? 0 Answers
Rotating Player 0 Answers
Help with plat-former character rotate 1 Answer
Moving an object based on the position of another object 2 Answers