Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 vvega511 · Sep 28, 2017 at 11:41 AM · rotationmovementsnake

Make an object face forward in a snake game.

I think my issue is fairly simple but after several days of searching I have either been unable to find a solution or if I did find the correct one I have been unable to implement it properly in my code, so now I have finally broken down and have decided to ask for help.

I am making a snake-like game but I am using 3d models. The problem is that when I turn the head of the snake to a new direction I can't get it to also face in that direction. I have tried every rotation and eulerangle code have come across but none of them do what I need. I have been able to get the head object to rotate with some of them but then the control inputs don't move the head in the correct direction.

This is the code I have been using:

 public class Snake : MonoBehaviour {
 
     Vector3 dir = Vector3.left;
 
 
     List<Transform> tail = new List<Transform>();
 
     bool ate = false;
 
     public GameObject tailPreFab;
 
   
     void Start () {


      InvokeRepeating("Move", 0.3f, 0.5f); 
 
 
     }
     
 
     void Update () {
   
         if (Input.GetKey (KeyCode.A)) {
             dir = Vector3.right;
 
         } if (Input.GetKey (KeyCode.W)) {
             dir = Vector3.back; 
 
         } if (Input.GetKey (KeyCode.D)) {
             dir = Vector3.left; 
 
         } if (Input.GetKey (KeyCode.S)) {
             dir = Vector3.forward;
 
         }
     
     }

     public void Move () {
 
 
         Vector3 v = transform.position;
 //        Quaternion q = transform.rotation;
 
 
         transform.Translate (dir);
 
 
         if (ate) {
             GameObject g = (GameObject)Instantiate (tailPreFab, v, Quaternion.identity);
 
             tail.Insert (0, g.transform);
 
             ate = false;
         }
 
         if (tail.Count > 0) {
             tail.Last ().position = v;
 
             tail.Insert (0, tail.Last ());
             tail.RemoveAt (tail.Count - 1);
 
         }       
     }
 
     void OnTriggerEnter(Collider coll) {
 
         if (coll.name.StartsWith("Food")) {
             ate = true;
             Destroy (coll.gameObject);
         }
         if (coll.name.StartsWith("Wall")) {
             CancelInvoke ("TimerInvoke");
             Exit ();
         }
         if (coll.name.StartsWith("Chick")) {
             CancelInvoke ("TimerInvoke");
             Exit ();
         }
     }   
 
     public void Exit () {
         SceneManager.LoadScene (1);
     }   
 }

I suspect my issues might stem from using dir = Vector3.(direction) but I can't figure out a better way to control the direction of the snake.

Sorry is this is vague or if the answer is obvious. This is the first project I have taken on myself so my knowledge of Unity and C# are still pretty weak.

Thanks in advance.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Runemark · Sep 28, 2017 at 12:55 PM

Easiest way:

 transform.Translate (dir);
 
 // Add this part
 Vector3 lookPos = transform.position + dir * 100;
 transform.LookAt(lookPos)

This will make your transform look at the point that is 100 unit far from it in the given direction.

Comment
Add comment · Show 2 · 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 vvega511 · Sep 28, 2017 at 04:40 PM 0
Share

I just tried this one my lunch break and it reacted similarly to the other things I have tried. It does rotate the object but it also messes up the direction the object moves when I use WASD.

avatar image Runemark · Sep 29, 2017 at 09:11 AM 0
Share

I imagined your game like the classic sneak game, where the camera is looking to the playground from top. If you are using a camera that follows the sneak head, you have to change the direction from global direction vectors to local ones.

You can do it by changing Vector3.forward to transform.forward, and the same with other directions. (If I misunderstood the problem, sorry its early in the morning. :D also pls attach an image about your game screen if this is the case! :))

avatar image
0

Answer by knorke · Sep 28, 2017 at 12:28 PM

Maybe this will help you out:
https://unity3d.com/de/learn/tutorials/topics/multiplayer-networking/creating-player-movement-single-player

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.

Follow this Question

Answers Answers and Comments

123 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Rigidbody.MovePosition doesn't move reliably? 1 Answer

World and Local Axis out of Alignment 1 Answer

Rotation and movement applied to other objects 2 Answers

Curve player movement 1 Answer

How can I get my character to move forward in the direction of the camera? 2 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