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