- Home /
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);
}
}
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); }
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.
I edited my answer to include this feature - give it a try.
Omg, awesome!! Thanks a lot!! This is EXACLY what I was trying to achieve :D
hahahah, o melhor q so agora me dei conta q vc eh brazuca!! mto obrigado amigo ;)
Brasileiro também? Tô em SP - meu email é aldo@pixtar.com.br
Your answer
![](https://koobas.hobune.stream/wayback/20220612220244im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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