- Home /
Turning Vectors....
What would I add to this script to have the object turn 45 degrees and move that direction when ever your cursor moves over the object?
var speed = 5;
var deer = 10;
var tap = 100;
function Update () {
transform.position += Vector3.forward * Time.deltaTime * speed;
}
function OnMouseEnter () {
speed = 15;
TotalScoreScript.Points += deer + 10;
}
function OnMouseExit () {
speed = 5;
}
function OnMouseDown () {
TotalScoreScript.Points += tap + 100;
}
Answer by Bunny83 · Sep 27, 2011 at 01:58 AM
Like @msknapp already mentioned that's how you can turn the character:
function OnMouseEnter () {
speed = 15;
TotalScoreScript.Points += deer + 10;
transform.Rotate(Vector3.up,45);
}
function OnMouseExit () {
speed = 5;
transform.Rotate(Vector3.up,-45);
}
However you forgot to move it in that direction. transform.position is the position in worldspace so it doesn't matter how it's rotated, it will always move the same direction.
You have to use the "rotated" version of forward.
function Update () {
transform.position += transform.forward * Time.deltaTime * speed;
}
Answer by msknapp · Sep 26, 2011 at 07:33 PM
In your function for OnMouseEnter, add
transform.Rotate(Vector3.up,45); // or use -45
In your function for OnMouseExit, add the same line but switch the sign of the angle.
I added the code and the only thing it did was make the object spin. Is there something I'm missing to make the object change direction its moving?
@Beta_Artist: please use comments when you don't want to answer the question! This site is not a forum with discussions. Each question has only one question and (hopefully) many answers. If an answer is not clear enough use comments to communicate.