Question by
xWerner · Nov 15, 2015 at 07:08 PM ·
rotate objectballrotatearoundbackdegree
Rotate Ball specific number of degrees and back again
Hey,
i want to rotate an object/ball 30 degree to the left side then back to the starting point and then -45 degree do the right side and back again to the starting point. After some seconds this should repead. I have tried this for hours and with different ways for example with "transform.eulerAngles" , "transform.rotation", "rotateAround" and so on but nothing worked fine. The rotation speed should be variable.
I used the searchfunction and edited some of the scripts I found but I could not score a goal with that.
Here are three attempts wich are not working:
using UnityEngine;
using System.Collections;
public class bowlerman_rotation_menu : MonoBehaviour {
public float rotation_speed = 10;
public float target_left_reached;
public float target_right_reached;
bool targetreached_right = true;
bool targetreached_left = false;
// Use this for initialization
void Start () {
//target_minus_right = this.transform.position + new Vector3(0, target_right, 0);
//target_plus_left = this.transform.position + new Vector3(0, target_z, 0);
}
void Update(){
if (targetreached_right)
{
Target_reached_left();
}
if (targetreached_left)
{
Target_reached_right();
}
}
void Target_reached_left()
{
target_left_reached += Input.GetAxis("Horizontal");
transform.eulerAngles = new Vector3(10, target_left_reached, 0);
//transform.RotateAround(this.transform.position, this.transform.up, rotation_speed * Time.deltaTime);
if (transform.eulerAngles.y == target_left_reached)
{
targetreached_right = false;
targetreached_left = true;
}
}
void Target_reached_right()
{
target_right_reached += Input.GetAxis("Horizontal");
transform.eulerAngles = new Vector3(-10, target_right_reached, 0);
//transform.RotateAround(this.transform.position, this.transform.up, rotation_speed * Time.deltaTime);
if (transform.eulerAngles.y == target_right_reached)
{
targetreached_right = true;
targetreached_left = false;
}
}
}
====================================
using UnityEngine;
using System.Collections;
using System;
public class bowlerman_rotate : MonoBehaviour
{
public float speed = 1.0f;
public float target_z;
public float target_minus_z;
Vector3 target = Vector3.zero;
Vector3 target_minus = Vector3.zero;
public float step;
public float magnitutde;
bool targetreached_right = true;
bool targetreached_left = false;
void Start()
{
target_minus = this.transform.position + new Vector3(0, target_minus_z, 0);
target = this.transform.position + new Vector3(0, target_z, 0);
}
void Update()
{
step = speed * Time.deltaTime;
if (targetreached_right)
{
Targetreached_left();
}
if (targetreached_left)
{
Targetreached_right();
}
}
void Targetreached_right()
{
transform.position = Vector3.RotateTowards(transform.position, target_minus, step, magnitutde);
if (Mathf.Approximately(transform.rotation.y, target.y))
{
targetreached_right = true;
targetreached_left = false;
}
}
void Targetreached_left()
{
transform.position = Vector3.MoveTowards(transform.position, target_minus, step);
if (Mathf.Approximately(transform.rotation.y, target_minus.y))
{
targetreached_left = true;
targetreached_right = false;
}
}
}
=======================================
public class bowlerman_rotate : MonoBehaviour {
//public float rotation_speed = 50.0f;
public float speed = 5.0f;
void Start()
{
}
// Update is called once per frame
void Update () {
transform.Rotate(Vector3.up * Time.deltaTime * speed, Space.Self);
//transform.rotation = Quaternion.AngleAxis(47, Vector3.up * Time.deltaTime* slowdown);
// transform.rotation = Quaternion.FromToRotation(Vector3.up, transform.(new Vector3(0, 47, 0) * Time.deltaTime));
//transform.RotateAround(this.transform.position, this.transform.up, rotation_speed * Time.deltaTime);
}
}
Comment