• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by titovelosa · Jan 25, 2019 at 05:41 PM · 2d game2d-physics2d sprites2d rotation

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tormentoarmagedoom · Jan 25, 2019 at 05:52 PM 2
Share

Try with LookAt() function

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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);
     }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image titovelosa · Jan 25, 2019 at 07:46 PM 0
Share

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

avatar image
0

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'

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

118 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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 make a 2d character three-dimensional? 1 Answer

Best approach for 2d animation to interact with the environment? 0 Answers

Why Is there A slight jitteryness on player movement and camera? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges