- Home /
transform.forward does not work with Rotate(Vector3.up)
So I'm trying to make a character move based on collisions. When the avatar's hand collides with a "rotator" cube, start rotating to the side of the cube (left/right). However, I also have 2 extra cubes, for movement (forward, backward). These cubes always move on the Vector3.forward axis, although I have explicitly asked for the transform.forward. Check my script and my screenshot. The scripts are as follows.
Collcomp.js
var TOP:GameObject;
var RIGHT:GameObject;
var BOT:GameObject;
var LEFT:GameObject;
//this is the direction variable. Starting from Top (or front) with 1, counting 1 by 1 moving clockwise.
static var dirVar:int;
function OnTriggerEnter(coll:Collider)
{
if (coll.gameObject==TOP.gameObject)
{
//Debug.Log("Top");
dirVar=1;
}
if (coll.gameObject==RIGHT.gameObject)
{
//Debug.Log("Right");
dirVar=2;
}
if (coll.gameObject==BOT.gameObject)
{
//Debug.Log("Bottom");
dirVar=3;
}
if (coll.gameObject==LEFT.gameObject)
{
//Debug.Log("Left");
dirVar=4;
}
}
function OnTriggerExit(coll:Collider)
{
//Debug.Log("Null movement.");
if (dirVar!=0)
{
dirVar=0;
}
}
MovementScript.js
var direction:Vector3 = Vector3.zero;
var character:CharacterController;
private var front:Vector3;
private var back:Vector3;
private var right:Vector3;
private var left:Vector3;
function Start()
{
character = this.GetComponent(CharacterController);
front = transform.forward;
back = -transform.forward;
left = -transform.right;
right = transform.right;
}
function Update()
{
switch(CollComp.dirVar)
{
case(1):
direction = front;
break;
case(2):
direction = left;
break;
case(3):
direction = back;
break;
case (4):
direction = right;
break;
default:
direction = Vector3.zero;
break;
}
character.Move(direction * speed * Time.deltaTime);
}
Is direction
a vector or a float? You use it in the $$anonymous$$ove()
so I assume it's a vector. But then you set it to +/-1, which makes it look like a float.
You can simply rotate the rotate the object with transform.Rotate(x, y, z, Space.Self). Adjust the positive or negative number to whatever axis you want to turn left or right.
You dont need the turn, direction, or Time.deltaTime to make this work.
Direction is a V3. I thought that v3 = X is the same as v3(X,X,X). It works anyway so I was never proven wrong.
Also I want the turn because it will be adjustable from the insp, I need direction because I want Debug.Logs also at another point and I read you should use time.deltatime to time stuff correctly.
Umm, no, it's not the same. There is no implicit cast from int or float to Vector3. Do you have #pragma strict at the top of your script? If you have you should get a compiler error that you can't convert an int to Vector3.
You said it's a Vector3 but it looks like it's a float since in all places you use it like a float. For example in your Rotate function in line 30 you multiply direction with another Vector3. If directio is also a Vector3 that wouldn't work at all.
The next strange thing is, why do you store the initial forward vector in "front" / "back"? Where do you actually use them? $$anonymous$$eep in $$anonymous$$d that the value that is stored in front / back won't change, it's a value type.
Your whole post is a bit strange. You include stuff you don't use and you omit stuff you actually use. Also it seems that you're not sure what type they have, so i would suggest to rethink the whole thing. Can you explain what it is supposed to do?
What it is supposed to do, is when your hand (the avatar is controlled by kinect) collides with a rotator, the rotator sets the direction to positive/negative (actually direction DOES seem wrong as a v3). Then at the Update, based on the direction the collision is giving, rotate the character controller left/right based on direction.
I know the script has some errors. I learnt UnityScript on my own(basics only) and I'm just now taking the Unity course at my Uni. Should I remove the decls for front/back etc?
EDIT: This whole thing is messed up. I'll post the whole scripts separately in my question.
Your answer
![](https://koobas.hobune.stream/wayback/20220613092202im_/https://answers.unity.com/themes/thub/images/avi.jpg)