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 ruifernandes01 · Jun 26, 2012 at 09:27 AM · lookatboat

aim the enemy to me, Rotate(90 * Vector3.up)

Hello, i have this script that i've seen from a question here on unity answers, and i'm trying to adapt it to me, but a cant, i'm making a battleship game, and i need the enemy to target me, so he needs to be pointing at me, but since i've use someone else script, now i cant put Rotate(90 * Vector3.up), so that he looks lateral at me, can some one help? this is my script:

 # pragma strict
 
 var speed = 3000;
 
 var distance : int;
 var maxDistance = 50;
 var myTransform = transform;
 var LookAtTarget : Transform;
 var bullitPrefab : Transform;
 var damp = 6.0;
 var savedTime = 0;
 
 function Update () 
 {
     distance = (LookAtTarget.position - myTransform.position).magnitude;
     
     if(LookAtTarget && distance < maxDistance)
     {
         var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position); //this is what i want: transform.Rotate(90 * Vector3.up);
     
         transform.rotation = Quaternion.Slerp(transform.rotation,rotate,Time.deltaTime * damp);
         var seconds : int = Time.time;
         var evenodd = (seconds / 2);
         if (evenodd) 
         {
             Shoot(seconds);
         }
     }
 }  
  
 function Shoot(seconds)
 {
     if(seconds != savedTime)
     {
         var bullit = Instantiate(bullitPrefab,transform.Find("spawnPoint").transform.position,
                     Quaternion.identity);
         bullit.rigidbody.AddForce(-transform.right * speed);
         savedTime = (seconds);    
     } 
 }
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

3 Replies

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

Answer by Bunny83 · Jun 27, 2012 at 12:46 PM

If you just want to rotate the target rotation by 90°, just do something like that:

 var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position); 
 var r90 = Quaternion.Euler(0,90,0);
 rotate *= r90;

 transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);

You might need to use -90 but in general that should work.

ps: the whole evenodd thing doesn't work. You don't compute the modulo, you just divide the seconds. The if will be true as long as seconds is 2 or greater, which it is after two seconds...

Also there are a lot unnecessary calculations. Also you have a chached transform (myTransform) but you still use transform. Also distance and maxDistance are integer which doesn't make much sense. It also looks like you don't use the distance variable somewhere else in your script, so it should be just a local variable and not a member variable.

 function Update () 
 {
     var targetDirection = (LookAtTarget.position - myTransform.position);
     var distance = targetDirection.magnitude;
     
     if(LookAtTarget && distance < maxDistance)
     {
         var rotate = Quaternion.LookRotation(targetDirection ); 
         var r90 = Quaternion.Euler(0,90,0);
         rotate *= r90;
         
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, rotate, Time.deltaTime * damp); 
         var seconds : int = Time.time;
         var evenodd = (seconds % 2);  // modulo operator
         if (evenodd) 
         {
             Shoot(seconds);
         }
     }
 }
Comment
Add comment · Show 2 · 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 ruifernandes01 · Jun 27, 2012 at 02:34 PM 0
Share

@Bunny83 you have eli$$anonymous$$ated my shoot function, but i'm using that part in my script, you see this script is about rotating the enemy so that he "targets me" and then shoot at me, i also trynhg to make a hit system points, that when i hit the enemy boat 3 times he destroys himselve, after an animation was played, so i kinda need that part, but i've added you're part of the script to $$anonymous$$e, and it works just fine! thanks a lot man! :D

avatar image Bunny83 · Jun 27, 2012 at 10:31 PM 0
Share

I did what? I just posted an slightly optimised Update function. I didn't posted your whole script... I still call your Shoot function so of course you need the rest of your script (your variables and your Shoot function)

avatar image
0

Answer by FadeToBlack · Jun 26, 2012 at 10:40 AM

There is a few ways to do what you want:

  1. you can multiply your rotation quaternion by another quat with needed rotation (there is function to construct quat by axis and angle, then you need to multiply var rotate by it - note what multiplication order is important). you need to know some math to do it right, or just make some experiments.

  2. you can create dummy(empty) GameObject and link your model to it. (make children, i.e. drag your model at dummy in scene tree view). then you need to setup relative rotation in editor. this is the common way - any object can be adjusted, just make parent dummy object for it. also you can adjust center of rotation using this trick.

Comment
Add comment · Show 4 · 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 ruifernandes01 · Jun 26, 2012 at 11:15 AM 0
Share

can you explain the second one a litle more simplier?

avatar image FadeToBlack · Jun 26, 2012 at 07:39 PM 0
Share

the GameObjects in unity can has hierarchy, i.e. one GameObject can inherit transformation of the parent GameObject.. hmm.. not so much simpler... so, imagine a door as Game Object & door handle as GameObject. if you turn door, door handle also will move, because it connected (linked) to the door. as this case, gameobjects can has some connections (hierarchy relationships like parent-child). when we move or rotate parent object, child object also moves, & this is the common gamedev practice used in many game engines & also 3d max has the same thing (select & link tool). you need to create empty gameobject and link your battleship gameobject to it. then you need rotate your battleship by 90 deg relative to empty gameobject. you need to apply lookat transformation to empty game object, not to the battleship. to make hierarchy you just need to drag & drop your battleship object on empty gameobject in hierarchy window.

avatar image ruifernandes01 · Jun 27, 2012 at 10:57 AM 0
Share

but cant i just add this line to the script?

Rotate(90 * Vector3.up)

Isn't that simplier?

avatar image FadeToBlack · Jun 27, 2012 at 05:09 PM 0
Share

simple, simple. there is no simplicity at all. if there is a lot of battelships, you can be surprised that models will have different rotations (ye, modellers think another way). do you need kick modeller`s ass each time when it happens? no, just do it yourself!

avatar image
0

Answer by Flufflesthepancake · Jun 27, 2012 at 11:33 AM

var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position + Vector3.up*90)

Depending on which direction you want it to rotate you might minus it, or change it to one of the other vectors (right, forward...)

because we're dealing with vector3's and rotation here... silly me.

Comment
Add comment · Show 9 · 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 ruifernandes01 · Jun 27, 2012 at 11:45 AM 0
Share

lot more simplier! :D But he says that " Operator '+' cannot be used with a left hand side of type 'UnityEngine.Vector3' and a right hand side of type 'int'"..

avatar image Flufflesthepancake · Jun 27, 2012 at 11:52 AM 0
Share

Yeah, sorry, I posted a version that should work on the answer above:

var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position + 90);

Because rotate is a vector3 and you can't just go adding numbers to them jazz.

avatar image ruifernandes01 · Jun 27, 2012 at 12:04 PM 0
Share

yes i notest that, i've already tryed that, the same error appears, can use '+' operator.

and sorry but i didn't undestood you're 3 line, not so fluent at english, i'm portuguese!

any idea to solve this error?

avatar image Flufflesthepancake · Jun 27, 2012 at 12:18 PM 0
Share

Urgh, I'm not doing this well...

var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position + Vector3.right*90); THAT, might do it.

avatar image ruifernandes01 · Jun 27, 2012 at 12:24 PM 0
Share

yeah i've tryed this line before, i notest that! ;) but still the same problem resides, can join the + operator. and i'm sorry but i didnt understand you're 3 line. :P portuguese trying to speak english.. doing my best!

Any idea to solve this?

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How do you force a player to look at something? 4 Answers

Define var target : Transform; within the script 1 Answer

Make an FPS gun point at the mouse position. 2 Answers

How can I get the rotation between two objects 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