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 Mattchuuu · Apr 30, 2012 at 10:21 PM · cameramovement

Camera/Movement Script not working

I looked around for a WoW like camera/movement system and found a thread with dozens of examples. I took one, copied it and it worked beautifully until I tried right clicking. In WoW when you right-click the character will follow wherever the mouse is, however this one does nothing, and I am met with a null reference exception. I looked throughout that entire thread but nobody had a problem with it.

Here's the part that's wrong :

    // Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.    
 if(Input.GetMouseButton(1)) {    
       transform.rotation = Quaternion.Euler(Camera.main.transform.eulerAngles.x,Camera.main.transform.eulerAngles.y,0);    
 }     
 
    else {
     transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);   
    }


Here's the entire script if needed:

 // Movement Variables:
 
 var jumpSpeed:float = 9.0;
 
 private var gravity:float = 20.0;
 
 static var runSpeed:float = 4.0;
 
 static var swimSpeed:float = 2.0;
 
 static var walkSpeed:float = 1.7;
 
 static var runSpeedsave = 4.0;
 
 static var walkSpeedsave = 3.7;
 
 static var curSpeed:float = 0.0;
 
 private var rotateSpeed:float = 150.0;
 
 private var grounded:boolean = false;
 
 private var moveDirection:Vector3 = Vector3.zero;
 
 static var isWalking:boolean = false;
 
 private var moveStatus:String = "idle";
 
 private var xSpeed = 250.0;
 
 private var ySpeed = 120.0;
 
 private var yMinLimit = -40;
 
 private var yMaxLimit = 80;
 
 private var x = 0.0;
 
 private var y = 0.0;
 
 static var ddistance:float = 0.0;
 
 static var strafemod:Vector3 = Vector3.zero;
 
 static var strafing = false;
 
 static var animationspeed = 1.0;
 
 var hit : RaycastHit;
 
 static var isSwimming = false;
 
 static var bmove = false;
 
 static var bSpeed = 0.0;
 
 //
 
 //UPDATE 
 
 // 
 
    
 
 function Update () 
 
 //check if we fell down the world and teleport to a specific location
 
 {
 
 if(transform.position.y < -200)
 
     {
 
         transform.position.y = 16.07609;
 
         transform.position.x = 579.2826;
 
         transform.position.z = 130.8261;
 
     }
 
  
 
 //Move controller
 
 var controller:CharacterController = GetComponent(CharacterController);
 
 var flags = controller.Move(moveDirection * Time.deltaTime);
 
 grounded = (flags & CollisionFlags.Below) != 0; 
 
  
 
 //check if we're moving backwards
 
    if(Input.GetAxis("Vertical") < 0){
 
    bmove = true;
 
    }
 
    else{
 
    bmove = false;
 
    }
 
 //check if we're swimming
 
 if(isSwimming == true){
 
 swimSpeed = runSpeed/2;
 
    //reduce speed when moving backwards
 
      if(bmove == true){
 
          if(bSpeed > 0){
 
      bSpeed = bSpeed;
 
      }
 
      else{
 
      bSpeed = swimSpeed;
 
      swimSpeed = bSpeed * 0.6;
 
      }
 
      }
 
    else{
 
      if(bSpeed > 0){
 
      swimSpeed = bSpeed;
 
      bSpeed = 0.0;
 
      }
 
      }
 
        
 
       moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical"));
 
       if(Input.GetMouseButton(1) && Input.GetAxis("Vertical") && Input.GetAxis("Horizontal")) {
 
          moveDirection *= .7;
 
          }
 
     animationspeed = runSpeed/4;
 
     moveDirection = transform.TransformDirection(moveDirection);
 
     moveDirection *= swimSpeed;
 
        
 
      moveStatus = "swim_idle";
 
       if(moveDirection != Vector3.zero) {
 
         moveStatus = "swimming";
 
 //invoke swim animation here         
 
 }
 
       else {
 
 //swim idle animation
 
       }
 
       // Jump (or rather dive upwards)!
 
       if(Input.GetButton("Jump"))
 
       {
 
          // call JUMP animation here
 
          moveDirection.y = swimSpeed*1.6;
 
       }
 
    
 
    // Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
 
    if(Input.GetMouseButton(1)) {
 
       transform.rotation = Quaternion.Euler(Camera.main.transform.eulerAngles.x,Camera.main.transform.eulerAngles.y,0);
 
    } 
 
    else {
 
      transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
 
    }
 
    //simple strafing
 
    //now let's do the quick check if we're walking or running
 
    //walking goes here
 
    //left strafe button is pressed -> strafe to the left
 
    if (Input.GetButton("Strafel")){
 
    strafemod = new Vector3(-1, 0, 0);
 
    strafemod = transform.TransformDirection(strafemod);
 
    strafemod = swimSpeed * strafemod;
 
    controller.Move (strafemod * Time.deltaTime);
 
    }
 
 //right strafe button is pressed -> strafe to the right
 
 if (Input.GetButton("Strafer")){
 
    strafemod = new Vector3(1, 0, 0);
 
    strafemod = transform.TransformDirection(strafemod);
 
    strafemod = swimSpeed * strafemod;
 
    controller.Move (strafemod * Time.deltaTime);
 
    }
 
    }
 
    else{
 
  
 
 //check if we're moving 
 
    if(Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0 || Input.GetButton("Strafel") || Input.GetButton("Strafer")){
 
    if(isWalking == true){
 
    curSpeed = walkSpeed;
 
    }
 
    else{
 
    curSpeed = runSpeed;
 
    }
 
    }
 
    else{
 
    curSpeed = 0.0;
 
  
 
 //since the backward slowing can have some permanent effects to our runSpeed and walkSpeed (i.e. the speed will stick to 60%) here's some failsave code making use of the run/walk Speedsave variables
 
      
 
    if(runSpeed < runSpeedsave){
 
    runSpeed = runSpeedsave;
 
    }
 
    
 
  
 
    if(walkSpeed < walkSpeedsave){
 
    walkSpeed = walkSpeedsave;
 
    }
 
    }
 
  
 
    
 
    //reduce speed when moving backwards
 
    if(isWalking == true){
 
    
 
    if(bmove == true){
 
         if(bSpeed > 0){
 
         bSpeed = bSpeed;
 
         }
 
         else{
 
         bSpeed = walkSpeed;
 
         walkSpeed = bSpeed * 0.6;
 
         }
 
                             }
 
    else{
 
         if(bSpeed > 0){
 
         walkSpeed = bSpeed;
 
         bSpeed = 0.0;
 
         }
 
     }
 
     }
 
    else{
 
     if(bmove == true){
 
    if(bSpeed > 0){
 
    bSpeed = bSpeed;
 
    }
 
    else{
 
    bSpeed = runSpeed;
 
    runSpeed = bSpeed * 0.6;
 
    }
 
    }
 
    else{
 
    if(bSpeed > 0){
 
    runSpeed = bSpeed;
 
    bSpeed = 0.0;
 
    }
 
    }
 
    }
 
    
 
    //check if we're moving - if we're moving track distance and save to ddistance
 
    if(curSpeed > 0){
 
    ddistance = ddistance + (curSpeed * Time.deltaTime);
 
    }
 
    //
 
    // Only allow movement and jumps while -----------------  GROUNDED -------------
 
     
 
    if(grounded) {
 
    moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical"));
 
       if(Input.GetMouseButton(1) && Input.GetAxis("Vertical") && Input.GetAxis("Horizontal")) {
 
          moveDirection *= .7;
 
          }
 
     animationspeed = runSpeed/4;
 
     moveDirection = transform.TransformDirection(moveDirection);
 
       moveDirection *= isWalking ? walkSpeed : runSpeed;
 
        
 
       moveStatus = "idle";
 
       if(moveDirection != Vector3.zero) {
 
          moveStatus = isWalking ? "walking" : "running";
 
          if (isWalking){
 
            // invoke WALK animation here
 
          } else {
 
     // call RUN animation here
 
          }
 
       } else {
 
         // call IDLE animation here
 
       }
 
       // Jump!
 
       if(Input.GetButton("Jump"))
 
       {
 
          // call JUMP animation here
 
          moveDirection.y = jumpSpeed;
 
       }
 
     }                   
 
     // END "IS GROUNDED"
 
    
 
    // Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
 
     if(Input.GetMouseButton(1)) {
 
       transform.rotation = Quaternion.Euler(Camera.main.transform.eulerAngles.x,Camera.main.transform.eulerAngles.y,0);
 
    } 
 
    else {
 
      transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
 
    }
 
    
 
    // Toggle walking/running with the walk/run key
 
    if(Input.GetButtonDown("walk"))
 
          isWalking = !isWalking;
 
        
 
    //Apply gravity
 
    moveDirection.y -= gravity * Time.deltaTime;
 
    //Move controller
 
    //var controller:CharacterController = GetComponent(CharacterController);
 
    //var flags = controller.Move(moveDirection * Time.deltaTime);
 
    grounded = (flags & CollisionFlags.Below) != 0;
 
    
 
    //simple strafing
 
    //now let's do the quick check if we're walking or running
 
    //walking goes here
 
    if(isWalking == true){
 
    //left strafe button is pressed -> strafe to the left
 
    if (Input.GetButton("Strafel")){
 
    strafemod = new Vector3(-1, 0, 0);
 
    strafemod = transform.TransformDirection(strafemod);
 
    strafemod = walkSpeed * strafemod;
 
    controller.Move (strafemod * Time.deltaTime);
 
    }
 
 //right strafe button is pressed -> strafe to the right
 
 if (Input.GetButton("Strafer")){
 
    strafemod = new Vector3(1, 0, 0);
 
    strafemod = transform.TransformDirection(strafemod);
 
    strafemod = walkSpeed * strafemod;
 
    controller.Move (strafemod * Time.deltaTime);
 
    }
 
  
 
    }
 
    //running goes here
 
    else{
 
    //left strafe button is pressed -> strafe to the left
 
 if (Input.GetButton("Strafel")){
 
    strafemod = new Vector3(-1, 0, 0);
 
    strafemod = transform.TransformDirection(strafemod);
 
    strafemod = runSpeed * strafemod;
 
    controller.Move (strafemod * Time.deltaTime);
 
    }
 
   //left strafe button is pressed -> strafe to the right
 
 if (Input.GetButton("Strafer")){
 
    strafemod = new Vector3(1, 0, 0);
 
    strafemod = transform.TransformDirection(strafemod);
 
    strafemod = runSpeed * strafemod;
 
    controller.Move (strafemod * Time.deltaTime);
 
    }   
 
    }
 
    }
 
    };
 
    
 
    static function ClampAngle (angle : float, min : float, max : float) {
 
    if (angle < -360)
 
       angle += 360;
 
    if (angle > 360)
 
       angle -= 360;
 
    return Mathf.Clamp (angle, min, max);
 
 }
 
  
 
  
 
 // -------------------------------------------------------------------------------------------------------------
 
 // ----------------------------------------------------------- END UPDATE  --------------------------------
 
 // -------------------------------------------------------------------------------------------------------------
 
  
 
 @script RequireComponent(CharacterController)




What could be wrong?

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 Seth-Bergman · Apr 30, 2012 at 10:29 PM 0
Share

it would help to know what the null reference is in reference to.. is your camera tagged "$$anonymous$$ain Camera"?

1 Reply

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

Answer by Seth-Bergman · Apr 30, 2012 at 10:35 PM

Gonna go out on a limb and assume the camera you're using is not tagged as "Main Camera", as that's basically the only thing that could be returning a null reference directly off of the button push.

Comment
Add comment · Show 1 · 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 Mattchuuu · Apr 30, 2012 at 11:07 PM 0
Share

HAHAHA, my goodness the time I spent looking for an answer to this and it was that simple. Thank you sir, I'll have to make sure my checking is more thorough next time!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

UNITY 3D: How to make the camera follow the player? Smoothly 2 Answers

How do I add a camera bounce effect every time the player lands? 0 Answers

RTS Mouse Click Movement 1 Answer

Slow camera move speed? 2 Answers

Orient GameObject rotation to rotation of Main Camera 0 Answers


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