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 Adytzu04 · Apr 10, 2013 at 08:55 PM · animationmovementmouse

Help with mouse and keyboard movement.

Hi everyone! So i downloaded an asset called Dungeons Hordes from AssetStore and i want to improve it. I made a menu, added some music, a nice GUI and i want to add the possibility to move with mouse click or mouse hold.

So this is the Player code as it was from asset store with some modifications

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 
 
 public class PlayerController : MonoBehaviour {
     public float movespeed=4;
     public float Damage=60;
     public Transform Cam;
     public float CamHeight=10;
     public float CamHeightPushback=5;
     public AnimationClip Run;
     public AnimationClip Idle;
     public AnimationClip Attack;
     public AnimationClip die;
     public float AttackSpeed=0.7f;
     private bool kill;
     public bool dead;
     private bool playd;
     private float atime;
     private bool dealdamage;
     public int TotalAICount;
     public bool YouWon;
     public bool YouLose;
     public bool quitMenu=false;
     private bool w;
     public GUISkin MenuSkin;
     
     
     public Vector3 For;
     public List<Transform> KillList;
     
     // Use this for initialization
     void Start () {
         if(Cam)Cam.parent=null;
         
         w=true;
         playd=true;
         
     }
     
     // Update is called once per frame
     void Update () {
         
         
         }
         else if(destinationDistance > .5f){            // To Reset Speed to default
             moveSpeed = 14;
             animation.CrossFade( Run.name, 0.15f); 
         
         
         //check to see it menu button esc is pressed
         if(Input.GetKeyDown(KeyCode.Escape)) {
           quitMenu = !quitMenu;
         }
         
         //Victory
         if(w){
         if(TotalAICount<=0){
                 YouWon=true;
             w=false;
                 YouLose=false;
             }
         }
         
         
         Health php=(Health)GetComponent("Health");
         if(php){
         if(php.CurrentHealth<=0)dead=true;
         }
         
         //DEATH
         if(dead){
             if(playd){
             animation.CrossFade( die.name, 0.15f);    
             }
             playd=false;
             YouLose =true;
         }
         else{
         
         For.y=270;
         //CAMERA POSITION
         Vector3 ch=transform.position;
         ch.y=transform.position.y+CamHeight;
         ch.x=transform.position.x+CamHeightPushback;
         
         Cam.transform.position=ch;
         if(Input.GetKeyUp(KeyCode.Space)){
             dealdamage=true;
         kill=true;    
         }
         
             //LETS DO SOME KILLIN!
         if(kill){
             atime+=Time.deltaTime;
             animation[Attack.name].speed = animation[Attack.name].length / AttackSpeed;
         animation.CrossFade( Attack.name, 0.15f);
             
             if(atime>=AttackSpeed*0.35f&atime<=AttackSpeed*0.48f){
             if(KillList.Count>0&dealdamage){
                 int    ls=KillList.Count;
                 for (int i = 0; i < ls; i++){
                     Health hp=(Health)KillList[i].transform.GetComponent("Health");
                             
                         hp.CurrentHealth=hp.CurrentHealth-Damage;
                             if(hp.Dead){}
                             else if(hp.CurrentHealth<=0)TotalAICount=TotalAICount-1;
                     }
                     dealdamage=false;
                 }
             }
             
             if(atime>=AttackSpeed){
                 kill=false;
                 atime=0;
             }
         }
         else{
                 //RUN ANIMATION IF ANY ARROW KEYS ARE PRESSED
         if(Input.GetKey(KeyCode.UpArrow)|Input.GetKey(KeyCode.DownArrow)|Input.GetKey(KeyCode.RightArrow)|Input.GetKey(KeyCode.LeftArrow)){
     
                 animation.CrossFade( Run.name, 0.15f);    
         }
         else{
         animation.CrossFade( Idle.name, 0.15f);        
         }
         }
         //MOVEMENT
     if(Input.GetKey(KeyCode.UpArrow)){
             transform.position += transform.forward * +movespeed * Time.deltaTime;
     Cam.transform.position += transform.forward * +(movespeed) * Time.deltaTime;
             transform.rotation=Quaternion.Euler(0, -90, 0);
     
         }
         else{
         
         if(Input.GetKey(KeyCode.DownArrow)){
             transform.rotation=Quaternion.Euler(0, 90, 0);
     transform.position += transform.forward * +movespeed * Time.deltaTime;
     Cam.transform.position += transform.forward * +(movespeed) * Time.deltaTime;
                 
         }
         else{
         
         if(Input.GetKey(KeyCode.RightArrow)){
             transform.rotation=Quaternion.Euler(0, 0, 0);
     transform.position += transform.forward * +movespeed * Time.deltaTime;
     Cam.transform.position += transform.forward * +(movespeed) * Time.deltaTime;
         
         }
         
         
         if(Input.GetKey(KeyCode.LeftArrow)){
             transform.rotation=Quaternion.Euler(0, -180, 0);
                     transform.position += transform.forward * +movespeed * Time.deltaTime;
     Cam.transform.position += transform.forward * +(movespeed) * Time.deltaTime;
         
         }
         }
         }
         }
     }
     
     void OnTriggerEnter(Collider other){
         //ADD AIS TO LIST TO INFLICT DAMAGE IF IN RANGE
         Health AI=(Health)other.transform.GetComponent("Health");
         if(AI){
         KillList.Add(other.transform);
             
         }
     }
     
     void OnTriggerExit(Collider other){
         //REMOVE FROM LIST WHEN OUT OF RANGE
         Health AI=(Health)other.transform.GetComponent("Health");
         if(AI){
         KillList.Remove(other.transform);
             
         }
     }
     
                
         
     void OnGUI(){
         
         //set menu skin
         GUI.skin = MenuSkin;
         
         //HEALTH BAR AND AI COUNT
         Health php=(Health)GetComponent("Health");
         if(php){
         float hpp=php.CurrentHealth;
         GUI.Label(new Rect(0, 30, 300, 33), "Health: "+hpp);
         }
         GUI.Label(new Rect(0, 67, 300, 33), "Evil Skellies Left: "+TotalAICount);
         
     
             
         //YOU WON!
         if(YouWon){ 
          GUI.Box(new Rect(Screen .width/2-165 ,Screen.height/2-180, 330, 260), "Congratulations!  You have defeated all the Evil Skellies!");
             if(GUI.Button(new Rect(Screen .width/2-60 ,Screen.height/2-13, 120, 26), "Continue Playing"))YouWon=false;
         }
         else if(YouLose) {
                 GUI.Box(new Rect(Screen .width/2-165 ,Screen.height/2-180, 330, 260), "Noooo!  You have failed to defeat all the Evil Skellies!");
             if(GUI.Button(new Rect(Screen .width/2-60 ,Screen.height/2-13, 120, 26), "Exit Game"))Application .Quit();
         }
         
         //Exit  game using esc key
     /*    if (Input.GetKey(KeyCode.Escape))
             {
                 Application.Quit();
         } 
     */
         //if esc is pressed show this menu
         if(quitMenu) {
  
             GUI.Box(new Rect(Screen .width/2-150 ,Screen.height/2-150,300,300), "Main Menu"); 
                  if(GUI.Button(new Rect(Screen.width/2-140 ,Screen.height/2+90,280,50), "Quit"))Application.Quit();
                 if(GUI.Button(new Rect(Screen.width/2-140 ,Screen.height/2-120,280,50), "New Game")) Application.LoadLevel("Dungeon Hordes");
             
  
         }
     }
     
     
 }
 

