Question by
unity_0yC6vhmB0khegg · Aug 10, 2019 at 07:07 AM ·
mathplayer movement
How to control horizontal movement while making Sine wave pattern?
Hi,
For a very small personal project in AR i'm trying to control the movement of a sphere(Only Horizontally) while it's making a Sinewave pattern.
For example:
Sphere is going from point A to point B with a sinewave pattern. I press the right button (on the touchscreen of the phone) The spere moves to the right but it maintains it's wave without chaging magnitude, frequency, etc.
I've tried with a Sinewave script:
using UnityEngine;
using System.Collections;
public class Projectile : MonoBehaviour {
public float MoveSpeed = 5.0f;
public float frequency = 20.0f; // Speed of sine movement
public float magnitude = 0.5f; // Size of sine movement
private Vector3 axis;
private Vector3 pos;
void Start () {
pos = transform.position;
//DestroyObject(gameObject, 1.0f);
axis = transform.right; // May or may not be the axis you want
}
void Update () {
pos += transform.up * Time.deltaTime * MoveSpeed;
transform.position = pos + axis * Mathf.Sin (Time.time * frequency) * magnitude;
}
}
And then adding a movement script to the camera:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour {
//variables
public float moveSpeed = 300;
public GameObject character;
private Rigidbody characterBody;
private float ScreenWidth;
// Use this for initialization
void Start () {
ScreenWidth = Screen.width;
characterBody = character.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
int i = 0;
//loop over every touch found
while (i < Input.touchCount) {
if (Input.GetTouch (i).position.x > ScreenWidth / 2) {
//move right
RunCharacter (1.0f);
}
if (Input.GetTouch (i).position.x < ScreenWidth / 2) {
//move left
RunCharacter (-1.0f);
}
++i;
}
}
void FixedUpdate(){
#if UNITY_EDITOR
RunCharacter(Input.GetAxis("Horizontal"));
#endif
}
private void RunCharacter(float horizontalInput){
//move player
characterBody.AddForce(new Vector2(horizontalInput * moveSpeed * Time.deltaTime, 0));
}
}
But they override each other and they don't work simultaneously.
Anyone got an idea?
Comment