- Home /
Question by
tiffany811205 · May 18, 2014 at 09:05 AM ·
transformpositiontranslateforward
Stop moving at the target position by using Vector3.forward
Hi I want to create a game that when I press the button, it will generates a random number and my character will move.(If the number is 3, then my character will move 3 units.) Now I have a problem. I use Vector3.forward. How to modify my script to let my character stop at the correct position? How to set the target position?
This is my script:
var speed:float = 0.5;
var number:int;
static var targetPosition:Vector3;
var script:DiceNumber;
var minDistance:float = 0.5;
var a = 3;
function Start () {
script = GetComponent(DiceNumber);
gameObject.Find("person4").AddComponent ("Rigidbody");
rigidbody.constraints = RigidbodyConstraints.FreezeRotationX |
RigidbodyConstraints.FreezeRotationY |
RigidbodyConstraints.FreezeRotationZ |
RigidbodyConstraints.FreezePositionY;
}
function Go (){
number = DiceNumber.num; //call DiceNumber.num
}
function OnCollisionEnter(collision : Collision){
Debug.Log("hit the" + collision.gameObject.name);
if( collision.gameObject.name == "Plane10"+a){
rigidbody.transform.eulerAngles.y = 0;
}
}
public function Update () {
transform.Translate(Vector3.forward * Time.deltaTime);
if(Vector3.Distance(transform.position,targetPosition)< minDistance )
animation.CrossFade("Idle");
else
animation.CrossFade("Walk");
}
Please help me :(
Thanks!!
Comment
Your answer
Follow this Question
Related Questions
Difference between transform.forward and transform.position.z? 1 Answer
Match position of two object without making one a child of the other 4 Answers
UnityEngine.Component:get_transform() Error 1 Answer
Transform.Position Collision Issue 0 Answers
Transform position changes multiplies value by 7.61 1 Answer