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 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
Add comment
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

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

3 People are following this question.

avatar image avatar image avatar image

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

How to make my Camera Controller look at target 0 Answers

Simple C# script to move a gameObject toward the direction of the player at the instance of it's spawning. 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