- Home /
Player facing wrong direction
I'm trying to choose a point on a circle and set this point as direction of my actor. Now the actor doens't move toward the right point and rotations faces the wrong direction :|
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Actor : MonoBehaviour {
public static List Actors = new List();
public Color Color;
public Actor instance;
public Vector3 Position;
public Vector3 Direction;
public float Speed;
// Use this for initialization
void Start () {
instance = this;
Debug.Log(Actors.Count);
Position = transform.position;
}
// Update is called once per frame
void Update ()
{
transform.position += Direction * Speed * Time.deltaTime;
Position = transform.position;
}
public void instantiateActor(Color color)
{
Actors.Add(this);
this.renderer.material.color = color;
}
//former Draw method
public void rotation()
{
Vector3 target = new Vector3(Direction.x,0,Direction.y);
Debug.Log(target);
transform.LookAt(target);
}
public static Vector3 GetRandomPosition(int Xmax, int Ymax)
{
return new Vector3(Random.Range(-Xmax,Xmax),0,Random.Range(-Ymax,Ymax));
}
public static Vector3 GetRandomDirection()
{
float rotation = Random.Range(0.0f,1.0f) * (Mathf.PI*2);
return new Vector3(Mathf.Cos(rotation),0,Mathf.Sin(rotation));
}
public void SetPosition(Vector3 position)
{
transform.position = position;
}
void OnDrawGizmosSelected()
{
Gizmos.color= Color.yellow;
Gizmos.DrawSphere(new Vector3(Direction.x,0,Direction.y),10);
}
}
Comment
Answer by tmdchi · Jul 06, 2012 at 10:09 AM
Make sure the local Z axis of your actor is facing forward.
Answer by corax · Jul 06, 2012 at 02:04 PM
fixed:
public void rotation()
{
float rotationAngle = Mathf.Atan2(Direction.x,Direction.z) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0,rotationAngle,0);
}
The trick is the radiant to degree conversion :D
Your answer
Follow this Question
Related Questions
Choose starting point/angle for rotation with cos and sin 0 Answers
Camera Rotation 1 Answer
Trigonometric functions 2 Answers
simulate a circle move 2 Answers
Using sin and cos to decompose a vector 2 Answers