- Home /
Objects Rotation to modify Objects transform velocity/Direction of travel
Hi, So I'm trying to make a 2.5d game, And i have created a script that modifies the players rotation when the player hits a certain way point, this is for the exploration of the extra .5d. However even though the player rotation is changed the character continues on its way to the right Ignoring the rotation. I am using the default script from the Mobile assets called side scroll control.
//////////////////////////////////////////////////////////////
// SidescrollControl.js
//
// SidescrollControl creates a 2D control scheme where the left
// pad is used to move the character, and the right pad is used
// to make the character jump.
//////////////////////////////////////////////////////////////
#pragma strict
@script RequireComponent( CharacterController )
// This script must be attached to a GameObject that has a CharacterController
var moveTouchPad : Joystick;
var jumpTouchPad : Joystick;
var forwardSpeed : float = 4;
var backwardSpeed : float = 4;
var jumpSpeed : float = 16;
var inAirMultiplier : float = 0.25; // Limiter for ground speed while jumping
private var thisTransform : Transform;
private var character : CharacterController;
private var velocity : Vector3; // Used for continuing momentum while in air
private var canJump = true;
function Start()
{
// Cache component lookup at startup instead of doing this every frame
thisTransform = GetComponent( Transform );
character = GetComponent( CharacterController );
// Move the character to the correct start position in the level, if one exists
var spawn = GameObject.Find( "PlayerSpawn" );
if ( spawn )
thisTransform.position = spawn.transform.position;
}
function OnEndGame()
{
// Disable joystick when the game ends
moveTouchPad.Disable();
jumpTouchPad.Disable();
// Don't allow any more control changes when the game ends
this.enabled = false;
}
function Update()
{
var movement = Vector3.zero;
// Apply movement from move joystick
if ( moveTouchPad.position.x > 0 )
movement = Vector3.right * forwardSpeed * moveTouchPad.position.x;
else
movement = Vector3.right * backwardSpeed * moveTouchPad.position.x;
// Check for jump
if ( character.isGrounded )
{
var jump = false;
var touchPad = jumpTouchPad;
if ( !touchPad.IsFingerDown() )
canJump = true;
if ( canJump && touchPad.IsFingerDown() )
{
jump = true;
canJump = false;
}
if ( jump )
{
// Apply the current movement to launch velocity
velocity = character.velocity;
velocity.y = jumpSpeed;
}
}
else
{
// Apply gravity to our velocity to diminish it over time
velocity.y += Physics.gravity.y * Time.deltaTime;
// Adjust additional movement while in-air
movement.x *= inAirMultiplier;
// movement.z *= inAirMultiplier;
}
movement += velocity;
movement += Physics.gravity;
movement *= Time.deltaTime;
// Actually move the character
character.Move( movement );
if ( character.isGrounded )
// Remove any persistent velocity after landing
velocity = Vector3.zero;
}
So i was wondering what should i change to allow the rotation of the player to influence the velocity or Direction of travel the player takes. Or would i have to change how the rotation script works by adding another variable to change.
Thanks in Advance.
Answer by robertbu · Oct 12, 2014 at 04:20 AM
Untested, but if you replace lines 54 through 57 but try replacing lines 54 through 57 with:
// Apply movement from move joystick
if ( moveTouchPad.position.x > 0 )
movement = transform.right * forwardSpeed * moveTouchPad.position.x;
else
movement = transform.right * backwardSpeed * moveTouchPad.position.x;
Note the change from Vector3 to transform.
Thank you!, I knew that it would be something simple like that just to be annoying... Well I tested it and it works.
Your answer
Follow this Question
Related Questions
How to make a player rotate in a direction with a slope of the ground. 0 Answers
Rotating the player animation also rotates the player 2 Answers
HTC Vive Controller Twist - Extracting Angular Velocity On The Correct Axis 0 Answers
Return Proper Velocity of Player object 3 Answers
How to Convert dir = (a.transform.position - b.transform.position).normalized; to Degrees ? 1 Answer