- Home /
Create object for orthographic camera
Hi When I use the code for the camera "Perspective" is a missile shoots in place of touch but it is not very accurate. I thought the solution would be to set the camera "orthographic". But at the touch creates an object not beating no way the Z axis - creates a short distance from the camera. Skypt adds to the camera: using UnityEngine; using System.Collections;
public class tests : MonoBehaviour {
public Rigidbody bullet;
public float power = 1500f;
public float moveSpeed = 2f;
void Update ()
{
for (int i = 0; i < Input.touchCount; i++)
{
if (Input.GetTouch(i).phase == TouchPhase.Began) {
Vector3 touchPos = Input.GetTouch(i).position;
touchPos.z = 4f;
var createPos = Camera.main.ScreenToWorldPoint(touchPos);
Rigidbody instance =Instantiate (bullet, createPos, Quaternion.identity)as Rigidbody;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
instance.AddForce(fwd * power);
}
}
}
}
How to achieve that fired from the place touched and flew the Z axis?
Your code looks okay (and should work for both orthographic and perspective cameras). Are you sure the pivot point of your bullet prefab is at the center of the object? Note you are firing your bullet in the direction the game object that his this script is facing.
@robertbu I have no model, only the cube so I think, it does not pivot point.
When I turn off the gravity at the camera "Ortographics" it shoots bullets. I have no idea what is wrong ...
Any chance your bullets are hitting some other object as they are being fired?
@robertbu Certainly not collide with something else. For the test I set to bullet time on the Y axis and is fine. I have no idea why does not work on the Z axis
Answer by zharik86 · Sep 21, 2014 at 07:07 AM
In your code force by name "fwd" works along the camera axis "z". Then you won't see movement on the axis "z" as for the orthographic camera objects doesn't change the sizes from distance to it. But movement will be. If you want to see simply movement change the vector direction.
Your answer

Follow this Question
Related Questions
Searching GameObject with in 5 feet 1 Answer
Android Touch Input Help! 1 Answer
Blender vs Mudbox Workflow for Games 1 Answer
How to anchor a (Image, Button, Slider) to Grid create by a script? 0 Answers
Unity Wont show up on Mac 2 Answers