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 cronkie · Dec 01, 2012 at 09:04 PM · cameracontrollernavigationcharacter movement

How do I keep the player from automatically changing direction?

Hey all -

So I'm experimenting with some different third-person movement inputs. I have both a point and click to move character sort of working and a platform input character (WASD keyboard movement...just the Platform Input Controller slightly modified). This is all working through the locomotion system, so the movement is properly animating the character (albeit roughly).

I'm doing a fixed camera sort of idea (think the original Resident Evil), and when the character passes through a trigger, the camera changes to a different position (basically turns off the main camera and turns on a different main camera that is positioned differently).

Here's my issue. When the player triggers the camera change and I'm still "moving" the character (just holding down on W), the character will automatically spin around and change direction. It seems as though the WASD movement (or sometimes the mouse to click movement) gets confused when the camera changes and it is basing the direction off of whatever the camera is seeing as opposed to what direction the character is moving in? How would I change this around...is this a transform.position = Vector3.zero problem?

Where would I look to see what's going on? Would this be the Character Motor, the Input Controller? Something else?

Thanks a bunch.

Comment
Add comment · Show 1
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 cronkie · Dec 01, 2012 at 11:53 PM 0
Share

So, I think I need to cache the previous camera vector information for when the camera switches over to the other. How would one do that?

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Dec 02, 2012 at 11:32 AM

The WASD controls in the ThirdPersonController script are based on the main camera orientation: W goes in the camera's forward direction, D goes to its right direction, and so on - that's why the character changes direction when a new camera is selected.
Changing this would require extensive modifications in the original script - I suggest you to remove the scripts ThirdPersonController and ThirdPersonCamera and use a custom script to control both, the character and the camera.
The script below does basically what you want, and may serve as a starting point. It doesn't use multiple cameras - instead of this, the main camera is translated instantly to the pre-set positions when their associated triggers are entered. These positions are defined by empty objects tagged as "CameraPos", which contain only a box (or sphere) collider adjusted to the desired area.
That's the character script:

 var walkSpeed: float = 4.0;
 var runSpeed: float = 8.0;
 var jumpSpeed: float = 8.0;
 var turnSpeed: float = 90.0;
 // animation names
 var walk = "walk";
 var run = "run";
 var idle = "idle";
 var jump = "jump_pose";
 var walkLim: float = 0.2; // enters walk animation above this speed
 var runLim: float = 5.0; // enters run animation above this speed
 
 private var velY: float = 0.0; // current vertical velocity (used for gravity)
 private var gravity: float = 10.0;
 private var jumping = false;
 private var controller: CharacterController;
 private var trCam: Transform;
 
 function Start(){
   controller = GetComponent(CharacterController);
   trCam = Camera.main.transform;
   animation[jump].wrapMode = WrapMode.ClampForever;
 }
 
 function Update(){
   // movement control: AD rotates, WS goes forth/back
   transform.Rotate(0, Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime, 0);
   var speed = walkSpeed; // select moving speed
   if (Input.GetKey("left shift") || Input.GetKey("right shift")){
     speed = runSpeed; // shift activates sprint
   }
   var moveVel = transform.forward * Input.GetAxis("Vertical") * speed;
   if (controller.isGrounded){
     jumping = false; // character landed: isn't jumping anymore
     velY = 0;
     if (Input.GetButton("Jump")){
       velY = jumpSpeed;
       jumping = true; // character started the jump
     }
   }
   velY -= gravity * Time.deltaTime; // calculate gravity effect
   moveVel.y = velY; // add it to the current movement velocity
   controller.Move(moveVel * Time.deltaTime); // move the character
   // animation
   var vel = controller.velocity.magnitude; // base it on current velocity
   if (jumping){ // jumping is prioritary!
     animation.CrossFade(jump);
   } else if (vel > runLim){ // if not jumping, check run...
     animation.CrossFade(run);
   } else if (vel > walkLim){ // else check walk...
     animation.CrossFade(walk);
   } else { // else go to idle
     animation.CrossFade(idle);
   }
 }

 // camera control

 function LateUpdate(){
   trCam.LookAt(transform); // always look at the character
 }
 
 function OnTriggerEnter(other: Collider){
   if (other.tag == "CameraPos"){ // if entered a CameraPos trigger...
     trCam.position = other.transform.position; // move camera to its position
   }
 }
 

Comment
Add comment · Show 4 · 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 cronkie · Dec 02, 2012 at 06:12 PM 0
Share

Playing with the character script and this seems to work! I'll continue to tweak and massage to make it fit my needs a bit more, but I think it's the push I needed. I'm avoiding the camerapos stuff right now, simply because I have that particular element working right now and want to focus on one thing before I break another. :-D Thanks a bunch, Aldo! $$anonymous$$uch appreciated.

avatar image cronkie · Dec 02, 2012 at 07:34 PM 0
Share

Quick Addendum (Not sure if these get lost after the question is "answered", but I'll try): How does the CameraPos get what it's focused on when it's triggered? I'm trying to frame things in a particular way, but it almost always initially focuses on the player's feet since I need a trigger volume that reaches where the player can walk through. It tracks the character properly and that's really cool, but I just want to figure out how to make sure I get some detailed fra$$anonymous$$g for that initial trigger.

In addition, I would still like to leverage my $$anonymous$$ainCameraSwitch script for when I want to do a big change in perspective/environement/etc...how would I tweak my current script to make sure it gets the cool character tracking when the switch is initialized. Here's my code for that particular script:

var camera1 : Camera; var camera2 : Camera;

private var startCamera : int = 1;

function OnTriggerEnter(other : Collider) { if(other.CompareTag("Player")) {

camera1.enabled = !camera1.enabled; camera1.gameObject.active = !camera1.gameObject.active; camera2.enabled = !camera2.enabled; camera2.gameObject.active = !camera2.gameObject.active; startCamera = startCamera == 2 ? 1 : 2;

}

}

Thanks a bunch! Hopefully, this gets seen. If not, I might make a new question to get visibility. :D

avatar image aldonaletto · Dec 03, 2012 at 03:02 AM 0
Share

When a CameraPos trigger is entered, the character function OnTriggerEnter simply moves the camera to the CameraPos object position. The camera focus the character because the instruction LookAt(transform) in LateUpdate makes the camera aim to the player position. If you want different camera heights in each trigger, simply adjust the trigger vertical position.

avatar image cronkie · Dec 03, 2012 at 11:28 PM 0
Share

Okay - that makes sense, thanks Aldo. Is there an easy method in which to set an OnTargetExit on the CameraPos trigger, so it would automatically go back to the previous camera location?

Also, do you know how I would make sure that the character tracking continues to work after I switch the $$anonymous$$ain Camera using my script that I posted above?

Thanks a lot, Aldo. I really appreciate it.

avatar image
0

Answer by ZorNiFieD · Dec 02, 2012 at 05:23 AM

In the rigidbody component of your moving object, there is a Constraints area. You can check off either X Y or Z for Freeze Position or Freeze Rotation. In your case, I believe it is Rotation.

Comment
Add comment · Show 2 · 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 cronkie · Dec 02, 2012 at 08:09 AM 0
Share

That didn't really seem to make much of a difference. Tried noodling around with multiple combinations of settings in the Constraints, but didn't come across anything that helped with this particular issue. I'll keep poking around.

avatar image aldonaletto · Dec 02, 2012 at 11:34 AM 0
Share

No, the 3rd Person Controller is a character controller, not a rigidbody - there are no constraints for a character controller.

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

12 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

Related Questions

How to rotate camera diagonally over players shoulder while still facing towards players direction 0 Answers

WASD Controls not in-sync with camera? 1 Answer

Associate animations with keys 1 Answer

Moving camera around with GUI buttons 4 Answers

Camera interfering with character movement while locking on to enemy. 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