- Home /
 
Shooter 2D - Problem with rotation of bullet to face direction (velocity)
I'm new and unity and i'm trying to do a 2d Shooter with ricochet effects only using Unity Phisics. Now i iam on this point: Example: https://ibb.co/ZmWPpqP
I was trying to rotate the bullet to face it velocity vector. I don`t know if i was doing right but now i have this error:
Error CS1503 Argument 1: cannot convert from 'float' to 'UnityEngine.Vector3'
My bullet script:
     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
  
     public class Bullet : MonoBehaviour
     {
              public float speed = 20f;
              public Rigidbody2D rb;
  
     void Start()
     {
         rb.velocity = transform.right * speed;
         transform.rotation = Quaternion.SetLookRotation(speed);
     }
     }
 
               Any sugestions to fix that, please?
Hi, @tormentoarmagedoom , can you help me with that function?
I modified my script like this:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Bullet : $$anonymous$$onoBehaviour
 {
     public float speed = 20f;
     //public Rigidbody2D rb;
     public Rigidbody2D myRigidbody;
 
 
     // Start is called before the first frame update
     void Start()
     {
         this.myRigidbody = this.GetComponent<Rigidbody2D>();
         this.myRigidbody.velocity = transform.right * speed;
         transform.LookAt(transform.position + this.myRigidbody.velocity);
     }
 }
 
                   But now i have this error: Error CS0034 Operator '+' is ambiguous on operands of type 'Vector3' and 'Vector2'
Answer by triis4924 · Jan 25, 2019 at 06:21 PM
If you want an object facing to the mouse, use this:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class mouserot : MonoBehaviour {
 
     Camera viewCamera;
     private float angle;
     Vector2 mousePos;
     
     void Start ()
     {
         viewCamera = Camera.main;
     }
     
     
     void Update ()
     {
         mousePos = viewCamera.ScreenToWorldPoint(Input.mousePosition) - transform.position;
         angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.AngleAxis(angle, Vector3.forward), 1000 * Time.deltaTime);
     }
 
              The character shoots well in the direction of the mouse pointer. $$anonymous$$y problem is rotating the bullet sprite after the ricochet in the new direction
Answer by titovelosa · Jan 26, 2019 at 05:04 PM
I modified my bullet script like this:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Bullet : MonoBehaviour
 {
     public float speed = 20f;
     //public Rigidbody2D rb;
     public Rigidbody2D myRigidbody;
 
 
     // Start is called before the first frame update
     void Start()
     {
         this.myRigidbody = this.GetComponent<Rigidbody2D>();
         this.myRigidbody.velocity = transform.right * speed;
         transform.LookAt(transform.position + this.myRigidbody.velocity);
     }
 }
 
               But now i have this error: Error CS0034 Operator '+' is ambiguous on operands of type 'Vector3' and 'Vector2'
Your answer
 
             Follow this Question
Related Questions
My player is not rotating upwards when it moves upwards. Any advice? 1 Answer
Slow Sprite Rotation Toward Movement Direction - Top-Down 2D Game 0 Answers
How to create soil or sand?? 1 Answer
2D Sprite drifting down, otherwise passes through walls - how do I fix these issues? 0 Answers
Instantiate a GameObject with a specific Z rotation 2 Answers