Search Unity

Projectile firing toward player's forward vector

Discussion in 'Scripting' started by siflandolly, Apr 22, 2012.

  1. siflandolly

    siflandolly

    Joined:
    May 17, 2011
    Posts:
    141
    I'm having trouble with the ShootFireball method at the bottom. It always shoots toward the world axis forward. I want the fireball's forward to be the same as the player's local forward. Can anyone offer some help?

    Code (csharp):
    1.  
    2.  
    3.  
    4. using UnityEngine;
    5. using System.Collections;
    6.  
    7. public class Player: MonoBehaviour {
    8.     public Camera myCamera;
    9.     private Vector3 targetPosition;
    10.     public float smooth = 1f;
    11.     private CharacterController cc;
    12.     private bool attacking;
    13.     public float height, back;
    14.     private GameObject currentEnemy;
    15.     void Start() {
    16.         targetPosition = this.transform.position;
    17.         cc = this.gameObject.GetComponent<CharacterController>();
    18.         myCamera.transform.position = transform.position + (Vector3.up * height) + (Vector3.back * back);
    19.         attacking = false;
    20.     }
    21.     void Update() {
    22.         int fingerCount = 0;
    23.        
    24.         foreach (Touch touch in Input.touches) {
    25.             if (touch.phase != TouchPhase.Ended  touch.phase != TouchPhase.Canceled) {
    26.                 //fingerCount++;
    27.                
    28.                 Vector3 touchPos = new Vector3(touch.position.x, touch.position.y, 0f);
    29.                 OnTouchOrClick(touchPos);
    30.                
    31.             }            
    32.         }
    33.        
    34.         if(Input.GetKeyDown(KeyCode.Mouse0))
    35.         {
    36.             OnTouchOrClick(Input.mousePosition);
    37.         }      
    38.        
    39.         if(Input.GetKeyDown(KeyCode.Mouse1) || Input.GetKeyDown(KeyCode.LeftAlt)) {
    40.            
    41.             OnRightClick(Input.mousePosition);
    42.         }
    43.        
    44.         if (Input.GetKeyDown(KeyCode.LeftShift)) {
    45.             ShootFireball(Input.mousePosition);
    46.         }
    47.        
    48.         //transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth); 
    49.         Vector3 dir = targetPosition - transform.position ;
    50.        
    51.        
    52.        
    53.        
    54.         if (Vector3.Distance(transform.position, targetPosition) > 2f) {
    55.             this.animation.Play("Sword_Walk");
    56.             this.animation.wrapMode = WrapMode.Loop;
    57.             this.transform.LookAt(targetPosition);
    58.            
    59.             //cc.SimpleMove(dir.normalized * Time.deltaTime * smooth);
    60.             cc.SimpleMove(transform.forward * Time.deltaTime * smooth);
    61.             myCamera.transform.position = transform.position + (Vector3.up * height) + (Vector3.back * back);
    62.         }
    63.         else {
    64.             if (!attacking) {
    65.                 animation.CrossFade("CombatIdle");
    66.                 animation.wrapMode = WrapMode.Loop;
    67.             }
    68.             else {
    69.                 StartCoroutine(Attack());
    70.             }
    71.             //animation.Stop();
    72.         }
    73.        
    74.     }
    75.    
    76.     private IEnumerator Attack() {
    77.        
    78.         transform.LookAt(currentEnemy.transform.position);
    79.        
    80.         animation.Play("Sword_Swing_High_Straight_Down");
    81.         animation.wrapMode = WrapMode.Once;
    82.         yield return new WaitForSeconds(.6f);
    83.        
    84.         if (currentEnemy) {
    85.             if (Vector3.Distance(transform.position, currentEnemy.transform.position) < 4f  attacking) {
    86.                 currentEnemy.GetComponent<Skeleton>().GotHit();        
    87.                 currentEnemy.GetComponent<Skeleton>().health -= 10;
    88.                 //Debug.Log("swing");              
    89.             }
    90.         }      
    91.         attacking = false;         
    92.     }
    93.    
    94.     public void GotHit() {
    95.         animation.CrossFade("Duck_Below_High_Swing");
    96.         animation.wrapMode = WrapMode.Once;
    97.     }
    98.    
    99.     private void OnRightClick(Vector3 inputPostition) {
    100.        
    101.         Plane playerPlane = new Plane(Vector3.up, transform.position);
    102.         Ray ray = myCamera.ScreenPointToRay(inputPostition);
    103.         float hitdist = 0.0f;
    104.         RaycastHit rayHit;
    105.         Physics.Raycast(ray, out rayHit);
    106.         if (playerPlane.Raycast (ray, out hitdist)) {
    107.             Vector3 targetPoint = ray.GetPoint(hitdist);
    108.             float height = Terrain.activeTerrain.SampleHeight(ray.GetPoint(hitdist));
    109.             Vector3 location = new Vector3(targetPoint.x, height, targetPoint.z);
    110.            
    111.             GameObject fireWall = (GameObject) Instantiate(Resources.Load("Firewall"));
    112.             fireWall.transform.position = location;
    113.             //Debug.Log(fireWall.transform.position);
    114.         }
    115.     }
    116.    
    117.     private void OnTouchOrClick(Vector3 inputPostition) {
    118.         Plane playerPlane = new Plane(Vector3.up, transform.position);
    119.         Ray ray = myCamera.ScreenPointToRay(inputPostition);
    120.         float hitdist = 0.0f;
    121.         RaycastHit rayHit;
    122.         Physics.Raycast(ray, out rayHit);
    123.        
    124.         if (rayHit.collider.gameObject.name == "mSkeleton") {
    125.             Debug.Log("Clicked on Skeleton");
    126.             attacking = true;
    127.             currentEnemy = rayHit.collider.gameObject;
    128.         }
    129.         else {
    130.             if (playerPlane.Raycast (ray, out hitdist)) {
    131.                 Vector3 targetPoint = ray.GetPoint(hitdist);
    132.                 float height = Terrain.activeTerrain.SampleHeight(ray.GetPoint(hitdist));
    133.                 targetPosition = new Vector3(targetPoint.x, height, targetPoint.z);
    134.             }
    135.         }
    136.     }
    137.    
    138.     void ShootFireball(Vector3 inputPostition) {       
    139.            
    140.             GameObject fireball = (GameObject) Instantiate(Resources.Load("Fireball"));
    141.             fireball.transform.position = this.transform.position;
    142.             fireball.transform.rotation= this.transform.rotation;  
    143.        
    144.     }
    145. }
    146.  
     
  2. siflandolly

    siflandolly

    Joined:
    May 17, 2011
    Posts:
    141
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The fireball will have the rotation of whatever the script is attached to when it's instantiated. By the way, you can just write "transform.position", you don't need "this.transform.position". I'd also recommend generally not using Resources.Load; hook prefabs up with public variables instead. That allows Unity to manage metadata properly instead of you having to remember to change your scripts whenever you change something in the project, and protects against typos in strings which the compiler can't detect. Also, you can supply the position and rotation when using Instantiate, so you don't need to assign Instantiate to a variable here.

    Code (csharp):
    1. Instantiate(fireballPrefab, transform.position, transform.rotation);
    --Eric
     
  4. siflandolly

    siflandolly

    Joined:
    May 17, 2011
    Posts:
    141
    Thanks for the advice. It was actually a dumb mistake.

    I put transform.position += Vector3.forward * speed * Time.deltaTime;

    instead of

    transform.position += transform.forward * speed * Time.deltaTime;

    in the script that moves the fireball forward