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.
Related
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 am making a 3D game but my player can only move on the X and Y axis. I have a player with an attached camera following my mouse, but I only want it to follow up to a max radius distance from Vector3.zero, even if my mouse is beyond those bounds.
I have tried repositioning the player to the max distance on radius every frame it tries to follow the mouse outside its bounds, but this causes camera jitters even in LateUpdate.
void LateUpdate()
{
if (Input.GetMouseButtonDown(0)) {
firstTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
playerPos = transform.position;
}
if (Input.GetMouseButton(0)) {
Ray currentTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
Vector2 direction = currentTouchPos.origin - firstTouchPos.origin;
float distance = Vector3.Distance(transform.position, Vector3.zero);
if (distance >= radius) {
targetPosition = direction.normalized * radius;
} else {
targetPosition = playerPos + direction * touchSensitivity;
}
}
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * followSpeed);
}
I'm thinking there must be a way to clamp the positioning of the player to a radius so that I don't have to "leash" him back by repositioning him through code every frame.
You should try using Vector3.ClampMagnitude().
The solution was to use Clamp Magnitude.
void LateUpdate()
{
if (Input.GetMouseButtonDown(0)) {
firstTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
playerPos = transform.position;
}
if (Input.GetMouseButton(0)) {
// targetPosition will follow finger movements
Ray currentTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
Vector2 direction = currentTouchPos.origin - firstTouchPos.origin;
targetPosition = playerPos + direction * touchSensitivity;
}
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * followSpeed);
transform.position = Vector3.ClampMagnitude(transform.position, radius);
}
I'm trying to write a very simple 3d model viewer that allows the user to click and drag on the x and y axes to rotate an object. The problem I am facing with my included code sample is that, when I rotate something, say, about the y axis, and then try to rotate about the x axis, I find that the object is rotated about the object's x axis instead of the x-axis from the perspective of the camera.
I'm effectively trying to simulate rotating something along the z-axis, albeit through two motions.
public Transform obj;
private Vector3 screenPoint;
private Vector3 offset;
//public float minX = 270.0f;
//public float maxX = 360.0f;
//public float minY = -90.0f;
//public float maxY = 90.0f;
public float sensX = 100.0f;
public float sensY = 100.0f;
float rotationY = 0.0f;
float rotationX = 0.0f;
float posX = 0.0f;
float posY = 0.0f;
void Update() {
if (Input.GetMouseButton(0)) {
rotationX += Input.GetAxis("Mouse X") * sensX * Time.deltaTime;
//rotationX = Mathf.Clamp(rotationX, minX, maxX);
rotationY += Input.GetAxis("Mouse Y") * sensY * Time.deltaTime;
//rotationY = Mathf.Clamp(rotationY, minY, maxY);
Quaternion q = Quaternion.Euler(rotationY, -rotationX, 0);
transform.rotation = q;
}
if (Input.GetMouseButton(1)) {
posX += Input.GetAxis("Mouse X") * 25.0f * Time.deltaTime;
posY += Input.GetAxis("Mouse Y") * 25.0f * Time.deltaTime;
transform.position = new Vector3(posX, posY, 0);
}
}
If you are looking to rotate around the z-axis, you could try the transform.RotateAround function. This will allow you to specify a point (as a Vector3), a rotation axis (again as a Vector3), and a degree to which to rotate. This function can modify both the position and the rotation elements of your transform.
I have this code below and works in Keyboard, but does not works with touch.
Quaternion rot = transform.rotation; float z = rot.eulerAngles.z;
z-= Input.GetAxis("Horizontal") * rotSpeed * Time.deltaTime;
rot = Quaternion.Euler( 0, 0, z );
transform.rotation = rot;
I need that code above works on touch, how to do this?
Maybe you should read the Api for input of Unity Scripting http://docs.unity3d.com/ScriptReference/Input.html
you could use the getTouch method to rotate the object
here the documentation
http://docs.unity3d.com/ScriptReference/Input.GetTouch.html
you can use the delta for draging or the Position for rotating the Object
so it would be like this not sure its working it should work with dragging
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
z-=touchDeltaPosition.x * rotSpeed * Time.deltaTime;
rot = Quaternion.Euler( 0, 0, z );
}
It should be nearly the same as above but instead of the x axis which is the horizontal one we are using the y axis for the vertical one this code is also with dragging
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
Vector3 pos = transform.position;
Vector3 velocity = new Vector3(0, touchDeltaPoition.y * maxSpeed * Time.deltaTime, 0);
pos += rot * velocity;
}
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;
}
}