- Home /
Problem is not reproducible or outdated
Why moveDirection.z doesn't work?
part of the code:
if(Input.GetButtonDown("Fire1") && controller.isGrounded){
playIdle = false;
animation.CrossFade("CHit1",0.01);
animation["CHit1"].speed = 0.23;
moveDirection.z = 20;
moveDirection = transform.TransformDirection(moveDirection);
}
What mode are you in? 2D or 3D? What do you means by "doesn't work"? Please be more specific as to what you a re trying to accomplish and what is happening.
By definition, in 2D, Z movement means nothing.
Well since moveDirection does nothing, Im not surprised.
The code you gave us has no movement code. if there is more code that does, then show it to us.
As is I have no idea even how moveDirection is defined, let alone how it issued.
code:
var gravity : float = 20.0;
var AirSpeedRemove : float = 2.0;
private var moveDirection : Vector3 = Vector3.zero;
private var secondJumped = false;
private var playIdle = true;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
transform.Rotate(0,Input.GetAxis("Horizontal"),0);
if(Input.GetButtonDown("Fire1") && controller.isGrounded){
playIdle = false;
animation.CrossFade("CHit1",0.01);
animation["CHit1"].speed = 0.23;
moveDirection.z = 20;
moveDirection = transform.TransformDirection(moveDirection);
}
if (controller.isGrounded) {
if(playIdle == true){
animation.CrossFade("Idle");
}
secondJumped = false;
moveDirection = Vector3(0, 0,Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
animation.CrossFade("Jump1");
}
}
else {
moveDirection = Vector3(0, moveDirection.y, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection.z *= speed/AirSpeedRemove;
moveDirection.x *= speed/AirSpeedRemove;
}
if(Input.GetButtonDown("Jump") && controller.isGrounded == false && secondJumped == false){
moveDirection.y = secondJumpSpeed;
secondJumped = true;
animation.CrossFade("Jump2");
}
if(Input.GetAxis("Vertical") && controller.isGrounded){
animation.CrossFade("Run");
if(Input.GetButtonDown("Jump")){
animation.CrossFade("Jump1");
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
}
I don't see speed defined and set anywhere. Thus, by this script, it is 0, which means all your functions using speed result in 0.
Answer by Jeff-Kesselman · Jun 01, 2014 at 05:33 PM
I don't see speed defined and set anywhere. Thus, by this script, it is 0, which means all your functions using speed result in 0.
Rather then just dropping your first attempt at code on others in the hopes that they will fix it, you should learn to properly debug your code.
Debugging is a matter of checking each value along the way (usually through Debug.Log) to see fi it is what yo expect it to be and, when you find the first one that isn't, ascertaining why,
$$anonymous$$y code is correct,speed is in script but it wasn't pasted in code,only moveDirection.z = 20 is not working and i don't know why?
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var secondJumpSpeed : float = 15.0;
var gravity : float = 20.0;
var En1 : EnemySph1;
var AirSpeedRemove : float = 2.0;
private var moveDirection : Vector3 = Vector3.zero;
private var secondJumped = false;
private var playIdle = true;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
transform.Rotate(0,Input.GetAxis("Horizontal"),0);
if(Input.GetButtonDown("Fire1") && controller.isGrounded){
playIdle = false;
animation.CrossFade("CHit1",0.01);
animation["CHit1"].speed = 0.23;
moveDirection.z = 20;
moveDirection = transform.TransformDirection(moveDirection);
}
if (controller.isGrounded) {
if(playIdle == true){
animation.CrossFade("Idle");
}
secondJumped = false;
moveDirection = Vector3(0, 0,Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
animation.CrossFade("Jump1");
}
}
else {
moveDirection = Vector3(0, moveDirection.y, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection.z *= speed/AirSpeedRemove;
moveDirection.x *= speed/AirSpeedRemove;
}
if(Input.GetButtonDown("Jump") && controller.isGrounded == false && secondJumped == false){
moveDirection.y = secondJumpSpeed;
secondJumped = true;
animation.CrossFade("Jump2");
}
if(Input.GetAxis("Vertical") && controller.isGrounded){
animation.CrossFade("Run");
if(Input.GetButtonDown("Jump")){
animation.CrossFade("Jump1");
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
}
Answer by gamertest · Jun 24, 2014 at 04:36 AM
if(Input.GetButtonDown("Fire1") && controller.isGrounded){ playIdle = true; animation.CrossFade("CHit1",0.01); animation["CHit1"].speed = 0.23; moveDirection.z = 20; moveDirection = transform.TransformDirection(moveDirection); }
Presuming you have that in a Vector3:
var localVector = transform.InverseTransformDirection(moveDirection); You have to change the playidle flase to true just little change make it worked
Just set the y-component of your moveDirection to 0 before you use it for LookAt, or since it's a 2.5d game link, just use the z axis
// Just use the z axis transform.LookAt(transform.position + Vector3(0,0,moveDirection.z));
// Or set the y axis to 0 var tempDirection = moveDirection; tempDirection.y = 0; transform.LookAt(transform.position + moveDirection);
moveDirection = Vector3(0, moveDirection.y, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection.z *= speed/AirSpeedRemove;
moveDirection.x *= speed/AirSpeedRemove;
Just set the y-component of your moveDirection to 0 before you use it for LookAt, or since it's a 2.5d game link, just use the z axis