- Home /
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 ;)
$$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.
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);
}
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 ;)
It's easy: just replace Input.Get$$anonymous$$ouseButtonDown(0) with Input.GetButtonDown("Fire1")
Not quite that easy- GUI buttons have to be in OnGUI, not Update. Even still, pretty straightforward.
Well observed, @syclamoth! I missed the GUI thing - maybe I should change my glasses...
Your answer
Follow this Question
Related Questions
Fix Gimbal Lock Mouse Look? 0 Answers
Why is OnDrag triggering but not OnMouseDown? 0 Answers
Camera problems 1 Answer