Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Cooper37 · Jun 18, 2013 at 07:27 AM · 3dcontrollerplatformerforward

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;
     }
 }

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cooper37 · Jun 18, 2013 at 05:25 PM 0
Share

Any help?

avatar image robertbu · Jun 18, 2013 at 10:55 PM 0
Share

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.

avatar image Cooper37 · Jun 18, 2013 at 11:06 PM 0
Share

I mean the player rotating around and turning towards the direction it's moving, and it moving relative to the camera.

avatar image Cooper37 · Jun 19, 2013 at 04:43 AM 0
Share

Someone said I should multiply the vector of move by Vector3.forward, but I don't really know how to go about doing this...

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges