How can I have a directional projectile coming out? - unity3d

This problem is because when I want instantiating the projectile it doesnt come out the same way as the direction

Assuming your Angle is the same angle you want the projectile to have:
transform.rotation = Quaternion.AngleAxis(Angle, Vector3.forward);
If it's facing 90 degrees away from the correct direction, either rotate the sprite to face upwards, or try adding/substracting 90 to/form Angle like:transform.rotation = Quaternion.AngleAxis(Angle - 90, Vector3.forward);

Rewrite your CastSpell function to the following and tell me if it works:
void CastSpell(int slot) {
var spellObject = Instantiate(spellPrefab, transform.position, Quaternion.identity);
var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
var dir = mousePos - transform.position;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg + 90;
spellObject.transform.rotation = Quaternion.AngleAxis(angle - 90, Vector3.forward);
spellObject.GetComponent<SpellController>().srcPlayerID = playerID;
StartCoroutine(SpellCooldown(slot, gameConstants.spellCooldown));
}

Related

Keep camera from rotating around relative z-axis

I'm trying to create camera movement that mimics the behavior of the unity scene editor where you can perform a spherical rotation around the scene with 2d mouse movement. So far the camera is rotating correctly given x or y movement, but dragging diagonally causes the camera to rotate around its relative z-axis until it gets locked. I cannot force the camera to look at the origin relative to world up because then it cannot rotate upside-down
Here is the script that I've attached to the camera
using UnityEngine;
public class MainCamera : MonoBehaviour
{
Vector2 startingPosition;
Vector2 mousePosition;
Vector3 orthogonalCameraVector;
float degreesPerUnitWidth = 180f / Screen.width;
float degreesPerUnitHeight = 180f / Screen.height;
float cameraRadius = 10;
void Start()
{
transform.position = new Vector3(0, 0, -cameraRadius);
transform.LookAt(Vector3.zero);
orthogonalCameraVector = -Vector3.left;
}
void LateUpdate()
{
if (Input.GetMouseButtonDown(0))
{
mousePosition = Input.mousePosition;
}
if (Input.GetMouseButton(0))
{
var input = new Vector2(Input.mousePosition.x ,Input.mousePosition.y);
startingPosition = mousePosition;
var mouseDelta = startingPosition - input;
var xzDegrees = -mouseDelta.y * degreesPerUnitHeight;
var xzRotation = Quaternion.AngleAxis(xzDegrees, orthogonalCameraVector); // rotate about the relative x-z plane
var yRotation = Quaternion.AngleAxis(-mouseDelta.x * degreesPerUnitWidth, Vector3.up); // rotate about the world y-axis
var rotation = xzRotation * yRotation;
orthogonalCameraVector = rotation * orthogonalCameraVector;
transform.position = rotation * transform.position;
transform.rotation = rotation * transform.rotation;
mousePosition = Input.mousePosition;
}
}
}
I realized the orthogonalCamerVector only needed to be rotated about the y-axis because I wanted to keep it parallel with the xz-plane. I then applied this rotation to the orthogonalCamerVector before rotating the transform so it would always be rotating about a fixed plane so it looks something like this:
var xzDegrees = -mouseDelta.y * degreesPerUnitHeight;
var yRotation = Quaternion.AngleAxis(-mouseDelta.x * degreesPerUnitWidth, Vector3.up); // rotate about the world y-axis
orthogonalCameraVector = yRotation * orthogonalCameraVector;
var xzRotation = Quaternion.AngleAxis(xzDegrees, orthogonalCameraVector); // rotate about the relative x-z plane
var rotation = xzRotation * yRotation;
transform.position = rotation * transform.position;
transform.rotation = rotation * transform.rotation;

Unity 3d main camera LookAt is not rotating on x axis

