- Home /
Character Control Auto-Move
Hello community!
I've been working on a script recently, and it's not working for me, i want to be able to make the character continually go in a forward direction when they press the keys "ctrl" and "f" and disable when they press another combination.
i'm not too worried about turning the auto-move off right now since.... it doesn't turn on.
(my new script is below)
also, that isn't my full MoveAround script.... but that's all the parts of my script that control auto-movement.
yes, i am using the Tornado Twins moving script, but i've modified it so much....that the full script is hardly recognizable as their tutorial's MoveAround script. but, you know, it still is their script in a way. and they helped me ALOT.
thanks for any help!!!
//shooting static var UseBetterBullet = false;
//dying static var dead = false;//1st time dying static var Dead = false;//2nd time dying private var Respawn = true;//first respawn static var money = 0; static var SCORE = 0; var constant = false; var cont = false; private var auto = false; private var movement : Vector3 = Vector3.zero;
static var Player1 = true; var bullitPrefab : Transform; var respawn1 : Vector3; var respawn2 : Vector3;
var RespawnToHere : Vector3;
static var DSN = false;
//geting hit static var gotHit = false; static var IsPlayer = true;
//respawn function LateUpdate() { if(PlayerHeliPlayer.DoneFlying) { transform.position = RespawnToHere; PlayerHeliPlayer.DoneFlying = false; }
if(DSN)
{
// activates the game object.
gameObject.active = true;
}
if(Dead && !Respawn)
{
transform.position = respawn2;
Dead = false;
print("i respawned yet again!");
}
if(dead && Respawn)
{//where the player respawns
transform.position = respawn1;
dead = false;
print("i respawned!");
}
}
function OnTriggerEnter( hit : Collider ) { if(hit.gameObject.tag == "SuddenDeath1") { enemyFollow.MOVE1 = false; enemyFollow.MOVE2 = false; enemyFollow.MOVE3 = true; }
if(hit.gameObject.tag == "TurretSlaught2")
{
enemyFollow.MOVE1 = false;
enemyFollow.MOVE2 = true;
enemyFollow.MOVE3 = false;
}
if(hit.gameObject.tag == "MoneyMaker")
{
money += 50;
Destroy(hit.gameObject);
print("I have " +money+ " Dollars!");
}
if(hit.gameObject.tag == "fallout3" && !Respawn)
{
Dead = true;
HealthControl.LIVES -= 1;
}
if(hit.gameObject.tag == "fallout2")
{
Respawn = false;
}
//when the player falls off (this doesnt really work too good. )
if(hit.gameObject.tag == "fallout")
{
dead = true;
//subtract life here
HealthControl.LIVES -= 1;
print("i fell off...");
}
//getting hit (this works)
if(hit.gameObject.tag == "enemyProjectile")
{
gotHit = true;
HealthControl.HITS += 1;
Destroy(hit.gameObject);
print("i got hit!");
}
//manipulating weather
if(hit.gameObject.tag == "weather")
{
ParticleEmitToggle.WCONTROL = true;
print("Now i can control weather.");
}
//level select button controls
if(hit.gameObject.tag == "Level2")
{
GUIshader.Level2 = true;
print("hit level two");
}
if(hit.gameObject.tag == "Level3")
{
GUIshader.Level3 = true;
print("hit level three");
}
if(hit.gameObject.tag == "Level4")
{
GUIshader.Level4 = true;
print("hit level four");
}
if(hit.gameObject.tag == "Level5")
{
GUIshader.Level5 = true;
print("hit level five");
}
if(hit.gameObject.tag == "WORK")
{
MovingPlatform4.work = true;
}
}
/// This script moves the character controller forward /// and sideways based on the arrow keys. /// It also jumps when pressing space. /// Make sure to attach a character controller to the same game object. /// It is recommended that you make only one call to Move or SimpleMove per frame.
var speed : float = 6.0; var jumpSpeed : float = 8.0; var gravity : float = 20.0; var Character : Transform; private var moveDirection = movement;
function Update() { var controller : CharacterController = GetComponent(CharacterController); if (controller.isGrounded) { if(auto) { movement = transform.forward speed; print("Automatic"); } else { movement = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); movement = transform.TransformDirection(movement) speed; }
//jumping
if (Input.GetButton ("Jump")) {
movement.y = jumpSpeed;
print("I'm jumping!");
}
}
// Apply gravity
movement.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(movement * Time.deltaTime);
//main menu button
if(Input.GetKeyDown("escape"))
{
Application.LoadLevel("Main Menu");
print("Main Menu Loading...");
}
//shooting
if(Input.GetKeyDown("f"))
{
if(Input.GetKey("left shift"))
{
auto = true;
print("auto");
}
if(Input.GetKey("right shift"))
{
auto = false;
print("!auto");
}
if(!UseBetterBullet && !constant)
{
var bullit = Instantiate(bullitPrefab, gameObject.Find("spawnPoint").transform.position,
Quaternion.identity);
bullit.tag = "wormProjectile";
//shoots it forward
bullit.rigidbody.AddForce(transform.forward * 3500);
}
if(UseBetterBullet)
{
particleEmitter.emit = true;
Invoke("DestroyEmitter",1);
}
}
//prints your score
print("Score: "+SCORE);
//changes your score
switch(SCORE)
{
case 10:
Application.LoadLevel("Main Menu");
break;
}
} function DestroyEmitter() { if(Time.time*1.5 > 7.5) particleEmitter.emit = false; }
@script RequireComponent(CharacterController)
Answer by skovacs1 · Nov 26, 2010 at 03:48 PM
There's a lot of stuff here and I'm not sure which parts you still need, but to do something on key-press (ctrl+F) would be something like:
if(Input.GetKey("left ctrl") && Input.GetKeyDown("f")) DoSomething();
You have this object with a character controller and Character with a Rigidbody. If this script is all to move the same character, you really shouldn't be trying to use both.
So your script modified would be something like:
var controller : CharacterController; var speed : float = 6.0; var jumpSpeed : float = 8.0; var gravity : float = 20.0; var bulletPrefab : Transform; private var auto : boolean = false; private var movement : Vector3 = Vector3.zero; private var spawnPoint : Transform;
function Start() { controller = GetComponent(CharacterController); }
function Update() { if(Input.GetKeyDown("f")) { //auto if(Input.GetKey("left ctrl")) auto = !auto; else { //shooting //If your spawnpoint doesn't change, you could move this //to start since it will always find the same one or //you could just store a reference to the Transform. //Also GameObject.Find is very slow/expensive so you are //better off tagging and using FindWithTag in stead spawnPoint = GameObject.FindWithTag("SpawnPoint").transform; var bullet : Transform = Instantiate(bulletPrefab, spawnPoint.position, spawnPoint.rotation); //If you tag the prefab, you don't need this //bullet.tag = "wormProjectile"; //If you set this on the prefab, you don't need this //bullet.rigidbody.useGravity = false; bullet.rigidbody.AddForce(transform.forward 3500); } } if (controller.isGrounded) { //movement if(auto) movement = transform.forward speed; else { movement = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); movement = transform.TransformDirection(movement) * speed; }
//jumping
if(Input.GetButton("Jump")) movement.y = jumpSpeed;
}
// Apply gravity
movement.y -= gravity * Time.deltaTime;
//apply movement
controller.Move(movement * Time.deltaTime);
//main menu button
if(Input.GetKeyDown("escape"))
Application.LoadLevel("Main Menu");
}
i tried converting my script into your example as best i could, and the print statements i added aren't even showing up, what's wrong?
It's because you want to use ctrl and you are testing in the editor. When ctrl+some$$anonymous$$ey is entered in the editor, the editor thinks it's a shortcut for the editor. In your built applications, this won't be a problem.
ok. thanks. i'll take your word for it, since it sounds true.
You don't have to if you don't want to. You can test it yourself. I tested it on my end with the exact code I answered with. In the editor, whenever I held ctrl, all other key input that I tested for was unresponsive. I built my scene with the exact code above, tested and it worked as it should. I replaced ctrl with shift and even it editor it worked. This implies the problem you describe is caused by the fact that it was running in the editor and it was s$$anonymous$$ling whatever came after ctrl.
yeah, i also just replaced ctrl with shift, and it worked! that's exactly what i wanted!
Your answer
![](https://koobas.hobune.stream/wayback/20220613084626im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help wanted -- How to make my character jump (using slightly modified Tornado Twins move script) 5 Answers
How to get a ai to drive to a raycast 0 Answers
Connect a objects Y axis to Slider C# 2 Answers
Character Controller follow mouse 2 Answers
How could I make this script run a bit smoother (iOS)? Any Ideas? 1 Answer