- Home /
Question by
Sindario · Dec 23, 2014 at 11:14 AM ·
c#movementcamera-movementcamera-look
Updating PC Control code for mouse camera movement Doesn't work Help!
// ----- PC Control Script for MS2307 workshop examples ------------------------
// ----- David Dorrinton, Oct 2012, uelgames.org ------------------------------
//------------------------------------------------------------------------------
// ----- Requirments: Character Controller
// ----- States: grounded
using UnityEngine;
using System.Collections;
public class DD_PC_CControl : MonoBehaviour {
//-------------------------------------------------------------------------
// ----- Declare Variables
public float fl_speed = 6.0F;
public float fl_jumpSpeed = 8.0F;
public float fl_gravity = 20.0F;
private Vector3 v3_moveDirection = Vector3.zero;
public float fl_health = 100.0F;
private Vector3 v3_direction;
private CharacterController cc_PC;
private GameObject Lev_Man;
private DD_Level_Manager LM_Script;
private string st_PC_GUI_text;
// Shoot Variables
public GameObject go_bullet;
private float fl_cooldown_time;
private float fl_cooldown = 0.5F;
private float fl_bullet_speed_modifier = 1.0F;
private float fl_bullet_range_modifier = 1.0F;
private float fl_bullet_damage_modifier = 1.0F;
private float fl_bullet_accuracy_modifier = 1.0F;
// Cam Postion
private float fl_min_cam_height = -1F;
private float fl_max_cam_height = 3F;
private float fl_cam_distance = -2.5F;
private GameObject go_cam;
private DD_Level_Manager lm_script;
private bool bl_climbing;
private float fl_original_gravity;
//-------------------------------------------------------------------------
// ----- Initialization
void Start () {
// Find the level manager script
Lev_Man = GameObject.Find("Level_Manager");
LM_Script = Lev_Man.GetComponent<DD_Level_Manager>();
// Find the Level Manager Empty Game Object in the scene and the Script to read game management data
lm_script = GameObject.Find("Level_Manager").GetComponent<DD_Level_Manager>();
// Set initial values and game states
v3_direction = Vector3.zero;
// Find the attached character controller to the PC
cc_PC = GetComponent<CharacterController>();
// Find Game Objects
go_cam = GameObject.Find("PC_Cam");
//Screen.lockCursor = true ;
fl_original_gravity = fl_gravity;
;
}// ---
//-------------------------------------------------------------------------
// ----- Frame Update
void Update() {
// -- Check PC Health
if (fl_health < 0){
LM_Script.st_message = "PC is Dead";
}
else{
// Alive Functions
MovePC();
Shoot();
}
} // ---
//-------------------------------------------------------------------------
// Fire Bullets
void Shoot (){
if (fl_cooldown_time < Time.realtimeSinceStartup && Input.GetButton("Fire1")){
// Create a new Bullet Object
GameObject clone;
clone = Instantiate(go_bullet ,transform.TransformPoint(Vector3.forward), transform.rotation) as GameObject;
// Modify the Bullet Parameters
clone.SendMessage("Modify_Speed" , fl_bullet_speed_modifier , SendMessageOptions.DontRequireReceiver);
clone.SendMessage("Modify_Range" , fl_bullet_range_modifier , SendMessageOptions.DontRequireReceiver);
clone.SendMessage("Modify_Damage" , fl_bullet_damage_modifier ,SendMessageOptions.DontRequireReceiver);
clone.SendMessage("Modify_Accuracy" , fl_bullet_accuracy_modifier ,SendMessageOptions.DontRequireReceiver);
// add current time to the cool down period + a random time
fl_cooldown_time = Time.realtimeSinceStartup + fl_cooldown ;
}
// Update GUI Text
st_PC_GUI_text = "PC Health\n----------------\n" + fl_health + "\n\nBullet Mods\n--------------";
st_PC_GUI_text += " \nSpeed " + fl_bullet_speed_modifier;
st_PC_GUI_text += " \nRange " + fl_bullet_range_modifier;
st_PC_GUI_text += " \nAccuracy " + fl_bullet_accuracy_modifier;
st_PC_GUI_text += " \nDamage " + fl_bullet_damage_modifier;
}// ----
//-------------------------------------------------------------------------
// Move PC
void MovePC(){
// Find the attached Character Controller
CharacterController controller = GetComponent<CharacterController>();
// Is the PC on the ground
if (controller.isGrounded) {
// add to movement vector based on KB input for up / down
v3_moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
// Convert world coords to local
v3_moveDirection = transform.TransformDirection(v3_moveDirection);
// multiply movement vector by speed
v3_moveDirection *= fl_speed;
// Rotate the PC based on LR input
transform.Rotate(0,Input.GetAxis("Horizontal"),0);
// Jump
if (Input.GetButton("Jump"))
v3_moveDirection.y = fl_jumpSpeed;
}
// Apply Gravity
v3_moveDirection.y -= fl_gravity * Time.deltaTime;
// Move the PC
controller.Move(v3_moveDirection * Time.deltaTime);
}// ---
//-------------------------------------------------------------------------
// React when sent hit by another object
void Hit (float _damage){
// Reduce health by damage
fl_health -= _damage;
}// ---
//-------------------------------------------------------------------------
// GUI Script
void OnGUI(){
GUI.Box(new Rect(Screen.width -100, 100, 90, 180), st_PC_GUI_text);
}// ---
//-------------------------------------------------------------------------
// --- When PC Hits Object Check and Update Stats
void OnControllerColliderHit(ControllerColliderHit CC_hit) {
if (CC_hit.gameObject.tag == "Bul_Speed_Up"){
fl_bullet_speed_modifier += 0.5f;
Destroy (CC_hit.gameObject);
}
if (CC_hit.gameObject.tag == "Bul_Range_Up"){
fl_bullet_range_modifier += 0.5f;
Destroy (CC_hit.gameObject);
}
if (CC_hit.gameObject.tag == "Bul_Accuracy_Up"){
fl_bullet_accuracy_modifier += 0.5f;
Destroy (CC_hit.gameObject);
}
if (CC_hit.gameObject.tag == "Bul_Damage_Up"){
fl_bullet_damage_modifier += 0.5f;
Destroy (CC_hit.gameObject);
}
} // ---
void OnTriggerStay(Collider cl_trigger_collider){
if (cl_trigger_collider.gameObject.tag =="Moving"){
transform.parent = cl_trigger_collider.transform;
}
}
void OnTriggerExit(Collider cl_trigger_collider){
if (cl_trigger_collider.gameObject.tag =="Moving"){
transform.parent = null;
}
}
} // -----
// Mouse Look ==================================================================================
void MovePC()
{
// Are we in a climb zone
if (bl_climbing) fl_gravity = -3; else fl_gravity = fl_original_gravity;
// If the run key pressed double the speed
if (Input.GetKey(KeyCode.LeftShift)) fl_speed = 12; else fl_speed = 6;
// Is the PC on some ground
if ( cc_PC.isGrounded )
{
// Rotate the PC based on Horizontal input (A,D or Cursor L,R)
transform.Rotate( 0, ( fl_turn_rate * Input.GetAxis("Horizontal") * Time.deltaTime), 0 );
// Add Z movement to the direction vector based on Vertical input (W,S or Cursor U,D)
v3_direction = new Vector3( 0, 0, Input.GetAxis("Vertical") );
// Convert world coordinates to local for the PC
v3_direction = fl_speed * transform.TransformDirection(v3_direction);
// if the jump key is pressed add jump force to the direction vector
if ( Input.GetButton("Jump") ) v3_direction.y = fl_jump_force;
// Check Fall Height
if (bl_PC_falling){
fl_PC_fallen_height = fl_fall_start - transform.position.y;
bl_PC_falling = false;
// Apply Damage from Fall
if (fl_PC_fallen_height > fl_fall_height_no_damage) fl_health -= 30;
if (fl_PC_fallen_height > fl_fall_height_death) fl_health -= 200;
}
}
else
{ // when the PC is in the air
// Reset fall height check
if(!bl_PC_falling)
{
fl_fall_start = transform.position.y;
bl_PC_falling = true;
}
// Add fl_gravity to the direction vector
v3_direction.y -= fl_gravity * Time.deltaTime;
}
// Move the character controller with the direction vecto
cc_PC.Move( v3_direction * Time.deltaTime );
}
// Mouse Look ==================================================================================
void MouseLook()
{
// Mouse Rotate
transform.Rotate (0, 3 * fl_turn_rate * Time.deltaTime * Input.GetAxis ("Mouse X") ,0);
// Move Cam up and Down
if ( Input.GetAxis("Mouse Y") < 0 && go_cam.transform.localPosition.y < fl_max_cam_height ) go_cam.transform.Translate(0, 3 * Time.deltaTime ,0);
if ( Input.GetAxis("Mouse Y") > 0 && go_cam.transform.localPosition.y > fl_min_cam_height ) go_cam.transform.Translate(0,-3 * Time.deltaTime ,0);
// look at PC Object
go_cam.transform.LookAt(transform.position + new Vector3(0,1,0));
// Move the Camera
go_cam.transform.localPosition = new Vector3 (go_cam.transform.localPosition.x, go_cam.transform.localPosition.y, fl_cam_distance);
if (Input.GetAxis("Mouse ScrollWheel") < 0) fl_cam_distance -= 0.1F;
if (Input.GetAxis("Mouse ScrollWheel") > 0) fl_cam_distance += 0.1F;
/*if (Input.GetKeyDown("o")){
if (fl_cam_distance < 0) fl_cam_distance = 1;
else fl_cam_distance = -2.5F;
}*/
}//======
Hello my problem is that For some reason my mouse look bit don't want to work it shows "a namespace can only contain types and namespace declarations" Is in the line code 221 and 286 those two voids shows this error please help. Thank you
Comment
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Multiple Cars not working 1 Answer
Free camera look question 2 Answers