- Home /
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);
}
}
}
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.
Your answer
Follow this Question
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