• 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 Hemin11 · Oct 16, 2020 at 07:58 AM · 3d model

I want to rotate my chess pieces to -90° as they are spawning horizontally.

using System.Collections; using UnityEngine; using System.Collections.Generic;

public class BoardManager : MonoBehaviour {

 private const float TILE_SIZE = 1.0f;
 private const float TILE_OFFSET = 0.5F;

 private int selectionX = -1;
 private int selectionY = -1;

 public List<GameObject> chessmanPrefabs;
 private List<GameObject> activeChessman = new List<GameObject>();

 private void Start()
 {
     SpawnAllChessmans();
 }

 private void Update()
 {
     UpdateSelection();
     DrawChessboard();
 }
 
 private void UpdateSelection()
 {
     if (!Camera.main)
         return;
     RaycastHit hit;
     if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 25.0f,LayerMask.GetMask("ChessPlane")))
     {
         selectionX = (int)hit.point.x;
         selectionY = (int)hit.point.z;
     }
     else
     {
         selectionX = -1;
         selectionY = -1;
     }
 }
 
 private void SpawnChessman(int index, Vector3 position)
 {
     GameObject go = Instantiate(chessmanPrefabs [index], position, Quaternion.identity) as GameObject;
     go.transform.SetParent(transform);
     activeChessman.Add(go);
 }

 private SpawnAllChessmans()
 {
     activeChessman = new List<GameObject>();

     //Spawn White Team!

     //King
     SpawnChessman(0, GetTileCenter(3, 0));

     //Queen
     SpawnChessman(1, GetTileCenter(4, 0));

     //Rooks
     SpawnChessman(2, GetTileCenter(4, 0));
     SpawnChessman(2, GetTileCenter(7, 0));

     //Bishops
     SpawnChessman(3, GetTileCenter(2, 0));
     SpawnChessman(3, GetTileCenter(5, 0));

     //Kinghts
     SpawnChessman(4, GetTileCenter(1, 0));
     SpawnChessman(4, GetTileCenter(6, 0));

     //Pawns
     for (int i = 0; i < 8; i++)
         SpawnChessman(5, GetTileCenter(i, 1));
     
 }

 private Vector3 GetTileCenter(int x, int y)
 {
     Vector3 origin = Vector3.zero;
     origin.x += (TILE_SIZE * x) + TILE_OFFSET;
     origin.y += (TILE_SIZE * y) + TILE_OFFSET;
     return origin;
 }

 private void DrawChessboard()
 {
     Vector3 widthLine = Vector3.right * 8;
     Vector3 heightLine = Vector3.forward * 8;


     for (int i=0; i <= 8; i++)
     {
         Vector3 start = Vector3.forward * i;
         Debug.DrawLine(start, start + widthLine);
         for (int j=0; j <= 8; j++)
         {
             start = Vector3.right * j;
             Debug.DrawLine(start, start + heightLine);
         }
     }
     // Draw the selection
     if(selectionX >= 0 && selectionY >=0)
     {
         Debug.DrawLine(Vector3.forward * selectionY + Vector3.right * selectionX, Vector3.forward * (selectionY + 1) + Vector3.right * (selectionX + 1));

         Debug.DrawLine(Vector3.forward * (selectionY + 1) + Vector3.right * selectionX, Vector3.forward * selectionY + Vector3.right * (selectionX + 1));
     }
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by unity_03phillipsm · Oct 16, 2020 at 09:04 AM

In the SpawnChessman function, when you instantiate the object, you can set its rotation. Replace Quaternion.identity with the rotation you need to make the prefab in the correct orientation.

  private void SpawnChessman(int index, Vector3 position)
  {
      GameObject go = Instantiate(chessmanPrefabs [index], position, Quaternion.identity) as GameObject;
      go.transform.SetParent(transform);
      activeChessman.Add(go);
  }
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 Hemin11 · Oct 16, 2020 at 10:14 AM 0
Share

Can you please explain briefly

avatar image unity_03phillipsm · Oct 16, 2020 at 08:19 PM 0
Share

From the docs https://docs.unity3d.com/ScriptReference/Object.Instantiat e.html

public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);

So, in your code above, replace Quaternion.identity with a rotation variable that matches what you need. And if your rotation is defined by a Vector3, then you might need to convert it to a Quaternion using Quaternion.Euler(new Vector3(0, 0, 0))

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

137 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 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

Highlighting background triggered from a 3d model 0 Answers

How to paint on a 3D model and create a texture? 3 Answers

Scroll Paper or Carpet 2 Answers

3D model script for mesh and vertice deformatoin on mobile using touch and drag deformations by the user 1 Answer

best practices for importing and updating models 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges