- Home /
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
}
}
}
}
}
}
Please be so kind to leave some feedback, let us know if your issue has been resolved. :D
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!");
}
}
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?
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.
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?
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
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.
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;
}
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
Distribute terrain in zones 3 Answers
How can I make an object in my scene jump continuously? 1 Answer