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 Christina1 · Aug 08, 2013 at 10:54 PM · c#movementdisable

Disabling Character movement in C#

Hi,

I have a C# script controlling my raycasting and I'm not very good with C#. I want to be able to disable character movement when the ray hits certain objects. I'm currently using the script below to disable the Character Motor script.

                         GameObject.Find("Player").GetComponent<CharacterMotor>().enabled = false;

But this just comes up with an error. I think my syntax might be wrong? But I don't know how to fix it. Does anyone know if there is a better way to disable the Character Motor script temporarily?

Thanks!

Christina

Edit

The specific error is:

"Assets/Standard Assets/Script/Raycast01.cs(30,80): error CS0246: The type or namespace name `CharacterMotor' could not be found. Are you missing a using directive or an assembly reference?"

But I don't understand what that means exactly.

This is the full script

 using UnityEngine;
 using System.Collections;
 
 public class Raycast01 : MonoBehaviour {
 public GameObject target;
 public AudioClip sound1;
 
     // Use this for initialization
     void Start () {
     Screen.lockCursor = true;        //Lock cursor to the center of the screen
     }
     
     // Update is called once per frame
     void Update () {
     
     Ray ray=Camera.mainCamera.ScreenPointToRay(Input.mousePosition); //Generate Ray
     Debug.DrawRay(ray.origin, ray.direction * 10, Color.cyan);        //Debug by showing a blue ray
     RaycastHit hit;                                                    //Ray hit variable named "hit"
 
 
         if(Physics.Raycast(ray, out hit) == true){                    //If the ray is hitting something
             if(hit.collider.gameObject == target)                    //If that something is the target
                 if(hit.distance <= 2.0){                            //If that target is within 2m
                     if(Input.GetMouseButton(0)){                    //And if the mouse is down
                         Screen.lockCursor=false;                    //Unlock the cursor
                         audio.clip = sound1;                        //Play the audio
                         audio.Play ();
                         GameObject.Find("Player").GetComponent<MouseLook>().enabled = false; //Freeze the Camera motion
                     GameObject.Find("Player").GetComponent<CharacterMotor>().enabled = false;//Freeze the player motion
                 }
             
                     else{                                            //Else if the button is not pressed
                     Screen.lockCursor=true;                            //Mouse locked
                     if(!GameObject.Find ("Player").GetComponent<MouseLook>().enabled){        //If Camera is still frozen
                     GameObject.Find("Player").GetComponent<MouseLook>().enabled = true;        //Unfreeze it
                     }
                 }
             }
         }
                 
     }
 }


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 clunk47 · Aug 13, 2013 at 02:15 AM 1
Share

Please be so kind to leave some feedback, let us know if your issue has been resolved. :D

3 Replies

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

Answer by clunk47 · Aug 08, 2013 at 11:07 PM

Your line of code is fine. Are you getting a null ref exception? If so, make sure you have a CharacterMotor component actually attached to the "Player" GameObject. To check via script if there is a CharacterMotor attached:

 if(GameObject.Find("Player").GetComponent<CharacterMotor>())
 {
     GameObject.Find("Player").GetComponent<CharacterMotor>().enabled = false;
 }
 
 else
 {
     Debug.Log("No Motor Attached!");
 }

You can also define a GameObject for player instead of typing this line every time.

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour
 {
     GameObject player;
     
     void Start()
     {
         player = GameObject.Find ("Player");
         
         if(player.GetComponent<CharacterMotor>())
         {
             player.GetComponent<CharacterMotor>().enabled = false;
             Debug.Log ("Motor Disabled Successfully!");
         }
         
         else
             print ("No Motor Attached!");
     }
 }
Comment
Add comment · Show 5 · 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 Christina1 · Aug 08, 2013 at 11:20 PM 0
Share

This looks great but I still get the same error as above, it looks like it can't find the Character $$anonymous$$otor script?

Assets/Standard Assets/Script/Raycast01.cs(18,21): error CS0246: The type or namespace name `Character$$anonymous$$otor' could not be found. Are you missing a using directive or an assembly reference?

avatar image clunk47 · Aug 08, 2013 at 11:27 PM 1
Share

You need to go to Assets>Import Package>Character Controller, import that, then make sure you attach the Character$$anonymous$$otor script to your character.

avatar image Christina1 · Aug 08, 2013 at 11:39 PM 1
Share

The character controllers are all imported and the Character $$anonymous$$otor script is attached to the FPController. I can import them again if you think it would help?

avatar image clunk47 · Aug 09, 2013 at 12:04 AM 1
Share

Worth a try for troubleshooting's sake.

avatar image VoLkizz · Nov 10, 2014 at 07:52 PM 0
Share

Hello everyone! Have little deeper issue here. How to disable specific axis in input manager thru the script (please C# ). Trying to disable movement in Z direction on the character in the specific state. Thanks

avatar image
0

Answer by JuanseCoello · Aug 08, 2013 at 11:01 PM

try == null , or try an if statement with return; Maybe you can post more code for me to help you, for example, post more of the code of the character movement.

Comment
Add comment · 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
0

Answer by RyanZimmerman87 · Aug 08, 2013 at 11:43 PM

I don't think you should necessarily have to disable entire scripts or your "character motor" to solve this problem.

I handle this kind of thing in a few different ways in my current project:

1) For the objects you don't want your player to move to set that item to a different layer that your movement RayCast ignores.

2) Use variable conditions to simply tell the script to not move if the rayCast hits your desired objects. Depending on your script that could be as simple as a bool causing the script to return.

3) If it's for things like buttons you can set up custom areas (using Rect variable) on the screen for these buttons and use something like:

 if (healthButtonHitCheckRect.Contains(Input.mousePosition))
     {
                 
         rayBool = false;
         return;
     }

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

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

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How can I make an object in my scene jump continuously? 1 Answer

c# array of game objects movement help 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