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 Jesus_Freak · Nov 26, 2010 at 02:24 PM · movetornado-twins

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)

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

1 Reply

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

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");

}

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 Jesus_Freak · Nov 26, 2010 at 06:54 PM 0
Share

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?

avatar image skovacs1 · Nov 26, 2010 at 07:36 PM 1
Share

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.

avatar image Jesus_Freak · Nov 26, 2010 at 08:38 PM 0
Share

ok. thanks. i'll take your word for it, since it sounds true.

avatar image skovacs1 · Nov 26, 2010 at 09:17 PM 1
Share

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.

avatar image Jesus_Freak · Nov 27, 2010 at 12:01 AM 0
Share

yeah, i also just replaced ctrl with shift, and it worked! that's exactly what i wanted!

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

No one has followed this question yet.

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


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