A bit of dumb confusion here. You want to be able to shoot at where your mouse is pointing, but you don’t want your player to be facing in that exact direction.
I say this, because your player rotates according to your A and D keyboard controls, but what you are asking for is that you be able to shoot in any direction based on where you click on the screen?
An addition to your script not requested:
function LateUpdate (){
if(dead)
StartCoroutine(DeadCoroutine());
}
function DeadCoroutine(){
endTime=Time.time + 5.0;
Camera.main.transform.position = transform.TransformPoint(Vector3(0,4,-10));
Camera.main.transform.LookAt(transform.position);
while(Time.time < endTime){
yield;
}
dead = false;
}
This will allow you to pause 5 seconds before you magically come back to life.
Learning how to set it up yourself will help you understand how it works to manipulate it however you like, but out of the box it instantiates an object and shoots it forward at a speed based on a constant variable.
Ah, this is the point where I say, no… you must try it, you must post script here and ask questions about it. I will not write script out and post it so you can “NOT” learn how to do it yourself.
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) {
Debug.DrawLine (ray.origin, hit.point);
}
This one discribes a Raycast with a hit output. Distance is really not necessary.
I looked all links you gave me and i’m tring to find a way to make my character rotate only x(left/right) by mouse
and i tiped the script like you told me but it doesn’t do anything(for me).Here’s the picture.I made this as example.
Has anybody tried the FPS tutorial from above?
It doesn’t work and isn’t linked on their webpage.
Comes under the heading NFT! Not Fully Tested.
Unity is selling products and is not keeping up with support.
I would not spend money yet.
I am just about to bail on this product.
I use the parts I quoted from it above in my game, I just took a look at the tutorial and the PDF documents work fine. That’s all you need, it has step by step instructions for implementing projectiles, switching weapons, etc. the code I use I copied from the pdf and pasted into new scripts per the instructions.
Here first get a cursor in game, not using gui types.
function Update() {
var currentCamera : Camera = Camera.current;
if( !currentCamera ) return;
var cursorScreen : Vector3 = Input.mousePosition;
var cursorRay : Ray = currentCamera.ScreenPointToRay( cursorScreen );
var cursorHit : RaycastHit;
// first try raycast.
if( Physics.Raycast( cursorRay, hit ) ){
// got a hit on collider. use that.
transform.position = hit.point;
// rotate it to the normal. ( optional )
transform.rotation = Quaternion.LookRotation( hit.normal, currentCamera.transform.up );
} // if no hit just do a plane raycast
else{
float distance;
if( ( Plane(Vector3.up, 0).Raycast( cursorRay, distance ) ){
transform.position = cursorRay.GetPoint( distance );
transform.rotation = Quaternion.LookRotation( Vector3.up, currentCamera.transform.up );
}
}
}
Apply some renderer/mesh to a object with the script to test it. Once you’ve got that working then its simply just having your player look at it.
I don’t use JS so there might be some errors. But it should be pretty straight forward.
You should then set the process order of this script above others ( so it runs update prior to anything else )