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 /
This question was closed Jul 05, 2012 at 01:22 PM by whydoidoit for the following reason:

Other

avatar image
0
Question by multinfs · Jun 29, 2012 at 02:40 PM · mouserotateturret

make turret point where mouse points

I need help with making a script that rotates a turret to where the mouse points,I have made some kind of script but its just messy when turning.

I made it so there is a ray going from the camera to where the mouse points and a long distance and from the hit.point then make the turret LookAt.

    var hit : RaycastHit;
    var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    Debug.DrawRay (ray.origin, ray.direction * 200000, Color.yellow);
    if(Physics.Raycast(ray,hit)){
        transform.LookAt(hit.point,transform.up);
        }
It works when looking far ahead but when looking like behind the car it is sitting on it just turns weirdly and goes into the car. EDIT: I still can't make this work, I tried with adding Mathf.Clamp to limit rotation of the turret but it was'nt so successful.And I made a empty GameObject that is placed at hit.point to get the coordinates from that point.

var dist: float; var hitpoint : GameObject;

function Update () { transform.rotation.z = 0;

 var hit : RaycastHit;
 var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 Debug.DrawRay (ray.origin, ray.direction * 500000, Color.yellow);
 if(Physics.Raycast(ray,hit)){
     var rot2 : Vector3 = Vector3(hit.point.x + 90,hit.point.y - 90,0);
     dist = Mathf.Abs(Vector3.Distance(transform.position,hit.point));
     hitpoint.transform.position = ray.GetPoint(dist);
     transform.LookAt(hitpoint.transform.position,transform.up);
     transform.rotation.y = Mathf.Clamp(transform.rotation.y, -90,90);
     transform.rotation.x = Mathf.Clamp(transform.rotation.x, -20,10); 
 }

Link to test and see how it behaves HERE

alt text Oh sorry forgot, The box to the left is the right one, the thing to the right was the first try but I made a simple turret later, so the box to the left is the turret with this script.

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

  • Sort: 
avatar image
0

Answer by aldonaletto · Jun 29, 2012 at 03:39 PM

This script is correct, but the turret will rotate its whole body in the clicked point direction. If you want to make it rotate only around the Y axis, an auxiliary logic plane may be necessary - take a look at this question, where this solution is used.

EDITED: Well, the part you have showed at first was right, but its sequel isn't - you can't modify quaternion components directly.
I suppose the capsule is the shooter, and that cubic thing is the turret - am I right? If so, you should create an empty game object, child it to the car and move it to the shooter position, then child the turret and the shooter to this object. Adjust the turret position/orientation so that it's in front of the shooter and pointing in the empty object's forward direction (the blue axis).
The hierarchy of these objects will be:

  Car Body
    EmptyObject  <- the script is attached to this object
      Turret Model
      Shooter
Add the script below to the EmptyObject:

function Update () {
  // create a logical horizontal plane at this object's position:
  var hPlane: Plane = new Plane(transform.position, Vector3.up);
  // create a ray from the mouse pointer:
  var ray: Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
  var dist: float = 0; // this will receive the hit point distance
  if (hPlane.Raycast(ray, dist)){ // if the ray hits the plane...
    var hitPt = ray.GetPoint(dist); // get the hit point...
    transform.LookAt(hitPt); // and look at it
  }
}
Comment
Add comment · Show 3 · 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 multinfs · Jun 30, 2012 at 04:14 PM 0
Share

Well that code does'nt work properly, the turret itself goes into the car and is all messed up when looking on a object besides or behind the turret.. I use a gameobject to rotate 2 parts at a time as one and to keep the rotations, but the center of rotation thingy is sometimes not in the center.

avatar image multinfs · Jul 01, 2012 at 07:41 PM 0
Share

Hmm, doesnt that just make a plane and aim at the plane? Is it gonna be accurate if I shoot at a longer distance? And I want it to have a wide field of view, preferably if there is a way to have like either many planes all around the car or any other way so I can shoot around the car.

avatar image aldonaletto · Jul 02, 2012 at 12:39 AM 0
Share

The plane created has nothing to do with the Unity plane primitive: it's an infinite logical plane, created at the empty object position and parallel to the ground. Nothing is drawn, since it's a purely logical entity - but you can detect raycasts with the appropriate Plane.Raycast function. Better yet, only the plane detects these raycast hits, thus the other objects in scene are completely ignored.

avatar image
0

Answer by multinfs · Jul 05, 2012 at 12:48 PM

@aldonaletto It kinda works but it behaves weirdly when moving.I noticed that u made the plane at the position of the turret, which makes it very high of the ground. Now, I made a new vector3 at the turrets position but made it close to the ground. And when I made a cube appear where the mouse is pointing at it goes below ground when looking forward and on top of the ground when looking behind.

Comment
Add comment · 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
0

Answer by multinfs · Jul 05, 2012 at 01:04 PM

Closed this cause it is getting filled, And I can't comment or edit my posts! I can edit on internet explorer but not on chrome, Got to be a bug.

new post: here

Comment
Add comment · Show 1 · 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 whydoidoit · Jul 05, 2012 at 01:22 PM 0
Share

Removing the answer status from this as this is not an answer. Closing question per your desire.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Rotating object towards mouse point 1 Answer

Rotating a turret body and head 1 Answer

Look around when holding down mousebutton 1 Answer

Camera rotate with mouse in TPC 0 Answers

How do I rotate an object with the mouse? 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