In my project i have to rotate an OBJ file. i have constant pivot point. i got the horizontal rotation exactly.now i have to rotate my OBJ file as vertically. i concluded that have to change angle itself. give ideas to rotate vertically in that constant Pivot point.
my Pivot point is teapotNode_.rotationAxis = CC3VectorMake(0.1, 1, 0.3);
-(void)rightButtonPressed {
float angle;
teapotNode_.rotationAxis = CC3VectorMake(0.1, 1, 0.3);
angle +=2.0;
director_.running = YES;//Redirector object
if (director_.frameInterval == 2)
{
director_.running = NO;
}
}
-(void)leftButtonPressed {
float angle;
teapotNode_.rotationAxis = CC3VectorMake(0.1, 1, 0.3);
angle -=2.0;
director_.running = YES;//Redirector object
if (director_.frameInterval == 2)
{
director_.running = NO;
}
}
-(void)topButtonPressed {
float angle;
teapotNode_.rotationAxis = CC3VectorMake(0.1, 0, 0); ***//changed the Pivot point.***
angle +=2.0; **//how to calculate the Top Down angle.**
director_.running = YES;//Redirector object
if (director_.frameInterval == 2)
{
director_.running = NO;
}
}
I found answer myself https://github.com/antonholmquist/rend-ios in this rend-ios project.
Actually the problem is while clicking the right and left button the rotation is working properly, like top and bottom rotation is also working properly. but when i click right to top / bottom, the object is rotating but it flickered.
The solution is:
Globally declared:
float x=0.0;
float y=0.0;
float z=0.0;
float angle;
float xangle;
float yangle;
-(void)rightButtonPressed {
x=0; y=1; z=0; //ROTATE OBJECT IN Y-AXIS(Y=1 & X=0)
yAngle+=2.0; //GET ANGLE OF OBJECT ROTATION IN Y-AXIS (ANTI CLOCKWISE)
angle +=2.0;
teapotNode_.rotationAxis = CC3VectorMake(x,y,z);
teapotNode_.rotation = CC3VectorMake(0, yAngle, 0);
director_.running = YES;//Redirector object
if (director_.frameInterval == 2)
{
director_.running = NO;
}
}
-(void)leftButtonPressed {
x=0; y=1; z=0; //ROTATE OBJECT IN Y-AXIS(Y=1 & X=0)
yAngle-=2.0; //GET ANGLE OF OBJECT ROTATION IN Y-AXIS (CLOCKWISE)
angle -=2.0;
teapotNode_.rotationAxis = CC3VectorMake(x,y,z);
teapotNode_.rotation = CC3VectorMake(0, yAngle, 0);
director_.running = YES;//Redirector object
if (director_.frameInterval == 2)
{
director_.running = NO;
}
}
-(void)topButtonPressed {
//Existing
/*float angle;
teapotNode_.rotationAxis = CC3VectorMake(0.1, 0, 0); //Changed the Pivot point.
angle +=2.0; //how to calculate the Top Down angle.
*/
//Modified
x=1; y=0; z=0; //ROTATE OBJECT IN Y-AXIS(Y=0 & X=1)
xAngle+=2.0; //GET ANGLE OF OBJECT ROTATION IN Y-AXIS (Top Down)
angle +=2.0;
teapotNode_.rotationAxis = CC3VectorMake(x,y,z);
teapotNode_.rotation = CC3VectorMake(xangle, 0, 0);
director_.running = YES;//Redirector object
if (director_.frameInterval == 2)
{
director_.running = NO;
}
}
Related
I have the following code:
if (Input.GetKeyDown(KeyCode.A))
{
var rot = switchTransform.localEulerAngles;
Debug.Log(rot);
rot.x = 150;
switchTransform.localEulerAngles = rot;
}
When I click 'A', the switchTransform switches from (30, 0, 0) to (150, 0, 0) in the inspector and (30, 0, 0) to (30, 180, 180) in the console.
It works correctly if I write it like this:
angle = 150f;
if (Mathf.Abs(angle) <= 180 && Mathf.Abs(angle) > float.Epsilon)
{
angle = 180 - angle;
}
if (Input.GetKeyDown(KeyCode.A))
{
var rot = switchTransform.localEulerAngles;
rot.x = angle;
switchTransform.localEulerAngles = rot;
}
Why does it happen, and is there a better way to work around it?
This is the switch transform, as you can see it has unusual pivot.
this is the code
I add force but I want that the force Will be only on the x-axis and not on the y-axis
Collider2D[] objects = Physics2D.OverlapCircleAll(transform.position, radius, Layertohit);
foreach (Collider2D obj in objects)
{
Vector2 dir = obj.transform.position - transform.position;
dir.y = 0;
obj.GetComponent<Rigidbody2D>().AddForce(dir * force);
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, radius);
}
You can freeze the y axis like
foreach (Collider2D obj in objects)
{
Vector2 dir = obj.transform.position - transform.position;
dir.y = 0;
float y=obj.GetComponent<Rigidbody2D>().velocit.y
obj.GetComponent<Rigidbody2D>().AddForce(dir * force);
obj.GetComponent<Rigidbody2D>().velocity=new Vector2(obj.GetComponent<Rigidbody2D>().velocity.x,y)
}
I have a script which limits camera movement to within a defined range. Unfortunately, it only works on the first camera, and there are up to four orthographic cameras in my game.
I have been unable to adapt the script to work with multiple cameras, so I figured that the next best this would be to translate the points from the four corners from the first camera's viewport to world position, then from the world position back to the viewport of cameras 2, 3 and 4. However this doesn't appear to be working.
SpriteRenderer spriteBounds = GameObject.Find("Map").GetComponentInChildren<SpriteRenderer>();
float leftBound =0;
float rightBound =0;
float bottomBound =0;
float topBound = 0;
if((trackPlayer1 == null))
{
camPlayer1.transform.position = this.transform.position;
}
else
{
float vertExtent = camPlayer1.orthographicSize;
float horzExtent = vertExtent * (Screen.width * (camPlayer1.rect.width * 2)) / Screen.height; //I guess the problem is here... but how do I fix this??
leftBound = (float)(horzExtent - spriteBounds.sprite.bounds.size.x / 2.0f);
rightBound = (float)(spriteBounds.sprite.bounds.size.x / 2.0f - horzExtent);
bottomBound = (float)(vertExtent - spriteBounds.sprite.bounds.size.y / 2.0f);
topBound = (float)(spriteBounds.sprite.bounds.size.y / 2.0f - vertExtent);
camPlayer1.transform.position = new Vector3(Mathf.Clamp(trackPlayer1.transform.position.x, leftBound, rightBound), Mathf.Clamp(trackPlayer1.transform.position.y, bottomBound, topBound), camPlayer1.transform.position.z);
}
if((trackPlayer2 == null))
{
camPlayer2.transform.position = this.transform.position;
}
else
{
leftBound = camPlayer1.ViewportToWorldPoint(new Vector3(leftBound, 0, 0)).x;
rightBound = camPlayer1.ViewportToWorldPoint(new Vector3(rightBound, 0, 0)).x;
bottomBound = camPlayer1.ViewportToWorldPoint(new Vector3(0, bottomBound, 0)).y;
topBound = camPlayer1.ViewportToWorldPoint(new Vector3(0, topBound, 0)).y;
leftBound = camPlayer2.WorldToViewportPoint(new Vector3(leftBound, 0, 0)).x;
rightBound = camPlayer2.WorldToViewportPoint(new Vector3(rightBound, 0, 0)).x;
bottomBound = camPlayer2.WorldToViewportPoint(new Vector3(0, bottomBound, 0)).y;
topBound = camPlayer2.WorldToViewportPoint(new Vector3(0, topBound, 0)).y;
camPlayer2.transform.position = new Vector3(Mathf.Clamp(trackPlayer2.transform.position.x, leftBound, rightBound), Mathf.Clamp(trackPlayer2.transform.position.y, topBound, bottomBound), camPlayer2.transform.position.z);
}
I am having difficulty positioning a wall of objects(arrows) in front of the player. What I want is a solid wall of arrows to be shot in front of the player perpendicular to the view. So far I have the spawning of the objects correct and the y-axis placement correct. Now I just need to get the z and x-axis alignment correct. My code is as follows:
void run()
{
Vector3 pos = transform.position;
Quaternion angle = transform.rotation;
GameObject clone;
float startx = pos.x;
pos.y += 0.7f;
pos.z += 2f;
for(int y = 0; y < maxarrows; y++)
{
pos.y += 0.5f;
for(int x = 0; x < maxarrows; x++)
{
pos.x -= 0.5f;
clone = arrowPool.getArrowOld();
if(clone != null)
{
clone.transform.position = pos;
clone.transform.rotation = angle;
clone.rigidbody.velocity = clone.transform.forward*force;
}
}
pos.x = startx;
}
}
You're not taking into account the orientation of your player when you calculate the positions of all the arrows. What you could do is work in the player's local coordinate space and then transform to world space.
First we choose some points in the player's local coordinate space.
Vector3[] points = {
Vector3(-1, 1, 0), // upper-left
Vector3(1, 1, 0), // upper-right
Vector3(-1, -1, 0), // lower-left
Vector3(1, 1, 0), // lower-right
Vector3(0, 0, 0) // centre
};
Now we need to transform these points into world space. We can do this using Unity's Transform::TransformPoint function, which just multiplies the passed point by the transform's localToWorldMatrix:
for (int i = 0; i < 5; ++i) {
points[i] = transform.TransformPoint(points[i]);
}
Now you have the spawn positions for your arrows in world space.
Ok im new to javascript and Unity so this is clearly a very noob beginner question.
Basically I have created a script that i place on my camera. Then when i select a model in the game it becomes the target of the camera script and im trying to make the camera smoothly turn to it. below is what im trying
in the update function i use raycast to catch a unit on the mouse click. this becomes the target variable in the script. it sets a bool to true then to follow target in the moveto function. but it only seems to move the camera if i keep the mouse left button pressed down on the unit. Im trying to select a unit then let the camera smoothly rotate to the unit.
Any ideas on this rookie mistake?
#pragma strict
var speed = 5.0;
var freeMovementTurnSpeed = 10.0;
var freeMovementForwardSpeed = 10.0;
var freeMovementVerticalSpeed = 10.0;
var maxHeight = 30;
var minHeight = 2;
var target : Transform;
var distance = 10.0;
var maxDistance = 50;
var minDistance = 10;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
private var x = 0.0;
private var y = 0.0;
// The distance in the x-z plane to the target
var followDistance = 30.0;
var maxDistanceDelta = 0.2;
var damping = 3.0;
var followTarget = false;
var smoothRotation ;
function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
if(target){
transform.LookAt(target);
}
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate () {
if(target){
if(followTarget){
moveTo();
}
else{
targetSelectedMovement();
}
}
else {
freeMovement();
}
}
function moveTo(){
//transform.LookAt (target);
var currentDistance = Vector3.Distance(transform.position,target.position);
//if (currentDistance<40){
//followTarget = false;
//}
//else{
smoothRotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, smoothRotation, Time.deltaTime * damping);
print(target);
//transform.position = Vector3.MoveTowards(transform.position,target.position,maxDistanceDelta);
//}
}
function freeMovement(){
var zPos = Input.GetAxis("Vertical") * Time.deltaTime * freeMovementForwardSpeed;
var xPos = Input.GetAxis("Horizontal") * Time.deltaTime * freeMovementTurnSpeed;
var strafePos = Input.GetAxis("Strafe")* Time.deltaTime * freeMovementForwardSpeed;
var vertPos = Input.GetAxis("GainVertical")* Time.deltaTime * freeMovementVerticalSpeed;
if (Input.GetButton("Fire2")) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation2 = Quaternion.Euler(y, x, 0);
transform.rotation = rotation2;
}
else {
transform.Rotate(0, Input.GetAxis("Horizontal")*freeMovementTurnSpeed*Time.deltaTime, 0,Space.World);
}
if (vertPos > 0 && transform.position.y > maxHeight){
vertPos = 0;
}
else if(vertPos < 0 && transform.position.y < minHeight){
vertPos = 0;
}
print(transform.position.y);
transform.Translate(strafePos,vertPos,zPos);
}
function targetSelectedMovement() {
if(Input.GetKey("a") || Input.GetKey("d") ){
x += (-Input.GetAxis("Horizontal")) * xSpeed * 0.02;
var rotation = Quaternion.Euler(y, x, 0);
var currentDistance = Vector3.Distance(transform.position,target.position);
var position = rotation * Vector3(0.0, 0.0, -currentDistance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
var zPos = Input.GetAxis("Vertical") * Time.deltaTime * speed;
currentDistance = Vector3.Distance(transform.position,target.position);
if(currentDistance < maxDistance && zPos < 0){
transform.Translate(0, 0, zPos);
}
else if(currentDistance > minDistance && zPos > 0){
transform.Translate(0, 0, zPos);
}
if (Input.GetButton("Fire2")) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation2 = Quaternion.Euler(y, x, 0);
var currentDistance2 = Vector3.Distance(transform.position,target.position);
var position2 = rotation2 * Vector3(0.0, 0.0, -currentDistance2) + target.position;
transform.rotation = rotation2;
transform.position = position2;
}
}
static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
function Update () {
var hit : RaycastHit;
if (Input.GetButton("Fire1")) {
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
// Cast a ray forward from our position for the specified distance
if (Physics.Raycast(ray, hit))
{
// Add a specified force to the Rigidbody component to the other GameObject
if(hit.collider.tag == "Unit"){
print("Selected " + hit.collider.gameObject.name);
target = hit.collider.gameObject.transform;
//transform.LookAt(target);
followTarget = true;
}
else {
target = null;
}
}
}
}
:)
Answer pulled from my comments above:
"I'll have to look when I get on my dev. machine but in the mean time try replacing Input.GetButton("Fire1") with Input.GetButtonDown("Fire1"). Just as a test, because looking at your code nothing jumps out at me. GetButton is true as long as the button is held, in the past I have had issues with it incorrectly triggering just as the mouse moved off an object."