RotateTowards

I am trying to call this function in a C# script of mine and I am getting an error saying RotateTowards does not exist in current context. Am I missing a using statement or something?

using UnityEngine;
using System.Collections;
using System;

transform.rotation = RotateTowards(transform.rotation, targetRotation, Input.acceleration.x);

yes

transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Input.acceleration.x);

doh, Thanks for the help, getting this error now if you know anything about it, if not thanks for the help anyway =)

transform.rotation assign attempt for ‘Constructor’ is not valid. Input rotation is { NaN, NaN, NaN, NaN }.
UnityEngine.Transform:set_rotation(Quaternion)

Can’t fix errors without posted code

Do a test for NaN before applying the rotation, numbers very close to 0 or infinity will be (Not a Number) NaN due to floating point error.

if(float.IsNaN(yourNumber)) return;

using UnityEngine;
using System.Collections;
using System;

public class tilt : MonoBehaviour {
	
		CharacterController cc;
	
	// Use this for initialization
	void Start () {
		cc = gameObject.GetComponent<CharacterController>();
	}
	
	// Update is called once per frame
	void Update () {
		MovePlayer();
		RotatePlayer();
	}
	
	void MovePlayer() {   
		

		//Vector3 dir = Vector3.zero;
		if (Input.acceleration.y > 0.2)
		{
		Vector3 forward = cc.transform.forward;	
		forward *= 1.7f;
		forward.y = 0.0f;
			
			
		cc.Move(forward * Time.deltaTime);
		}
		
		if (Input.acceleration.y < -0.2)
		{
		Vector3 backward = -cc.transform.forward;	
		backward *= 1.7f;
		backward.y = 0.0f;
						
		cc.Move(backward * Time.deltaTime);
		
		}
    }

	void RotatePlayer() {
		
		// find speed based on delta
	    float curSpeed = Time.deltaTime * 1.5f;
		
            Quaternion targetRotation = Quaternion.Euler(0, Input.acceleration.x * curSpeed + transform.rotation.eulerAngles.y, 0);
            transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Input.acceleration.x);
	}
	
}

that is the current script producing the NaN errors

any ideas?

You sure you want rotateTowards, not slerp?
also try printing the vlues for input.accelation to make sure that the quaternion is getting a value

how would I do that using slerp?

Slerp (or lerp)

youve got a from and to already with targetRotation

i tried slerp with the same params and I get no errors, but when I load it onto a device the rotation does nothing. MovePlayer should move back and forth only with Input.accelerator.y, and RotatePlayer should only rotate the player on the Y axis using Input.Accelerator.x. My code starting this problem was

localRotation.y += Input.acceleration.x * curSpeed;

which would rotate the player to turn left and right but would progressively slow down when turning 180 degrees and if you turned exactly behind you it would lock up, would just like to be able to rotate the player around freely. I appreciate the advice john =)

This is my current code

using UnityEngine;
using System.Collections;
using System;

public class tilt : MonoBehaviour {
	
		CharacterController cc;
		private Quaternion localRotation;
		//float speed = 0.5f;
	
	// Use this for initialization
	void Start () {
		cc = gameObject.GetComponent<CharacterController>();
		localRotation = transform.rotation;
		
	}
	
	// Update is called once per frame
	void Update () {
		MovePlayer();
		RotatePlayer();
	}
	
	void MovePlayer() {   
		

		//Vector3 dir = Vector3.zero;
		if (Input.acceleration.y > -0.6)
		{
		Vector3 forward = cc.transform.forward;	
		forward *= 1.7f;
		forward.y = 0.0f;
			
		//forward.y = Input.acceleration.y;
			
		cc.Move(forward * Time.deltaTime);
		}
		
		if (Input.acceleration.y < -0.8)
		{
		Vector3 backward = -cc.transform.forward;	
		backward *= 1.7f;
		backward.y = 0.0f;
			
		//forward.y = Input.acceleration.y;
			
		cc.Move(backward * Time.deltaTime);
		
		}


    }

	void RotatePlayer() {
		
		// find speed based on delta
		float curSpeed = Time.deltaTime * 1.5f;

		//localRotation.y += Input.acceleration.x * curSpeed;
        Quaternion targetRotation = Quaternion.Euler(0, Input.acceleration.x * curSpeed + transform.rotation.eulerAngles.y, 0);
        transform.rotation = Quaternion.Slerp(localRotation, targetRotation, Input.acceleration.x);
		localRotation.y = transform.rotation.y;
        
	}
	
}

At the end we can get the error .

If you have :

RotateTowards(Quaternion from, Quaternion to, float maxDegreesDelta);

width maxDegreesDelta = 0 , it gives your de NaN NaN error.