Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

aim

Discussion in 'Scripting' started by The3DKnight, Aug 17, 2011.

  1. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Hi guys i need a script that makes my fireballs shoot a my mouse position
    this is a script:
    Code (csharp):
    1. var speed : float = 3;
    2. var rotateSpeed : float = 3;
    3. var bulletPrefab : Transform ;
    4. private var dead = false;
    5.  
    6. function OnControllerColliderHit(hit : ControllerColliderHit)
    7. {
    8.         if(hit.gameObject.tag == "fallout")
    9.         {
    10.             dead = true;
    11.         }
    12.  
    13. }
    14.  
    15.  
    16. function Update()
    17. {
    18.     var controller : CharacterController = GetComponent( CharacterController );
    19.     transform.Rotate( 0, Input.GetAxis( "Horizontal") * rotateSpeed, 0 );
    20.     var forward = transform.TransformDirection( Vector3.forward );
    21.     var curSpeed = speed * Input.GetAxis( "Vertical" );
    22.     controller.SimpleMove( forward * curSpeed );
    23.    
    24.     if(Input.GetButtonDown("Fire1"))
    25.     {
    26.         var bullet = Instantiate(bulletPrefab, GameObject.Find("spawnPoint").transform.position,
    27.         Quaternion.identity );
    28.        
    29.         bullet.rigidbody.AddForce(transform.forward * 2500);
    30.    
    31.     }
    32.    
    33. }
    34.  
    35. function LateUpdate ()
    36. {
    37.     if(dead)
    38.     {
    39.         transform.position = Vector3(0,4,0);
    40.         gameObject.Find("Main Camera").transform.position = Vector3(0,4,-10);
    41.         dead = false;
    42.  
    43.     }
    44. }
    45.    
    46.  
    47. @script RequireComponent( CharacterController )
    48.  
     
    Last edited: Aug 18, 2011
  2. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,685
    Look at Raycasting in the script reference, it's got examples
     
  3. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Can you give me the script please???
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    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:

    Code (csharp):
    1.  
    2. function LateUpdate (){
    3.     if(dead)
    4.         StartCoroutine(DeadCoroutine());
    5. }
    6.  
    7. function DeadCoroutine(){
    8.     endTime=Time.time + 5.0;
    9.     Camera.main.transform.position = transform.TransformPoint(Vector3(0,4,-10));
    10.     Camera.main.transform.LookAt(transform.position);
    11.     while(Time.time < endTime){
    12.         yield;
    13.     }
    14.     dead = false;
    15. }  
    16.  
    This will allow you to pause 5 seconds before you magically come back to life.
     
  5. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Ok thanx but do you know the script for shooting at clicking spot any script just to shoot at clicking spot?
     
  6. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    sure, there is a wonderful set of examples in the documentation..

    First, get a point where you want to shoot.

    http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html

    (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

    http://unity3d.com/support/documentation/ScriptReference/Object.Instantiate.html

    Tell it to "LookAt" the point where the Raycast hit.

    http://unity3d.com/support/documentation/ScriptReference/Transform.LookAt.html

    it should have a rigidbody

    http://unity3d.com/support/documentation/ScriptReference/Rigidbody.html

    you tell hte rigidbody's velocity to equal

    bullet.transform.forward * bulletSpeed;

    This forces the bullet to go in that direction.
     
  7. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    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?
     
  8. lmbarns

    lmbarns

    Joined:
    Jul 14, 2011
    Posts:
    1,473
    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.
     
  9. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    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.
     
  10. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Last edited: Aug 17, 2011
  11. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    This is the one you want:
    Code (csharp):
    1.  
    2. var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    3. var hit : RaycastHit;
    4. if (Physics.Raycast (ray, hit, 100)) {
    5. Debug.DrawLine (ray.origin, hit.point);
    6. }
    7.  
    This one discribes a Raycast with a hit output. Distance is really not necessary.
     
  12. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    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. $untitled.JPG
    And thanx for all.
     
    Last edited: Aug 18, 2011
  13. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    And does anybody know how to resize a gui texture?
     
  14. Overunity3d

    Overunity3d

    Joined:
    Sep 5, 2010
    Posts:
    14
    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.
     
  15. lmbarns

    lmbarns

    Joined:
    Jul 14, 2011
    Posts:
    1,473
    ???

    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
     
  16. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    I can't find anything to help me then i'll just rotate it with my (a,d). :(
     
  17. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    Here first get a cursor in game, not using gui types.

    Code (csharp):
    1.  
    2.  
    3. function Update() {
    4.     var currentCamera : Camera = Camera.current;
    5.     if( !currentCamera ) return;
    6.  
    7.     var cursorScreen : Vector3 = Input.mousePosition;
    8.     var cursorRay : Ray = currentCamera.ScreenPointToRay( cursorScreen );
    9.     var cursorHit : RaycastHit;
    10.     // first try raycast.
    11.     if( Physics.Raycast( cursorRay, hit ) ){
    12.         // got a hit on collider. use that.
    13.         transform.position = hit.point;
    14.         // rotate it to the normal. ( optional )
    15.         transform.rotation = Quaternion.LookRotation( hit.normal, currentCamera.transform.up );
    16.     } // if no hit just do a plane raycast
    17.     else{
    18.          float distance;
    19.          if( ( Plane(Vector3.up, 0).Raycast( cursorRay, distance ) ){
    20.              transform.position = cursorRay.GetPoint( distance );
    21.              transform.rotation = Quaternion.LookRotation( Vector3.up, currentCamera.transform.up );
    22.          }
    23.     }
    24. }
    25.  
    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 )
     
  18. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Thank you all but i got it working now. And one more question how do i change a look of my mouse?