Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Fabz · Sep 20, 2011 at 01:26 AM · raycastcollidertrajectory

Trajectory aim, how do i do it?

Hi, Im using a script I found somewhere in this forum, Its good enought to get my object (cannon) to follow the mouse direction, but I also need a trejetory line that stop at colision (the walls around the level). I can't find the answer anywhere, alread tryed messing around with raycast and lineRenderer, but I lack the knowledge on scripting. Anybody have a solution, or at least point me out on the right direction???

here's the script Im using:

// LookAtMouse will cause an object to rotate toward the cursor, along the y axis. // // To use, drop on an object that should always look toward the mouse cursor. // Change the speed value to alter how quickly the object rotates toward the mouse.

// 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);
 }

}

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
Best Answer

Answer by aldonaletto · Sep 20, 2011 at 03:13 AM

I don't know if this is exactly what you're looking for, but you can use the LineRenderer component to add a ray from the object to the target. Add a LineRenderer (menu Component/Miscellaneous/Line Renderer) to your object and modify the script like below:

private var lineRenderer : LineRenderer;

function Start() { lineRenderer = GetComponent(LineRenderer); lineRenderer.SetColors(Color.red, Color.red); lineRenderer.SetWidth(0.1,0.1); 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);
 } else {
     targetPoint = transform.position;
 }
 lineRenderer.SetPosition(0, transform.position);
 lineRenderer.SetPosition(1, targetPoint);

} EDITED: You can stop the ray at walls or other obstacles simply by doing a Linecast from transform.position to targetPoint, and replacing the later with the hit point (if any):

...
        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 6 · 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 · Sep 20, 2011 at 11:31 AM 0
Share

It is working perfectly thanks a lot, but is there any way to make the line stop at the wall?? I mean, my game have this top camera, and once I put the mouse past the wall the line follow's it.

avatar image aldonaletto · Sep 21, 2011 at 12:07 AM 0
Share

I edited my answer to include this feature - give it a try.

avatar image Fabz · Sep 21, 2011 at 12:17 AM 0
Share

Omg, awesome!! Thanks a lot!! This is EXACLY what I was trying to achieve :D

avatar image Fabz · Sep 21, 2011 at 01:41 AM 0
Share

hahahah, o melhor q so agora me dei conta q vc eh brazuca!! mto obrigado amigo ;)

avatar image aldonaletto · Sep 21, 2011 at 04:35 AM 0
Share

Brasileiro também? Tô em SP - meu email é aldo@pixtar.com.br

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Get color pixel from terrain, with a WheelCollider! 1 Answer

Physics.Raycast not hitting anything 1 Answer

raycast doesn't collide on anything? (C#) 2 Answers

Detect only UI button click 3 Answers

raycast not colliding 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