It works fine but when i add this code http://wiki.unity3d.com/index.php/Click_To_Move_C

It works as it should only after my character is dead. I tried to make another script file or implement that code in the code above but it has the same result. If my player is alive keyboard controls works fine but after i stopped pressing it the player goes back to the original position. if i move it with the mouse it stays there but there is no animation. If my player is dead the death animation does not occur and i can control the character with running animation....( so the code works perfect after my char dies).

Here are the both codes combined:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 
 
 public class PlayerController : MonoBehaviour {
     public float movespeed=4;
     public float Damage=60;
     public Transform Cam;
     public float CamHeight=10;
     public float CamHeightPushback=5;
     public AnimationClip Run;
     public AnimationClip Idle;
     public AnimationClip Attack;
     public AnimationClip die;
     public float AttackSpeed=0.7f;
     private bool kill;
     public bool dead;
     private bool playd;
     private float atime;
     private bool dealdamage;
     public int TotalAICount;
     public bool YouWon;
     public bool YouLose;
     public bool quitMenu=false;
     private bool w;
     public GUISkin MenuSkin;
     //----------------------------------------------------
         private Transform myTransform;                // this transform
     private Vector3 destinationPosition;        // The destination Point
     private float destinationDistance;            // The distance between myTransform and destinationPosition
     public float moveSpeed;                        // The Speed the character will move
     //-------------------------------------------------------------
     
     public Vector3 For;
     public List<Transform> KillList;
     
     // Use this for initialization
     void Start () {
         if(Cam)Cam.parent=null;
         
         w=true;
         playd=true;
         //---------------------------------------------------------------------------------
         myTransform = transform;                            // sets myTransform to this GameObject.transform
         destinationPosition = myTransform.position;            // prevents myTransform reset
         //-----------------------------------------------------------------------------------
     }
     
     // Update is called once per frame
     void Update () {
         
         //--------------------------------------------------------------------------------------------
         // keep track of the distance between this gameObject and destinationPosition
         destinationDistance = Vector3.Distance(destinationPosition, myTransform.position);
  
         if(destinationDistance < .5f){        // To prevent shakin behavior when near destination
             moveSpeed = 0;
             animation.CrossFade( Idle.name, 0.15f);//<---------------------------------------------
         }
         else if(destinationDistance > .5f){            // To Reset Speed to default
             moveSpeed = 14;
             animation.CrossFade( Run.name, 0.15f); //<-----------------------------------------------------------------------
         }
  
  
         // Moves the Player if the Left Mouse Button was clicked
         if (Input.GetMouseButtonDown(0)&& GUIUtility.hotControl ==0) {
  
             Plane playerPlane = new Plane(Vector3.up, myTransform.position);
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             float hitdist = 0.0f;
  
             if (playerPlane.Raycast(ray, out hitdist)) {
                 Vector3 targetPoint = ray.GetPoint(hitdist);
                 destinationPosition = ray.GetPoint(hitdist);
                 Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
                 myTransform.rotation = targetRotation;
             }
         }
  
         // Moves the player if the mouse button is hold down
         else if (Input.GetMouseButton(0)&& GUIUtility.hotControl ==0) {
  
             Plane playerPlane = new Plane(Vector3.up, myTransform.position);
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             float hitdist = 0.0f;
  
             if (playerPlane.Raycast(ray, out hitdist)) {
                 Vector3 targetPoint = ray.GetPoint(hitdist);
                 destinationPosition = ray.GetPoint(hitdist);
                 Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
                 myTransform.rotation = targetRotation;
             }
         //    myTransform.position = Vector3.MoveTowards(myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
         }
  
         // To prevent code from running if not needed
         if(destinationDistance > .5f){
             myTransform.position = Vector3.MoveTowards(myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
         }
         //--------------------------------------------------------------------------------------------------------------
         
         
         //check to see it menu button esc is pressed
         if(Input.GetKeyDown(KeyCode.Escape)) {
           quitMenu = !quitMenu;
         }
         
         //Victory
         if(w){
         if(TotalAICount<=0){
                 YouWon=true;
             w=false;
                 YouLose=false;
             }
         }
         
         
         Health php=(Health)GetComponent("Health");
         if(php){
         if(php.CurrentHealth<=0)dead=true;
         }
         
         //DEATH
         if(dead){
             if(playd){
             animation.CrossFade( die.name, 0.15f);    
             }
             playd=false;
             YouLose =true;
         }
         else{
         
         For.y=270;
         //CAMERA POSITION
         Vector3 ch=transform.position;
         ch.y=transform.position.y+CamHeight;
         ch.x=transform.position.x+CamHeightPushback;
         
         Cam.transform.position=ch;
         if(Input.GetKeyUp(KeyCode.Space)){
             dealdamage=true;
         kill=true;    
         }
         
             //LETS DO SOME KILLIN!
         if(kill){
             atime+=Time.deltaTime;
             animation[Attack.name].speed = animation[Attack.name].length / AttackSpeed;
         animation.CrossFade( Attack.name, 0.15f);
             
             if(atime>=AttackSpeed*0.35f&atime<=AttackSpeed*0.48f){
             if(KillList.Count>0&dealdamage){
                 int    ls=KillList.Count;
                 for (int i = 0; i < ls; i++){
                     Health hp=(Health)KillList[i].transform.GetComponent("Health");
                             
                         hp.CurrentHealth=hp.CurrentHealth-Damage;
                             if(hp.Dead){}
                             else if(hp.CurrentHealth<=0)TotalAICount=TotalAICount-1;
                     }
                     dealdamage=false;
                 }
             }
             
             if(atime>=AttackSpeed){
                 kill=false;
                 atime=0;
             }
         }
         else{
                 //RUN ANIMATION IF ANY ARROW KEYS ARE PRESSED
         if(Input.GetKey(KeyCode.UpArrow)|Input.GetKey(KeyCode.DownArrow)|Input.GetKey(KeyCode.RightArrow)|Input.GetKey(KeyCode.LeftArrow)){
     
                 animation.CrossFade( Run.name, 0.15f);    
         }
         else{
         animation.CrossFade( Idle.name, 0.15f);        
         }
         }
         //MOVEMENT
     if(Input.GetKey(KeyCode.UpArrow)){
             transform.position += transform.forward * +movespeed * Time.deltaTime;
     Cam.transform.position += transform.forward * +(movespeed) * Time.deltaTime;
             transform.rotation=Quaternion.Euler(0, -90, 0);
     
         }
         else{
         
         if(Input.GetKey(KeyCode.DownArrow)){
             transform.rotation=Quaternion.Euler(0, 90, 0);
     transform.position += transform.forward * +movespeed * Time.deltaTime;
     Cam.transform.position += transform.forward * +(movespeed) * Time.deltaTime;
                 
         }
         else{
         
         if(Input.GetKey(KeyCode.RightArrow)){
             transform.rotation=Quaternion.Euler(0, 0, 0);
     transform.position += transform.forward * +movespeed * Time.deltaTime;
     Cam.transform.position += transform.forward * +(movespeed) * Time.deltaTime;
         
         }
         
         
         if(Input.GetKey(KeyCode.LeftArrow)){
             transform.rotation=Quaternion.Euler(0, -180, 0);
                     transform.position += transform.forward * +movespeed * Time.deltaTime;
     Cam.transform.position += transform.forward * +(movespeed) * Time.deltaTime;
         
         }
         }
         }
         }
     }
     
     void OnTriggerEnter(Collider other){
         //ADD AIS TO LIST TO INFLICT DAMAGE IF IN RANGE
         Health AI=(Health)other.transform.GetComponent("Health");
         if(AI){
         KillList.Add(other.transform);
             
         }
     }
     
     void OnTriggerExit(Collider other){
         //REMOVE FROM LIST WHEN OUT OF RANGE
         Health AI=(Health)other.transform.GetComponent("Health");
         if(AI){
         KillList.Remove(other.transform);
             
         }
     }
     
                
         
     void OnGUI(){
         
         //set menu skin
         GUI.skin = MenuSkin;
         
         //HEALTH BAR AND AI COUNT
         Health php=(Health)GetComponent("Health");
         if(php){
         float hpp=php.CurrentHealth;
         GUI.Label(new Rect(0, 30, 300, 33), "Health: "+hpp);
         }
         GUI.Label(new Rect(0, 67, 300, 33), "Evil Skellies Left: "+TotalAICount);
         
     
             
         //YOU WON!
         if(YouWon){ 
          GUI.Box(new Rect(Screen .width/2-165 ,Screen.height/2-180, 330, 260), "Congratulations!  You have defeated all the Evil Skellies!");
             if(GUI.Button(new Rect(Screen .width/2-60 ,Screen.height/2-13, 120, 26), "Continue Playing"))YouWon=false;
         }
         else if(YouLose) {
                 GUI.Box(new Rect(Screen .width/2-165 ,Screen.height/2-180, 330, 260), "Noooo!  You have failed to defeat all the Evil Skellies!");
             if(GUI.Button(new Rect(Screen .width/2-60 ,Screen.height/2-13, 120, 26), "Exit Game"))Application .Quit();
         }
         
         //Exit  game using esc key
     /*    if (Input.GetKey(KeyCode.Escape))
             {
                 Application.Quit();
         } 
     */
         //if esc is pressed show this menu
         if(quitMenu) {
  
             GUI.Box(new Rect(Screen .width/2-150 ,Screen.height/2-150,300,300), "Main Menu"); 
                  if(GUI.Button(new Rect(Screen.width/2-140 ,Screen.height/2+90,280,50), "Quit"))Application.Quit();
                 if(GUI.Button(new Rect(Screen.width/2-140 ,Screen.height/2-120,280,50), "New Game")) Application.LoadLevel("Dungeon Hordes");
             
  
         }
     }
     
     
 }
 
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

0 Replies

· Add your reply
  • Sort: 

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

11 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

Related Questions

Animation not working correctly on moving object 1 Answer

Can someone help me with my TPS Controller/Camera? 1 Answer

set limit mouse x y Help needed 1 Answer

Script on moving object doesn't work? 1 Answer

How would I script a simple path animation in C# 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