- Home /
Can't change vector direction
Hello, I'm trying to develop a mobile controller (with two touchpads) that uses Character Motor script. I'm almost done. The only thing is that when I'm applying that vector to the Character Motor script, I need to write the rotation like this: motor.inputMoveDirection = transform.rotation * movement; But it's not working. Forward is always forward only in one direction, regardless of which way my camera and the character are pointing. I've tried creating another variable for the object, rotate it according to the camera, etc. Nothing works.
Here is the full script, which is a combined version of FPSInputController (from Standard Assets) and FirstPersonControl (from Standard Assets (mobile)). Disregard all the variables that are not used, I'll delete them later.
#pragma strict
@script RequireComponent( CharacterController )
// This script must be attached to a GameObject that has a CharacterController
var moveTouchPad : Joystick;
var rotateTouchPad : Joystick; // If unassigned, tilt is used
var cameraPivot : Transform; // The transform used for camera rotation
var forwardSpeed : float = 4;
var backwardSpeed : float = 1;
var sidestepSpeed : float = 1;
var jumpSpeed : float = 8;
var inAirMultiplier : float = 0.25; // Limiter for ground speed while jumping
var rotationSpeed : Vector2 = Vector2( 50, 25 ); // Camera rotation speed for each axis
var tiltPositiveYAxis = 0.6;
var tiltNegativeYAxis = 0.4;
var tiltXAxisMinimum = 0.1;
private var thisTransform : Transform;
private var character : CharacterController;
private var cameraVelocity : Vector3;
private var velocity : Vector3; // Used for continuing momentum while in air
private var canJump = true;
private var motor : CharacterMotor;
function Start()
{
motor = GetComponent(CharacterMotor);
// Cache component lookup at startup instead of doing this every frame
thisTransform = GetComponent( Transform );
character = GetComponent( CharacterController );
}
function Update()
{
var movement = thisTransform.TransformDirection( Vector3( moveTouchPad.position.x, 0, moveTouchPad.position.y ) );
// We only want horizontal movement
//movement.y = 0;
//movement.Normalize();
motor.inputMoveDirection = transform.rotation * movement;
// Apply rotation from rotation joystick
if ( character.isGrounded )
{
var camRotation = Vector2.zero;
if ( rotateTouchPad )
camRotation = rotateTouchPad.position;
camRotation.x *= rotationSpeed.x;
camRotation.y *= rotationSpeed.y;
camRotation *= Time.deltaTime;
// Rotate the character around world-y using x-axis of joystick
thisTransform.Rotate( 0, camRotation.x, 0, Space.World );
// Rotate only the camera with y-axis input
cameraPivot.Rotate( -camRotation.y, 0, 0 );
}
}
So, how do I make it that vector change its rotation based on the player's or camera's rotation? So that forward is wherever character is pointing? Thank you all in advance!
Your answer
Follow this Question
Related Questions
C# Raycast from Object direction (z-axis) + another Vector3 1 Answer
Vector direction after collision 1 Answer
Fire Knockback 3 Answers