- Home /
iTween RotateTo in C#
My character automatically runs forward and I'm trying to SMOOTHLY rotate the camera to an object behind the character by holding down R and then have the character face an object in front of it by releasing R.
My code is below. Any help is appreciated.
using UnityEngine;
using System.Collections;
public class Rotation02 : MonoBehaviour {public Transform target; public Transform target2;
// Update is called once per frame
void Update () {
if (Input.GetKeyUp(KeyCode.R))
iTween.RotateTo(gameObject,iTween.Hash(target.position),"time",2.3f);
I get an error that says: Assets/Standard Assets/Scripts/General Scripts/Rotation02.cs(11,32): error CS1501: No overload for method `RotateTo' takes `4' arguments
What is the problem you're having with the code right now?
I get errors:
Assets/Standard Assets/Scripts/General Scripts/Rotation02.cs(11,41): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected
Assets/Standard Assets/Scripts/General Scripts/Rotation02.cs(11,32): error CS1502: The best overloaded method match for `iTween.RotateTo(UnityEngine.GameObject, System.Collections.Hashtable)' has some invalid arguments
Assets/Standard Assets/Scripts/General Scripts/Rotation02.cs(11,32): error CS1503: Argument `#1' cannot convert `object' expression to type `UnityEngine.GameObject'
I edited my post above justinl and deleted that previous post.
Thanks latsushi. Your error is due to the fact that ALL the properties of the tween need to be inside of the Hash() parenthesis. Also, each parameter needs a pair of values. The first one telling iTween what property you want to change, and the second one being the value. Your line of code should read: iTween.RotateTo(gameObject,iTween.Hash("rotation", target.position, "time", 2.3f));
Answer by justinl · Sep 29, 2012 at 12:58 PM
You have several errors in your code:
You cannot convert a transform into a game object. Instead of writing `GameObject(target)`, write `target.gameObject`.
Secondly, you're not giving the tween anywhere to rotate to. It's the equivalent of you saying to someone "hey, rotate to _" but you never actually say where to rotate. The first parameter of RotateTo() tells iTween what GameObject to rotate, and then inside the `Hash()`, you need to specify where to rotate among other additional parameters such as the `easeType` which you are using.
For example inside your Hash() it should say something like `iTween.Hash("x", 25, "y", 90, "z", 0)`. You really should read the official iTween documentation to get a good handle of how to use that method.
I read the official iTween documentation for two hours yesterday but I still have trouble. I'll try your suggestions justini. Thank you for your input.
I thought I was telling the tween to rotate to a target that I would select from the project window.
the first parameter of RotateTo() should be the object being acted upon. So probably in your case it's going to be the object that this script is attached to, which would be gameObject. Then, the second argument is the the Hash() object, where you specify things such as where you would like the object to rotate to. Since you're probably going to supply a Vector3 type position in space, you can use iTween.Hash("rotation", target.position). Btw no problem. Happy to help!
Your answer
Follow this Question
Related Questions
iTween RotateTo 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Itween gradual jump 2d 1 Answer