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 ExyloN · Jul 17, 2012 at 11:48 PM · objectmousecollidersattack

Attack on mouse click

Hi guys, I have a character that is moved by mouse clicks, Im having some problems now that I want to make the character to attack a turret when someone clicks on the object. So I need the character to move to the object, hit the object and play an attack animation while hitting.

This is my movement script:

 //variaveis
 var vel : int;
 vel = 0;
 var Animacao : Animation;
 
 //Clipes de animação
 var clipes:AnimationClip[];
 
 //Guarda se ja foi clicado
 private var clicado:boolean = false;
 private var seta : GameObject;
 var detectCollisions : boolean;
 
 function Start(){
 seta=GameObject.CreatePrimitive(PrimitiveType.Plane);
 seta.transform.position = Vector3(0, 0, 0);
 }
 
 function Update() {
 //se clicou com botão esq
 if(Input.GetKeyDown(KeyCode.Mouse1))
 
 {
 //Cria um plano com a posição da personagem
 var playerPlane = new Plane(Vector3.up, transform.position);
 
 //Desenha um ponto entre o personagem e o local clicado
 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 var hitdist = 0.0;
 vel = 10;
 
 //Seta que foi clicado
 clicado = true;
 
 //Verifica se pode mover-se e desenha o caminho
 if (playerPlane.Raycast (ray, hitdist)) {
 
 //cria variavel depois de criado o caminho
 var targetPoint = ray.GetPoint(hitdist);
 
 //guarda a rotação
 targetPosition = ray.GetPoint(hitdist);
 
 var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
 seta.transform.position = Vector3(targetPosition.x,targetPosition.y,targetPosition.z );
 
 //Ajusta rotação da personagem
 transform.rotation = targetRotation;
 }
 
 rigidbody.isKinematic = false;
 rigidbody.detectCollisions = true;
 
 }
 
 //Caso a personagem não esteja no ponto clicado ela anda até lá
 if((transform.position - seta.transform.position).magnitude > 2){
 transform.Translate(Vector3.forward * ( Time.deltaTime * vel));
 
 //animação dele a andar
 Animacao.clip = clipes[1];
 Animacao.Play();
 }
 else
 {
 //animação dele parado
 Animacao.clip = clipes[0];
 Animacao.Play();
 }
 }

I think this script might help with something (Untested):

 #pragma strict
 
 var currentWeaponRange : float = 10.0;
 var locationToMoveTo : Vector3;
 var dir : Vector3;
 
 //Transforms
 
 var movePoint : Transform;
 var currentTarget : Transform = null;
 private var currentMovePoint : Transform;
 
 var FireDmg : int = 10;
 
 var hit : RaycastHit;
 var ray : Ray;
 
 var enemyScript : DamageReceiver = null;
 
 function Update () 
 {
     //Mouse Location awarenesss. When Mouse-Left is pressed, then...
     if(Input.GetMouseButtonDown(1)) 
     {
  ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         //Casts a ray, stores the location and object it transected in Hit
         if(Physics.Raycast(ray,hit,1000))   
         {
             //If it hits an Enemy-
             if(hit.transform.tag == "Test")    
             {
              print("Encontrou Test");
                 currentTarget = hit.transform;
                 enemyScript = hit.transform.GetComponent(DamageReceiver);
                 Instantiate(movePoint, Vector3(hit.point.x, hit.point.y + 1, hit.point.z), transform.rotation);
                 if(Vector3.Distance(transform.position, hit.transform.position) <= currentWeaponRange)  
                 {
                     //Fire the Bullet
                     attack();
                     enemyScript.ApplyDamage(FireDmg);
                     print("Enemy currently is " + enemyScript.currEnergy + "/" + enemyScript.MaxEnergy);
                     currentTarget = hit.transform;
                 }
             }
             //If it hits a Turret-
             if(hit.transform.tag == "Harvestable")  
             {
 
             }
         }
     }
 }
 
 function attack()   
 {
 
 }

Regards, ExyloN

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Bullet mouse control 2 Answers

How can I manipulate an object with the mouse? 1 Answer

Dragged object keeping it's original rotation. 0 Answers

2 colliders on 1 opbject is that possible?? 1 Answer

Destroy objects by clicking on them 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