Am developing a follow target camera, it's working fine but when the vehicle(Target) is on slope, the camera is not rotating to show the full vehicle.
void LateUpdate()
{
if (car1.controlled && Camera.main != null)
{
float speedFactor = Mathf.Clamp01(target.root.GetComponent<Rigidbody>().velocity.magnitude / 20.0f);
if (speedFactor < 0.01f)
speedFactor = 0.01f;
Camera.main.fieldOfView = Mathf.Lerp(40, 65, speedFactor);
float currentDistance = Mathf.Lerp(13.5f, 8.5f, speedFactor);
currentVelocity = currentVelocity.normalized;
Vector3 newTargetPosition = target.position + Vector3.up * height;
Vector3 newPosition = newTargetPosition - ((currentVelocity * currentDistance));
newPosition.y = newTargetPosition.y;
Vector3 targetDirection = newPosition - newTargetPosition;
if (Physics.Raycast(newTargetPosition, targetDirection, out hit, currentDistance, raycastLayers))
newPosition = hit.point;
Camera.main.transform.position = newPosition;
Camera.main.transform.LookAt(newTargetPosition);
}
LookAt is going to set the camera rotation to be looking straight at the newTargetPosition. It doesn't actually moves the camera up, it simply rotates it to look at the vehicule. You should modify your code so the camera moves up when the vehicule goes down a slope and then make it look at the vehicule.

Rotate object to mouse direction

I have this script, that already works
I,m using leanFinger
var c = Camera.main;
var center = rb.position;
var lastPos = _finger.GetWorldPosition(c.transform.position.y, c);
var lastDelta = Vector3.Distance(center, lastPos);
float angle = Mathf.Atan2(lastPos.x, lastPos.z) * Mathf.Rad2Deg;
rb.rotation = Quaternion.Euler(new Vector3(0, angle - initialRotation, 0));
The problem is that my object is rotating in absolute angles, so if it is already rotated, there is a unwanted rotation. I want it to take an initialRotation value, but i don't know how.
Thanks
You can use transform.rotation to access the current rotation of your object: Transform.rotation
You could also use this method for convenience: Transform.LookAt
Simple JS example:
public var target : Transform;
function Update ()
{
transform.LookAt(target);
}

Raycast rotate help need in unity

I have this code which lets an object rotate on Y axis. But I want to rotate the object on Z axis only. I tried to change the hPlane value, but no luck.
How can I do this?
Thanks
private Ray ray;
private Quaternion target;
private float speed = 1000.0f;
// Update is called once per frame
void Update () {
if (Input.GetMouseButton (0)) {
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
Plane hPlane = new Plane(Vector3.up, Vector3.zero);
float distance = 0;
if(hPlane.Raycast(ray, out distance)){
Vector3 targetPoint = ray.GetPoint(distance);
targetPoint += transform.position;
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
float step = speed * Time.deltaTime;
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation,step);
}
}
}
Update:
My Goal is to rotate a bar (cube mesh) on Z axis smoothly using mouse click . The rotation will occur only when (this part I could not do) the mouse touch the corners of the bar.
I have already done the Z axis rotation with this code but the rotation very low (even for speed =1000.0f) and it rotates clockwise I suppose. But I want to move the bar in both direction and the movement will be smooth and faster like Quaternion.RotateTowards
Thanks
private Ray ray;
private RaycastHit hit;
private Quaternion target;
private float speed = 1000.0f;
// Update is called once per frame
void Update () {
if (Input.GetMouseButton (0)) {
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if(Physics.Raycast(ray, out hit)){
//transform.position = new Vector3(hit.point.x, hit.point.y, 0);
target = Quaternion.Euler(0.0f,0.0f, hit.point.z * speed);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime);
}
}
}
http://i.stack.imgur.com/7qIcs.jpg
I suggest you to change the orientation of the object axis. For a better use of the Quaternion functions, edit your bar in order to have local Y has rotation axis and local Z as look direction.
Then use this code :
if (Input.GetMouseButton(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Vector3 distance = hit.point - transform.position;
distance.y = 0;
Quaternion targetRotation = Quaternion.LookRotation(distance);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime * speed);
}
}
Your bar will only rotate around Y axis.

Unity OnMouseDrag too fast drag issue

I have two sprites and I am rotating them on mouse drag. When I rotate one, another has to rotate also but a bit slower. The issue is when I move first sprite too fast. I guess Unity skips some points where I ask for current rotation of object. Here is the code:
if(objekatKliknut=="Minute"){
mouseClickPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 dir = mouseClickPos - transform.position;
angle = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg;
angle-=90;
if (angle < 0.0f) angle += 360.0f;
angle = Mathf.Round(angle/6.0f)*6.0f;
if(angle==360) angle=0;
hand1.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
a1=angle;
if(a1!=a2){
float x = Mathf.Abs(a1-a2);
if(a1>a2){
moveRight = false;
if(x==6){
handRot+=addAngle*x/6f;
globeRot-=addGlobeAngle;
}
a2=a1;
}
else if(a1<a2){
moveRight = true;
if(x==6){
handRot-=addAngle*(x/6f);
globeRot+=addGlobeAngle*(x/6f);
}
a2=a1;
}
}
hand.transform.rotation = Quaternion.AngleAxis(handRot, Vector3.forward);
oblaci.transform.rotation = Quaternion.AngleAxis(globeRot, Vector3.forward);
}
hand1 is first object that I am rotating, and hand is the second one that needs to be rotated relative to the first one.
Can someone please help me?