- Home /
3D Platform Controller Forward & Camera HELP!
I'm working on the controls for a 3D platformer. I've got most things I want so far in the controls, but I can't figure out how to make the object face the direction I want, AND make it play right with a 3D camera, like the Camera Orbit. I realize that my script is custom made, but I'd rather keep it like this for better accessing later in development. #pragma strict #pragma implicit #pragma downcast //OUTSIDE SCRIPT VARIABLES var canMove = true; var moveSpeed : float = 6.0;
var canJump = true;
var jumpHeight : float = 12.0;
var gravity : float = 20.0;
var canDoubleJump = true;
var doubleHeight : float = 4.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update(){
var controller : CharacterController = GetComponent(CharacterController);
//CONTROLS THE MOVEMENT
if(controller.isGrounded && canMove){
moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= moveSpeed;
canDoubleJump = true;
//CONTROLS THE JUMP
if(Input.GetButtonDown("Jump") && canJump){
moveDirection.y = jumpHeight;
}
}
//CONTROLS MOVEMENT IN AIR
if(!controller.isGrounded && canMove){
moveDirection.x = Input.GetAxis("Horizontal");
moveDirection.x *= moveSpeed + 3;
moveDirection.z = Input.GetAxis("Vertical");
moveDirection.z *= moveSpeed + 3;
}
//CONTROLS DOUBLE JUMPING
if(!controller.isGrounded && canDoubleJump && canJump && Input.GetButtonDown("Jump")){
moveDirection.y = jumpHeight + doubleHeight;
canDoubleJump = false;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
function OnTriggerStay(other : Collider){
//MOVING ON PLATFORMS
if(other.tag == "Platform"){
this.transform.parent = other.transform.parent;
}
}
function OnTriggerExit(other : Collider){
//MOVING OFF PLATFORMS
if(other.tag == "Platform"){
this.transform.parent = null;
}
}
Note you don't spell out your problem. I have no clue what you mean by "object face the direction I want." Nor how this script and your camera script interact in a way you don't want.
I mean the player rotating around and turning towards the direction it's moving, and it moving relative to the camera.
Someone said I should multiply the vector of move by Vector3.forward, but I don't really know how to go about doing this...
Answer by Deternal · Jun 18, 2013 at 10:05 PM
I suggest you "Third Person MMO Controller" for camera orbit. You can search at asset store.
Answer by Cooper37 · Jun 19, 2013 at 10:23 PM
Okay, I modified the script to this:
#pragma strict #pragma implicit #pragma downcast //OUTSIDE SCRIPT VARIABLES var canMove = true; var moveSpeed : float = 6.0; var maxRotationSpeed : float = 360;
var canJump = true; var jumpHeight : float = 12.0; var padHeight : float = 25.0; var gravity : float = 20.0;
var canDoubleJump = true; var doubleHeight : float = 4.0; private var canPad = true; private var autoRotate : boolean = true;
private var moveDirection : Vector3 = Vector3.zero;
function Update(){ var controller : CharacterController = GetComponent(CharacterController);
//CONTROLS THE MOVEMENT
if(controller.isGrounded && canMove){
moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
directionVector = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= moveSpeed;
canDoubleJump = true;
if(directionVector != Vector3.zero){
var directionLength = directionVector.magnitude;
directionVector = directionVector / directionLength;
directionLength = Mathf.Min(1, directionLength);
directionLength = directionLength * directionLength;
directionVector = directionVector * directionLength;
}
directionVector = Camera.main.transform.rotation * directionVector;
var camToCharacterSpace = Quaternion.FromToRotation(-Camera.main.transform.forward, transform.up);
directionVector = (camToCharacterSpace * directionVector);
//CONTROLS THE JUMP
if(Input.GetButtonDown("Jump") && canJump){
moveDirection.y = jumpHeight;
}
if (autoRotate && directionVector.sqrMagnitude > 0.01) {
var newForward : Vector3 = ConstantSlerp(
transform.forward,
directionVector,
maxRotationSpeed * Time.deltaTime
);
newForward = ProjectOntoPlane(newForward, transform.up);
transform.rotation = Quaternion.LookRotation(newForward, transform.up);
}
} //CONTROLS MOVEMENT IN AIR if(!controller.isGrounded && canMove){ moveDirection.x = Input.GetAxis("Horizontal"); moveDirection.x = moveSpeed + 3; moveDirection.z = Input.GetAxis("Vertical"); moveDirection.z = moveSpeed + 3; } //CONTROLS DOUBLE JUMPING
if(!controller.isGrounded && canDoubleJump && canJump && Input.GetButtonDown("Jump")){ moveDirection.y = jumpHeight + doubleHeight; canDoubleJump = false; }
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
function ProjectOntoPlane (v : Vector3, normal : Vector3) { return v - Vector3.Project(v, normal); }
function ConstantSlerp (from : Vector3, to : Vector3, angle : float) { var value : float = Mathf.Min(1, angle / Vector3.Angle(from, to)); return Vector3.Slerp(from, to, value); }
function OnTriggerStay(other : Collider){ //MOVING ON PLATFORMS if(other.tag == "Platform"){ this.transform.parent = other.transform.parent; } }
function OnTriggerEnter(other : Collider){ //JUMP PAD//Works perfect with sphere colliders if(other.tag == "Jump Pad"){ canDoubleJump = false; moveDirection.y = padHeight; }
} function OnTriggerExit(other : Collider){ //MOVING OFF PLATFORMS if(other.tag == "Platform"){ this.transform.parent = null; } if(other.tag == "Jump Pad"){ canDoubleJump = true;
} }
I have the Main Camera with a Mouse Orbit script...it's close, but it still acts kinda weird. Any more help?
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How can I add air control to my 3d platformer's Character Controller? 0 Answers
Smoothing out jumping? 1 Answer
Begginer Jumping javascrpit problem. 1 Answer
Scripting help 1 Answer