Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Follett · Feb 01, 2012 at 01:36 PM · rotationinstantiateraycastquaternion

Setting rotation of instantiated object to that of a raycast?

The goal of my code is too find the angle of the exact point of where the ray cast hits and then instantiate an object there with the angle (x and y) of the hit point and rotate the new object accordingly. So far I have only attempted the Y angle and have failed miserably and have no idea why. All if does is spawn the object with no rotation just 0,0,0! Heres my code so far.


 using UnityEngine;

using System.Collections;

public class Build : MonoBehaviour

{

public Ray ray;

public RaycastHit hit;

public GameObject Piece;

public Object clone;

public float range = 10;

void Update(){

if (Input.GetButtonDown("Fire1")){

         Place();

}

}

void Place(){

ray = Camera.main.ScreenPointToRay (Input.mousePosition);

if (Physics.Raycast (ray,out hit, 100)) {

Debug.DrawLine (ray.origin, hit.point);

 Rigidbody clone;

    clone = Instantiate(Piece,hit.point,Quaternion.identity) as Rigidbody;

 clone.transform.up = hit.normal;

 Vector3 rotation = clone.transform.localEulerAngles;

 rotation.y = transform.localEulerAngles.y;

 clone.transform.localEulerAngles = rotation;

 

     }

     

 }



}

Comment
Add comment · Show 1
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 Follett · Feb 01, 2012 at 01:30 PM 0
Share

Sorry for the weird formatting of the code. I dont know why but even when I edit I cant fix it.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Owen-Reynolds · Feb 01, 2012 at 06:13 PM

clone.transform.LookAt(hit.normal); will aim it along the normal. In general, LookAt(someDirection) is the simplest way to set a rotation. Also look at the optional second input for setting "up".

The problem is that rotation is done with Quaternions, not angles. There's a conversion if you know all angles: rotation=Quaternion.Euler(0,90,0);. But setting or examining the x,y,z angles one at a time does unpredictable things.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate not shooting in right direction 1 Answer

Instantiated objects always facing the center 2 Answers

How to instantiate on custom rotation? 1 Answer

Rotating an object to equal normals of object below 1 Answer

Quarternion.LookRotation is only rotating my game object on one axis 1 Answer


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