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
0
Question by Lairinus · Nov 20, 2012 at 06:24 PM · c#cameramovement

One Camera, Multiple Characters, Multiple Problems

I'll keep this as brief as possible.

I have 2 stand-in characters at the moment; one cube, and one capsule.

I recently made it possible to switch between the two characters by hitting a key. That's only half of the battle, however.

I also want it so that the character moves when they're selected. Removing the components from the cube should make it so that it won't move when the capsule is selected, but I just can't get the capsule to move no matter what I do.

It turns with the camera (as intended) but the strange thing is that when I child an object to it, the object is what does the moving... not the actual capsule. I have full WASD control.. over the object.. which makes me sad.

Another issue that I'll just put here (might be of help) is that whenever I remove the component (TP_Motor) from the main cube and reapply it, it models the same behavior as the capsule.

Well, you're probably wondering where the scripts are, so here. They're mostly based off of the Buzz 3rd Person tutorial. I don't expect anyone to read through all of the code, but I will appreciate any help in this area.

Thanks

PS. Easy Navigation of Scripts : " public class TP_Motor" , " public class TP_Animator" , "public class TP_Controller" "public class TurnChecker"

TP_MOTOR (Guessing this is where the problem is)

 using UnityEngine;
 using System.Collections;
 
 public class TP_Motor : MonoBehaviour 
 {
         
     //Class Variables
     public static TP_Motor instance;
     //Move Variables
     public float forwardSpeed = 10f;
     public float backwardSpeed = 4f;
     public float strafingSpeed = 7f;
     public float runSpeed = 20f;
     
     
     public float slideSpeed = 10f;
     
     public Vector3 moveVector {get; set;}
     
     //Gravity Variables
     public float Gravity = 21f;
     public float terminalVelocity = 20f;
     public float verticalVelocity {get; set;}
     // Jumping
     public float jumpSpeed = 6f;
     public bool isJumping = false;
     //Sliding
     public float slideThreshold = 0.6f;
     public float maxControllableSlideMagnitude = 0.4f;
     
     private Vector3 slideDirection;
     
     private GameObject hero;
     
     #region Get and Sets
     
     public GameObject Hero
     {
         get {return hero;}
         set {hero = value;}
     }
     
     #endregion
     
     void Awake() 
     {
         instance = this;
     }
     
     void Start()
     {
     /*
      * HIDE THE CURSOR WHEN TESTING AND STUFF
     */
         Screen.showCursor = false; 
         
     }
     
     public void UpdateMotor() 
     {
         SnapAllignCharacterWithCamera();
         Process_Motion();
     }
     
     // Gravity Functions
     
     void ApplyGravity()
     {
         if (moveVector.y > -terminalVelocity)
             moveVector = new Vector3(moveVector.x, moveVector.y - Gravity * Time.deltaTime, moveVector.z);
         
         if (TP_Controller.characterController.isGrounded && moveVector.y < -1)
         {
             moveVector = new Vector3(moveVector.x, -1, moveVector.z);
         }
     }
     
     void ApplySlide() //Causes your character to slide
     {
         if (!TP_Controller.characterController.isGrounded)
             return;
             
         slideDirection = Vector3.zero;
         
         if (slideDirection.magnitude < maxControllableSlideMagnitude) //You still have control of your character when sliding
         {
             moveVector += slideDirection;
         }
         else // You don't have any control of your character while sliding
         {
             moveVector = slideDirection;
         } 
     }
     
     public void Jump()
     {
         if (TP_Controller.characterController.isGrounded)
             verticalVelocity = jumpSpeed;
     }
     
     void Process_Motion()
     {
         //Transform our moveVector into WorldSpace relative to character rotation
         moveVector = transform.TransformDirection(moveVector);
         
         //Normalize moveVector if Magnitude > 0
         
         if (moveVector.magnitude > 1)
             moveVector = Vector3.Normalize(moveVector);
         
         //Apply slide if applicable
         
         ApplySlide ();
         
         //Multiply Magnitude for vector based on moveSpeed
         
         moveVector *= MoveSpeed();
         
         //Reapply Vertical Velocity to moveVector.y
         
         moveVector = new Vector3(moveVector.x,verticalVelocity, moveVector.z);
         
         //Apply Gravity
         
         ApplyGravity();
         
         //Move Character in World Space
         TP_Controller.characterController.Move (moveVector * Time.deltaTime);
         
         //Turn Character Towards Mouse LookAt
         if (TP_Animator.instance.moveDirection == TP_Animator.Direction.stationary)
         {
         Turn_Character_Towards_Mouse();
         }
     }
     
     //Moves Camera to the Character
     void SnapAllignCharacterWithCamera()
     {
         if (moveVector.x != 0 || moveVector.z != 0)
         {
             //Character X values, Character Z values, Camera Y Values
             transform.rotation = Quaternion.Euler(transform.eulerAngles.x, Camera.mainCamera.transform.eulerAngles.y, transform.eulerAngles.z);
         }
     }
     
     float MoveSpeed()
     {
         
         var moveSpeed = 0f;
         
         switch(TP_Animator.instance.moveDirection)
         {
         case TP_Animator.Direction.stationary: //Stationary
                 moveSpeed = 0;
                 break;
         case TP_Animator.Direction.forward:  //Forward
                 moveSpeed = forwardSpeed;
                 break;
         case TP_Animator.Direction.backward:    //Backward
                 moveSpeed = backwardSpeed;
                 break;    
         case TP_Animator.Direction.left:
                 moveSpeed = strafingSpeed;
                 break;
         case TP_Animator.Direction.right:
                 moveSpeed = strafingSpeed;
                 break;
         case TP_Animator.Direction.leftForward:
                 moveSpeed = forwardSpeed;
                 break;
         case TP_Animator.Direction.rightForward:
                 moveSpeed = forwardSpeed;
                 break;
         case TP_Animator.Direction.leftBackward:
                 moveSpeed = backwardSpeed;
                 break;
         case TP_Animator.Direction.rightBackward:
                 moveSpeed = backwardSpeed;
                 break;
         case TP_Animator.Direction.runForward:
                 moveSpeed = runSpeed;
                 break; 
         
         }
         if (slideDirection.magnitude > 0)
         {
             moveSpeed = slideSpeed;
         }
         
         return moveSpeed;
     }
     
     public void Turn_Character_Towards_Mouse()
     {
         if (TP_Camera.instance.isAiming)
         {
           Vector3 MouseWorldPosition = Camera.mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
           Hero.transform.LookAt(MouseWorldPosition);
            Hero.transform.rotation = Quaternion.Euler(new Vector3(0, (hero.transform.rotation.eulerAngles.y - 180f), 0));
         }
         
     }
     
     
 }

 //TP_Animator (This would be my second guess)
 
 public class TP_Animator : MonoBehaviour 
 {    
     public static TP_Animator instance;
     
     public enum Direction {stationary, forward, backward, left, right, 
                             leftForward, rightForward, leftBackward, rightBackward, runForward, falling}
     
     public Direction moveDirection {get; set;}
 
 void Awake()
     {
         instance = this;
     }
     
     public void DetermineCurrentMoveDirection()
     {
         
         var _forward = false;
         var _backward = false;
         var _left = false;
         var _right = false;
         if (TP_Motor.instance.moveVector.z > 0)
         {
             _forward = true;
         }
         if (TP_Motor.instance.moveVector.z < 0)
         {
             _backward = true;
         }
         if (TP_Motor.instance.moveVector.x > 0)
         {
             _left = true;
         }
         if (TP_Motor.instance.moveVector.x < 0)
         {
             _right = true;
         }
         
         if (_forward)
         {
             if (_left)
             {
                 moveDirection = Direction.leftForward;
                 //Animation
                 
                 //animation.CrossFade(WalkForwardRight.name, .1f);
                 
     
                 // *Sound*
             }
             else if (_right)
             {
                 moveDirection = Direction.rightForward;
                 //Animation
                 
         
                 // *Sound*
             }
             else if (Input.GetKey (KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) //RUNNING
             {
                 moveDirection = Direction.runForward;
                 
                 //animation.CrossFade(Run.name, .1f);
             }
             else
             {
                 moveDirection = Direction.forward;
                 //Animation
 
                 
                 // *Sound*
             }
         }
         else if(_backward)     
         {
             if (_left)
             {
                 moveDirection = Direction.leftBackward;
                 //Animation
                 
                 
     
                 // *Sound*
             }
             else if (_right)
             {
                 moveDirection = Direction.rightBackward;
                 //Animation
                 
                 // *Sound*
             }
             else 
             {
                 moveDirection = Direction.backward;
                 //Animation
                 
                 
                 // *Sound*
             }
         }
         else if (_left)
         {
             moveDirection = Direction.left;
             //Animation
                     
             // *Sound*
         }
         else if (_right)
         {
             moveDirection = Direction.right;
             //Animation
 
             // *Sound*
         }
 
         
     }


public class TP_Controller : MonoBehaviour { public static CharacterController characterController; //Holds reference of this Character Controller public static TP_Controller instance;

     private bool _isRunning;
 
     #region Setters and Getters
     
     public bool isRunning
     {
         get{return _isRunning;}
         set{_isRunning = value;}
     }
     
     
     
     #endregion
     // Use this for initialization
     void Awake () 
     {
         characterController = GetComponent("CharacterController") as CharacterController;
         instance = this;
         
         TP_Camera.UseExistingOrCreateNewMainCamera();
     }
     
     // Update is called once per frame
     void Update() 
     {
         if (Camera.mainCamera == null)
             return;
         
         GetLocomotionInput();
         
         HandleActionInput();
         
         //Adjust jump animations
         
         //Access TP_Motor UpdateMotor
         TP_Motor.instance.UpdateMotor();
 
     }
     
     void GetLocomotionInput()
     {
         var deadZone = .01f;
         
         TP_Motor.instance.verticalVelocity = TP_Motor.instance.moveVector.y;
         
         TP_Motor.instance.moveVector = Vector3.zero;
 
         
         //Vertical
         if (Input.GetAxis ("Vertical") > deadZone || Input.GetAxis ("Vertical") < deadZone)
             TP_Motor.instance.moveVector += new Vector3(0, 0, Input.GetAxis("Vertical"));
         
         
         // Horizontal 
         if (Input.GetAxis ("Horizontal") > deadZone || Input.GetAxis ("Horizontal") < deadZone)
             TP_Motor.instance.moveVector += new Vector3(Input.GetAxis("Horizontal"), 0, 0);
         
         TP_Animator.instance.DetermineCurrentMoveDirection();
     }
     
     #region Specialty Actions (Jump)
     
     void Jump()
     {
         //Cause the character to jump
         TP_Motor.instance.Jump();
         
         //Add Effects / Sounds / etc
     }
     
     
     
     
     #endregion
     
     void HandleActionInput()
     {
         if (Input.GetKeyDown (KeyCode.Space))
         {
             Jump ();
         }
         if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
         {
             _isRunning = true;
         }
         else if (!Input.GetKey (KeyCode.LeftShift) || !Input.GetKey(KeyCode.RightShift))
         {
             _isRunning = false;
         }
     
     }
 }
 public class TurnChecker: MonoBehaviour 
 {
 
     public Transform cubeLookAt;
     public Transform capsuleLookAt;
     
     public bool cubeTurn;
     public bool capsuleTurn;
     
     // Use this for initialization
     void Awake()
     {
         cubeTurn = true;
         cubeLookAt = GameObject.Find("cubeLookAt").transform;
         
         capsuleLookAt = GameObject.Find ("capsuleLookAt").transform;
     
     }
     
     // Update is called once per frame
     void Update () 
     {
         ValidateCurrentCharacter();
         
         if (Input.GetKeyDown(KeyCode.B))
         {    
             
             ChangeCharacters ();
     
         }
 }
 
 void ChangeCharacters()
     {
         if (cubeTurn == true)
             {
 
                 TP_Camera.instance.TargetLookAt = capsuleLookAt.transform;
                 TP_Motor.instance.Hero = GameObject.Find("Capsule");
                 cubeTurn = false;
                 capsuleTurn = true;
             }
         
         else  if (capsuleTurn == true)
             {
                 TP_Camera.instance.TargetLookAt = cubeLookAt.transform;
                 TP_Motor.instance.Hero = GameObject.Find ("Cube");
                 cubeTurn = true;
                 capsuleTurn = false;
         }
     }
     
     void ValidateCurrentCharacter()
     {
         if (cubeTurn == true && TP_Camera.instance.Hero != GameObject.Find ("Cube"))
             {
                 TP_Camera.instance.Hero = GameObject.Find ("Cube");
                 Debug.Log (TP_Camera.instance.Hero);
             }    
         if (capsuleTurn == true && TP_Camera.instance.Hero != GameObject.Find ("Capsule"))
             {
                 TP_Camera.instance.Hero = GameObject.Find("Capsule");
                 Debug.Log (TP_Camera.instance.Hero);
             }
     }
 }
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 Lairinus · Nov 20, 2012 at 09:00 PM 0
Share

Well, answered my own question on this one..

If anyone has a similar question, the way I did it was by making two extra variables in my TurnChecker script.

cubeCharController

capsuleCharController

From there I kept switching the primary variable from TP_Controller with the character's turn that it was. So now when I hit B, i continually shift from character to character with teh ability to move one without moving the other.

The only problem as of now is that the characters rotate to face each other, though it won't really pose that much of an issue to fix.

0 Replies

· Add your reply
  • Sort: 

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

9 People are following this question.

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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Disable Camera Movement,Stop camera movement 0 Answers

Multiple Cars not working 1 Answer

Choppy Camera Follow 1 Answer

Regarding transform.position in the roll a ball tutorial 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