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 chewdri · May 07, 2013 at 12:00 PM · mousetargetshot

Replace "Transform" by my Mouse Target

Hi actually I have this shot script

 var bullet : Transform;
     var speed = 16000;
     var Speed = 300;
     var spawnPoint : Transform;
     var rapidFire = false;
     var counter = 0.0;
     var rateOfFire = 0.25;
     var ennemydeux : Transform;
     
     
     
      
     function Update ()
     
     
     {
     CheckClick();
     }
      
     function CheckClick()
     {
     if(Input.GetButtonDown("Fire1") || (rapidFire && Input.GetButton("Fire1")))
     {
     var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var hit : RaycastHit;
     // you can set the 100 to whatever distance you want :P
     if (Physics.Raycast(ray,hit,100))
     {
     var enemy = hit.transform.GetComponent(ennemy);
     if(hit.transform.GetComponent(ennemy))
     {
     transform.LookAt(ennemydeux);
     HandleFiring();
     }
     }
     }
     }
      
     function HandleFiring()
     {
     if(rapidFire)
     {
     counter += Time.deltaTime;
     // I'm going to assume RateOfFireCounter is a variable that you've not shown here? Otherwise this won't do anything for you...
     if(rateOfFire)
     {
     var shotRapid =Instantiate(bullet, spawnPoint.transform.position, Quaternion.identity);
     shotRapid.rigidbody.AddForce(transform.forward * speed);
     shotRapid.transform.LookAt(ennemydeux);
     shotRapid.transform.Translate(Vector3.forward*Speed*Time.deltaTime);
     Destroy((shotRapid.gameObject), 2);
     
     Counter=0;
     }
     }
     else
     {
     var shot = Instantiate(bullet, spawnPoint.transform.position, Quaternion.identity);
     shot.rigidbody.AddForce(transform.forward * speed);
     shot.transform.LookAt(ennemydeux);
     shot.transform.Translate(Vector3.forward*Speed*Time.deltaTime);
     Destroy((shot.gameObject), 2);
     }
     
     
     }


I'd like to replace "enemy" and "ennemydeux" by my MouseTarget but I dont know how to do it.

I tried to replace var ennemydeux : Transform; by var ennemydeux : RaycastHit; but I dont work. (error with th lookat)

ty for any help !

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
0

Answer by Mikilo · May 07, 2013 at 01:09 PM

Yo!

 var bullet : Transform;
 var speed = 16000;
 var Speed = 300;
 var spawnPoint : Transform;
 var rapidFire = false;
 var counter = 0.0;
 var rateOfFire = 0.25;
 var ennemydeux : Transform;
 
 function Update()
 {
     CheckClick();
 }
 
 function CheckClick()
 {
     if (Input.GetButtonDown("Fire1") || (rapidFire && Input.GetButton("Fire1")))
     {
         var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         var hit : RaycastHit;
         // you can set the 100 to whatever distance you want :P
         if (Physics.Raycast(ray, hit, 100))
         {
             ennemydeux = hit.collider.transform;
             transform.LookAt(ennemydeux);
             HandleFiring();
         }
     }
 }
 
 function HandleFiring()
 {
     if (rapidFire)
     {
         counter += Time.deltaTime;
         // I'm going to assume RateOfFireCounter is a variable that you've not shown here? Otherwise this won't do anything for you...
         if (rateOfFire)
         {
             var bulletScript : Bullet;
             var shotRapid = Instantiate(bullet, spawnPoint.transform.position, Quaternion.identity);
             bulletScript = shotRapid.GetComponent(Bullet);
             bulletScript.cible = ennemydeux.gameObject;
             shotRapid.transform.LookAt(ennemydeux);
             Destroy((shotRapid.gameObject), 2);
             Counter=0;
         }
     }
     else
     {
         var shot = Instantiate(bullet, spawnPoint.transform.position, Quaternion.identity);
         
         bulletScript = shot.GetComponent(Bullet);
         bulletScript.cible = ennemydeux.gameObject;
         shot.transform.LookAt(ennemydeux);
         Destroy((shot.gameObject), 2);
     }
 }


Bullet.js: [Avec un B majuscule!]

 #pragma strict
 
 var speed = 300;
 var cible : GameObject;
 
 function Update (){
     if (cible != null)
         this.transform.position += (this.cible.transform.position - this.transform.position).normalized * this.speed * Time.deltaTime; 
 }

J'ai testé, et c'est approuvé. =D

Comment
Add comment · Show 28 · 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 chewdri · May 07, 2013 at 01:10 PM 0
Share

Yeah I'm French

avatar image Mikilo · May 07, 2013 at 01:13 PM 0
Share

La dernière question n'est pas la plus importante! XD Dis moi si ça t'aide ou pas ce que je t'ai écrit.

avatar image Mikilo · May 07, 2013 at 01:29 PM 0
Share

Change le code à la ligne 27.

avatar image chewdri · May 07, 2013 at 01:34 PM 0
Share

Quand tu dits "remplace ennemydeux par vector 3 type" je dois le remplacer au début comme ça

var ennemydeux = Vector3; ?

avatar image Mikilo · May 07, 2013 at 01:37 PM 0
Share

Plutôt var ennemydeux : Vector3; =D

Show more comments

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

13 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 avatar image avatar image

Related Questions

Targeting problem 1 Answer

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

On object click change scene 2 Answers

Javascript version of 'Mouse look' script? 1 Answer

How to Set "Target" from two or more different script? 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