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 Fabz · Nov 16, 2011 at 05:24 PM · mousemouselookmouseclickmouse aimmousefollow

Stop object rotation

Hi, Im having trouble trying to figure out how to accomplish what I want.

Simple Idea, I have a cannon that follow the mouse and a button that fires the cannon, the problem is when I click the fire btn the direction of the cannon is pointing towards the button, and i want to FIRST aim the cannon freeze the position and FIRE it with the other btn

This is the script Im currently using for the mouse follow behavior.

private var lineRenderer : LineRenderer;

function Start() { lineRenderer = GetComponent(LineRenderer); lineRenderer.SetWidth(4,3); lineRenderer.SetVertexCount(2); }

// speed is the rate at which the object will rotate var speed = 4.0;

function Update () {

 // Generate a plane that intersects the transform's position with an upwards normal.
 var playerPlane = new Plane(Vector3.up, transform.position);
 // Generate a ray from the cursor position
 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 // Determine the point where the cursor ray intersects the plane.
 // This will be the point that the object must look towards to be looking at the mouse.
 // Raycasting to a Plane object only gives us a distance, so we'll have to take the distance,
 //   then find the point along that ray that meets that distance.  This will be the point
 //   to look at.
 var hitdist = 0.0;
 // If the ray is parallel to the plane, Raycast will return false.
 if (playerPlane.Raycast (ray, hitdist)) {
     // Get the point along the ray that hits the calculated distance.
     var targetPoint = ray.GetPoint(hitdist);
     // Determine the target rotation.  This is the rotation if the transform looks at the target point.
     var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
     // Smoothly rotate towards the target point.
     transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
     // shorten the ray if it's hitting some obstacle:
     var hit: RaycastHit;
     if (Physics.Linecast(transform.position, targetPoint, hit)){
         targetPoint = hit.point;
     }
 } else {
     targetPoint = transform.position;
 }
 lineRenderer.SetPosition(0, transform.position);
 lineRenderer.SetPosition(1, targetPoint);

}

Any help is apreciated, best regards ;)

Comment
Add comment · Show 1
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 cj_coimbra · Nov 16, 2011 at 08:20 PM 1
Share

$$anonymous$$aybe you could click, then make the canon aim follow mouse, and when you release the click you did, it would freeze the cannon so you could fire it while freezed at that position.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Nov 17, 2011 at 04:15 PM

To freeze the aim, you could use a bool variable and toggle its value when the button is pressed, and place the aim code inside an if - but you must declare the variable targetPoint outside the function, so it can "remember" the last point aimed when you freeze the aim.

private var lineRenderer : LineRenderer;

function Start() { lineRenderer = GetComponent(LineRenderer); lineRenderer.SetWidth(4,3); lineRenderer.SetVertexCount(2); }

// speed is the rate at which the object will rotate var speed = 4.0; var targetPoint: Vector3; // save the target point even when freezed var freeze = false; // tells if aim is freezed or not

function Update () {

 if (Input.GetMouseButtonDown(1){ // freeze/unfreeze the aim with Right Mouse Button
     freeze = !freeze;
 }
 if (freeze){ // if aim freezed...
     if (Input.GetMouseButtonDown(0)){ // allow firing with Left Mouse Button
         // place here the cannon shoot code
         // and finish unfreezing the aim
         freeze = false; // unfreeze aim after shooting
     }
 }
 else { // if not freezed...
     // Generate a plane that intersects the transform's position with an upwards normal.
     var playerPlane = new Plane(Vector3.up, transform.position);
     // Generate a ray from the cursor position
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     // Determine the point where the cursor ray intersects the plane.
     // This will be the point that the object must look towards to be looking at the mouse.
     // Raycasting to a Plane object only gives us a distance, so we'll have to take the distance,
     //   then find the point along that ray that meets that distance.  This will be the point
     //   to look at.
     var hitdist = 0.0;
     // If the ray is parallel to the plane, Raycast will return false.
     if (playerPlane.Raycast (ray, hitdist)) {
         // Get the point along the ray that hits the calculated distance.
         targetPoint = ray.GetPoint(hitdist);
         // Determine the target rotation.  This is the rotation if the transform looks at the target point.
         var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
         // Smoothly rotate towards the target point.
         transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
         // shorten the ray if it's hitting some obstacle:
         var hit: RaycastHit;
         if (Physics.Linecast(transform.position, targetPoint, hit)){
             targetPoint = hit.point;
         }
     } else {
         targetPoint = transform.position;
     }
 }
 lineRenderer.SetPosition(0, transform.position);
 lineRenderer.SetPosition(1, targetPoint);

}

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 Fabz · Nov 18, 2011 at 12:39 PM 0
Share

Its working, but I want to use a GUI Button "Fire" ins$$anonymous$$d of a mousebutton, but Im working on it, trying to understand scripting a bit better thanks to you mate ;)

avatar image aldonaletto · Nov 18, 2011 at 02:21 PM 0
Share

It's easy: just replace Input.Get$$anonymous$$ouseButtonDown(0) with Input.GetButtonDown("Fire1")

avatar image syclamoth · Nov 18, 2011 at 02:32 PM 0
Share

Not quite that easy- GUI buttons have to be in OnGUI, not Update. Even still, pretty straightforward.

avatar image aldonaletto · Nov 18, 2011 at 11:37 PM 0
Share

Well observed, @syclamoth! I missed the GUI thing - maybe I should change my glasses...

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Fix Gimbal Lock Mouse Look? 0 Answers

Why is OnDrag triggering but not OnMouseDown? 0 Answers

Camera problems 1 Answer

Way to swap left and right mouse buttons? 2 Answers

have object rotate to the mouses screen position 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