aim

Hi guys i need a script that makes my fireballs shoot a my mouse position
this is a script:

var speed : float = 3;
var rotateSpeed : float = 3;
var bulletPrefab : Transform ;
private var dead = false;

function OnControllerColliderHit(hit : ControllerColliderHit)
{
		if(hit.gameObject.tag == "fallout")
		{
			dead = true;
		}

}


function Update() 
{
    var controller : CharacterController = GetComponent( CharacterController );
    transform.Rotate( 0, Input.GetAxis( "Horizontal") * rotateSpeed, 0 );
    var forward = transform.TransformDirection( Vector3.forward );
    var curSpeed = speed * Input.GetAxis( "Vertical" );
    controller.SimpleMove( forward * curSpeed );
	
	if(Input.GetButtonDown("Fire1"))
	{
		var bullet = Instantiate(bulletPrefab, GameObject.Find("spawnPoint").transform.position, 
		Quaternion.identity );
		
		bullet.rigidbody.AddForce(transform.forward * 2500);
	
	}
	
}

function LateUpdate ()
{
	if(dead)
	{
		transform.position = Vector3(0,4,0);
		gameObject.Find("Main Camera").transform.position = Vector3(0,4,-10);
		dead = false;

	}
}
	

@script RequireComponent( CharacterController )

Look at Raycasting in the script reference, it’s got examples

Can you give me the script please???

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.

Ok thanx but do you know the script for shooting at clicking spot any script just to shoot at clicking spot?

sure, there is a wonderful set of examples in the documentation…

First, get a point where you want to shoot.

(pay attention to the section with ScreenPointToRay)

This will give you a point in space where you bullet should be going.

You should have a spawning point at the tip of your gun which tells you where you are shooting from.

Now simply instantiate a bullet there

Tell it to “LookAt” the point where the Raycast hit.

it should have a rigidbody

you tell hte rigidbody’s velocity to equal

bullet.transform.forward * bulletSpeed;

This forces the bullet to go in that direction.

I tried but it’s to confusion for me :(, but thanx for tring to help is there any way you can write a script for me plz?

This has what you want without ray casting. Specifically the MissileLauncher.js and ReceiveDamage.js code it walks you through making.

The pdf documents walk you through every step you need to do and is very easy to follow even for a beginner.

http://unity3d.com/support/resources/tutorials/fpstutorial.html

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.

Also you can use this package http://unity3d.com/support/resources/unity-extensions/explosion-framework.html to add cool explosions to whatever projectile you shoot.

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.

One question on this link Unity - Scripting API: Physics.Raycast
shoud i write whole the scripts ?

This is the one you want:

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.


And thanx for all.

And does anybody know how to resize a gui texture?

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.

Works great. I even use it with my AI to shoot at the player like this: http://www.youtube.com/watch?v=cDxFMVybaBA

I can’t find anything to help me then i’ll just rotate it with my (a,d). :frowning:

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 )

Thank you all but i got it working now. And one more question how do i change a look of my mouse?